From 61170cdb6314263cf32b2fae5faa0d8940d058be Mon Sep 17 00:00:00 2001 From: PrymeTyme Date: Thu, 13 Apr 2023 11:48:07 +0200 Subject: [PATCH 1/3] sorting by price/date,cart loclastorage,filter counter,infiniy loop fix --- package-lock.json | 1 + src/Components/Cart.jsx | 61 ++++++++++++++++++++++- src/Components/ProductFilters.jsx | 35 +++++++++++--- src/Components/ProductTable.jsx | 80 +++++++++++++++++++++++++++++-- src/Pages/Home.jsx | 18 ++++++- 5 files changed, 182 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index d01ddfa..bbd664e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "react-ecommerce", "version": "0.0.1", "dependencies": { "@headlessui/react": "^1.6.0", diff --git a/src/Components/Cart.jsx b/src/Components/Cart.jsx index 917b7a1..14a11e6 100644 --- a/src/Components/Cart.jsx +++ b/src/Components/Cart.jsx @@ -1,8 +1,67 @@ import { Dialog, Transition } from "@headlessui/react"; import { XIcon } from "@heroicons/react/outline"; -import React, { Fragment } from "react"; +import React, { Fragment, useEffect, useState } from "react"; + +/* const useLocalStorage = (storageKey, fallbackState) => { + const [value, setValue] = useState( + JSON.parse(localStorage.getItem(storageKey)) ?? fallbackState + ); + useEffect(() => { + localStorage.setItem(storageKey, JSON.stringify(value)); + }, [value, storageKey]); + + return [value, setValue]; +} */ export default function Cart({ open, setOpen, cart, updateCart }) { + /* const [localCart, setLocalCart] = useLocalStorage('localCart', cart) + + + + useEffect(() => { + if (localCart) { + console.log(localCart) + cart = localCart + console.log(cart) + } + }, []) + + useEffect(() => { + setLocalCart(cart) + }, [cart]); + */ + + + + /* useEffect(() => { + if (cart.length <= 0 && localCart != null) { + setLocalCart(localCart) + } else if (cart.length != 0) { + setLocalCart(cart) + } + }, [cart]); */ + + /* const jsonArray = JSON.stringify(cart) + + localStorage.setItem('cart', jsonArray) + + const str = localStorage.getItem('cart') + + const parsedArray = JSON.parse(str) + + console.log(parsedArray) */ + + /* if (localStorage.getItem('cart')===null) { + let jsonArray = JSON.stringify(cart) + localStorage.setItem('cart', jsonArray) + } else { + let str = localStorage.getItem('cart') + cart = JSON.parse(str) + + } + + console.log(cart) */ + return ( { + return checkedPriceState.filter(Boolean).length+checkedColorState.filter(Boolean).length; + } + + return (