Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fixed Search product fails for category filter when categoryId is string - @adityasharma7 (#3929)
- Revert init filters in Vue app - @gibkigonzo (#3929)
- All categories disappearing if you add the child category name to includeFields - @1070rik (#4015)
- Fix overlapping text in PersonalDetails component - @jakubmakielkowski (#4024)
- Redirect from checkout to home with a proper store code - @Fifciu

Expand Down
10 changes: 6 additions & 4 deletions core/modules/catalog/helpers/slugifyCategories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ const createSlug = (category: ChildrenData): string => {

const slugifyCategories = (category: Category | ChildrenData): Category | ChildrenData => {
if (category.children_data) {
for (let subcat of category.children_data) {
if (subcat.name) {
return slugifyCategories({ ...subcat, slug: createSlug(subcat) } as any as ChildrenData)
category.children_data.forEach((subCat: ChildrenData): void => {
Copy link
Contributor

@andrzejewsky andrzejewsky Jan 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1070rik What about shomething like this (simpler)?

category.children_data = category.children_data.map(ch => ({
  ...ch,
  ...(ch.name ? { slug: createSlug(subCat) } : {})
}))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I don't really see the added value of doing this instead of a forEach, this would only work for the first 2 levels of categories.
The slugifyCategories(subCat) call at the end of the loop would cause a recursive loop which would give the subcategories of that subcategory a slug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1070rik yes, but I didn't mention about deleting slugifyCategories(subCat) my changes are related to only for, it's just simpler, and actually entire function could be refactored with using map call. However - it's your choice, just let me know if you want to spare time on it or not - it's still good PR (it solves the issue, thanks!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turning it into a map and doing the function call doesn't really make the code look better or easier, it only makes it more complex in my opinion. I still prefer the forEach method over the map since it easier to understand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me, it looks much better, it's just imperative vs functional. In this case, it maybe doesn't change so much, but remember that in the for you are mutating values while map returns a new array - no side effects (they could be painful sometimes). As I mentioned entire function could be refactored using map - instead of modifying category you can return a new one with slugs inside (again, no side effects).

Of course, this solution is also fine, thanks!

if (subCat.name) {
subCat.slug = createSlug(subCat)
}
}

slugifyCategories(subCat)
})
}

return category
Expand Down