diff --git a/favicon.ico b/favicon.ico deleted file mode 100644 index a11777c..0000000 Binary files a/favicon.ico and /dev/null differ diff --git a/logo192.png b/logo192.png deleted file mode 100644 index fc44b0a..0000000 Binary files a/logo192.png and /dev/null differ diff --git a/logo512.png b/logo512.png deleted file mode 100644 index a4e47a6..0000000 Binary files a/logo512.png and /dev/null differ diff --git a/src/app/Cart.jsx b/src/app/Cart.jsx index 7fecf57..81d6ea0 100644 --- a/src/app/Cart.jsx +++ b/src/app/Cart.jsx @@ -1,5 +1,7 @@ -import React, { useState } from 'react'; - +import React, { useState, useEffect } from 'react'; +import CartProductNotFound from '../components/CartProductNotFound'; +import { Link } from 'react-router-dom'; +import { BsCircleFill, BsChevronCompactRight, BsChevronCompactLeft } from "react-icons/bs"; import '../css/Cart.css' export default function Cart() { @@ -11,6 +13,124 @@ export default function Cart() { // }, 0) // ); // }, [basket]); + + + const [ProductCount, setProductCount] = useState(0); + const [products, setProducts] = useState([]); + const [productDetails, setProductDetails] = useState([]); + useEffect(() => { + const ProductCount = JSON.parse(localStorage.getItem('Products')) || []; + setProductCount(ProductCount.length); + }, []); + + useEffect(() => { + const fetchData = async () => { + try { + const response = await fetch("https://dummyjson.com/products"); + const result = await response.json(); + if (result && Array.isArray(result.products)) { + setProducts(result.products); + } + } catch (error) { + console.error('Error fetching data:', error); + } + }; + + fetchData(); + }, []); + + useEffect(() => { + const fetchProductDetails = async () => { + const ProductIds = JSON.parse(localStorage.getItem('Products')) || []; + const productsDetails = []; + + for (const productInfo of ProductIds) { + const productId = productInfo.id; + const productQuantity = productInfo.quantity; + const product = products.find(product => product.id === productId); + + if (product) { + const productDetail = { + thumbnail: product.thumbnail, + price: product.price, + title: product.title, + description: product.description, + id: product.id, + quantity: productQuantity, + }; + productsDetails.push(productDetail); + } + } + + setProductDetails(productsDetails); + }; + + fetchProductDetails(); + }, [products]); + + const DeleteProduct = (productId) => { + const DeleteProducts = JSON.parse(localStorage.getItem('Products')) || []; + const updatedFavorites = DeleteProducts.filter((product) => product.id !== productId); + localStorage.setItem('Products', JSON.stringify(updatedFavorites)); + setProductDetails(prevDetails => prevDetails.filter(product => product.id !== productId)); + setProductCount(prevCount => prevCount - 1); + }; + + const AddQuantity = (productId) => { + const productsInLocalStorage = JSON.parse(localStorage.getItem('Products')) || []; + const productIndex = productsInLocalStorage.findIndex(product => product.id === productId); + if (productIndex !== -1 && productsInLocalStorage[productIndex].quantity > 0) { + productsInLocalStorage[productIndex].quantity += 1; + + localStorage.setItem('Products', JSON.stringify(productsInLocalStorage)); + + const updatedProductDetails = productDetails.map(product => { + if (product.id === productId) { + return { + ...product, + quantity: product.quantity + 1 + }; + } + return product; + }); + + setProductDetails(updatedProductDetails); + } + }; + + const RemoveQuantity = (productId) => { + const productsInLocalStorage = JSON.parse(localStorage.getItem('Products')) || []; + const productIndex = productsInLocalStorage.findIndex(product => product.id === productId); + + if (productIndex !== -1 && productsInLocalStorage[productIndex].quantity > 0) { + productsInLocalStorage[productIndex].quantity -= 1; + + if (productsInLocalStorage[productIndex].quantity === 0) { + const updatedProductsInLocalStorage = productsInLocalStorage.filter(product => product.id !== productId); + localStorage.setItem('Products', JSON.stringify(updatedProductsInLocalStorage)); + + const updatedProductDetails = productDetails.filter(product => product.id !== productId); + setProductDetails(updatedProductDetails); + setProductCount(prevCount => prevCount - 1); + } else { + localStorage.setItem('Products', JSON.stringify(productsInLocalStorage)); + + const updatedProductDetails = productDetails.map(product => { + if (product.id === productId) { + return { + ...product, + quantity: product.quantity - 1 + }; + } + return product; + }); + setProductDetails(updatedProductDetails); + } + } + }; + + + return ( <> @@ -23,39 +143,40 @@ export default function Cart() {
Your Cart list is empty.
+Why not add some items to your Cart?
+