Skip to content

Commit

Permalink
fixed: ony one option issue developer-job-simulation#1
Browse files Browse the repository at this point in the history
  • Loading branch information
antono91 committed Aug 10, 2023
1 parent 8863427 commit 23cc18f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
21 changes: 14 additions & 7 deletions src/Components/ProductFilters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,21 @@ export default function ProductFilters({
<button
onClick={() => {
// TODO
const newOption = {
...option,
current: !option.current,
const clikedOption = sortOptions.find(
(o) => o.name === option.name
)
const otherOption = sortOptions.find(
(o) => o.name !== option.name
)
if (!clikedOption.current) {
clikedOption.current = true
if (otherOption.current) {
otherOption.current = false
}
} else {
clikedOption.current = false
}
if (option.name === 'Price')
setSortOptions([newOption, sortOptions[1]])
else if (option.name === 'Newest')
setSortOptions([sortOptions[0], newOption])
setSortOptions([clikedOption, otherOption])
}}
className={classNames(
option.current
Expand Down
7 changes: 5 additions & 2 deletions src/Components/ProductTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ export default function ProductTable({ cart, updateCart }) {

// Sort products
const sortProducts = (p) => {
if (sortOptions[0].current) {
const price = sortOptions.find((o) => o.name === 'Price')
const newest = sortOptions.find((o) => o.name === 'Newest')

if (price.current) {
return p.sort((a, b) => a.price - b.price)
} else if (sortOptions[1].current) {
} else if (newest.current) {
return p.sort((a, b) => a.releaseDate - b.releaseDate)
} else {
return p
Expand Down

0 comments on commit 23cc18f

Please sign in to comment.