Skip to content

Conversation

@animanamit
Copy link

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.

Comment on lines +15 to +26
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);
};
Copy link
Member

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");
Copy link
Member

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]);
Copy link
Member

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.

Copy link
Member

@sbmsr sbmsr left a 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.

@sbmsr sbmsr closed this May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants