Skip to content
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

Cart features; Issues 7,6,5 Fixed #47

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed sort filtering
alexalextran committed Nov 29, 2022
commit 2892ce81a3d43e60d3d571893fd96dad4282e6fa
17 changes: 15 additions & 2 deletions src/Components/ProductFilters.jsx
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ function classNames(...classes) {
return classes.filter(Boolean).join(" ");
}

export default function ProductFilters({ filterOptions, setFilterOptions, sortOptions, setSortOptions }) {
export default function ProductFilters({ filterOptions, setFilterOptions, sortOptions, setSortOptions, setProducts, products }) {
return (
<Disclosure
as="section"
@@ -109,7 +109,20 @@ export default function ProductFilters({ filterOptions, setFilterOptions, sortOp
{({ active }) => (
<button
onClick={() => {
// TODO
if(option.name == "Price"){
setSortOptions(
[
{ name: "Price", current: true },
{ name: "Newest", current: false },
])
}
else{
setSortOptions(
[
{ name: "Price", current: false },
{ name: "Newest", current: true },
])
}
}}
className={classNames(
option.current ? "font-medium text-gray-900" : "text-gray-500",
24 changes: 21 additions & 3 deletions src/Components/ProductTable.jsx
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ const getDefaultSortOptions = () => {
};

export default function ProductTable({ cart, updateCart }) {
let [products, setProducts] = useState([]);
var [products, setProducts] = useState([]);

const [filterOptions, setFilterOptions] = useState(getDefaultFilterOptions());
const [sortOptions, setSortOptions] = useState(getDefaultSortOptions());
@@ -39,16 +39,34 @@ export default function ProductTable({ cart, updateCart }) {
let res = await fetch("http://localhost:3001/products");
let body = await res.json();
setProducts(body);

for(let i = 0; i < sortOptions.length; i++){

if(sortOptions[i].name == "Price" && sortOptions[i].current == true){
(setProducts(products.sort((a, b) => a.price < b.price ? 1 : -1)))
break;
}
else if(sortOptions[i].name == "Newest" && sortOptions[i].current == true){
(setProducts(products.sort((a, b) => a.releaseDate < b.releaseDate ? 1 : -1)))
break;
}
}



};
fetchProducts();
}, []);



}, [sortOptions]);


return (
<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 }} />
<ProductFilters {...{ filterOptions, setFilterOptions, sortOptions, setSortOptions, setProducts, products }} />

<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) => (