-
Notifications
You must be signed in to change notification settings - Fork 70
Bugfix sorting #32
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
Bugfix sorting #32
Conversation
| const sortProducts = (name) => { | ||
| let optionsArr = [...sortOptions]; | ||
| optionsArr.map((item) => { | ||
| if (item.name === name) { | ||
| // switch on the sorting method clicked by user | ||
| item.current = true; | ||
| } | ||
| // switch off a previously selected sorting method if it had been selected | ||
| else item.current = false; | ||
| }); | ||
| setSortOptions((prevState) => optionsArr); | ||
| }; |
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.
praise: I like how you separated the logic into a method, and simply call it below in the click handler. Good work!
| for (let i = 0; i < sortOptions.length; i++) { | ||
| if (sortOptions[i].current === true) { | ||
| if (sortOptions[i].name === "Price") { | ||
| console.log("sorting by price"); |
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.
nitpick: please remove console.log
| } | ||
| } | ||
| setProducts((prevState) => sortedProducts); | ||
| }, [sortOptions]); |
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.
praise: Good work tying this logic to changes to the sortOptions dependency.
sbmsr
left a comment
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.
As usual, good work Animan. Clean code with good separation of logic from view code.
Created sorting functionality via a sorting function in a useState that has the sortOptions as a dependency. There might be a more concise way to write out the sorting function that is more performant, I was having trouble getting a Javascript Array method that could work for this case.