Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim55069633 committed May 4, 2023
1 parent fc37810 commit 0d6cbba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 33 additions & 1 deletion src/Components/ProductFilters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@ function classNames(...classes) {
return classes.filter(Boolean).join(" ");
}

export default function ProductFilters({ filterOptions, setFilterOptions, sortOptions, setSortOptions }) {
export default function ProductFilters({ filterOptions, setFilterOptions, sortOptions, setSortOptions, products, setProducts }) {

// define a new sort function
const customSort=( criteria )=>{

const productsCopy = [...products]

if( criteria == "Price")
productsCopy.sort(function(a, b){return a.price - b.price});
else if ( criteria == "Newest" )
productsCopy.sort(function(a, b){return Number(a.releaseDate) - Number(b.releaseDate) });

return productsCopy;
};

return (
<Disclosure
as="section"
Expand Down Expand Up @@ -110,6 +124,24 @@ export default function ProductFilters({ filterOptions, setFilterOptions, sortOp
<button
onClick={() => {
// TODO

// 1 bold the current sort option
const new_sortOptions = [];
sortOptions.forEach(element => {
if (element.name == option.name)
{
new_sortOptions.push({name: element.name, current:true});

}
else
new_sortOptions.push({name: element.name, current: false });
});
setSortOptions(new_sortOptions);

// 2 sort accordingly
setProducts(customSort(option.name));


}}
className={classNames(
option.current ? "font-medium text-gray-900" : "text-gray-500",
Expand Down
4 changes: 2 additions & 2 deletions src/Components/ProductTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default function ProductTable({ cart, updateCart }) {
<div className="bg-white">
<div className="max-w-2xl mx-auto px-4 sm:px-6 lg:max-w-7xl lg:px-8">
<h2 className="sr-only">Products</h2>
<ProductFilters {...{ filterOptions, setFilterOptions, sortOptions, setSortOptions }} />

{/* pass two more props {products, setProducts} to the Component */}
<ProductFilters {...{ filterOptions, setFilterOptions, sortOptions, setSortOptions, products, setProducts }} />
<div className="grid grid-cols-1 gap-y-10 sm:grid-cols-2 gap-x-6 lg:grid-cols-3 xl:grid-cols-4 xl:gap-x-8">
{products.map((product) => (
<a key={product.id} className="group">
Expand Down

0 comments on commit 0d6cbba

Please sign in to comment.