-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix: slugify sub-category names #4015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: slugify sub-category names #4015
Conversation
| 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 => { |
There was a problem hiding this comment.
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) } : {})
}))
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
Short Description and Why It's Useful
The old function wouldn't show any categories at all if you added the "*.name" field in the includedFields array in your config. The way it works now is also easier to read.
Screenshots of Visual Changes before/after (if There Are Any)
Which Environment This Relates To
Check your case. In case of any doubts please read about Release Cycle
developbranch and want to merge it back todevelopreleasebranch and want to merge it back toreleasehotfixormasterbranch and want to merge it back tohotfixUpgrade Notes and Changelog
IMPORTANT NOTICE - Remember to update
CHANGELOG.mdwith description of your changeContribution and Currently Important Rules Acceptance