diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js index 9b1bb2d..51cce21 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Cart.js @@ -1,5 +1,6 @@ import { useEffect } from "react"; import { createContext, useState } from "react"; +import Cookies from "universal-cookie"; export const CartContext = createContext({ items: [], @@ -15,14 +16,26 @@ export const CartContext = createContext({ export function CartProvider({ children }) { const [cartProducts, setCartProducts] = useState([]); + const cookies = new Cookies(); + useEffect(() => { - + if (cartProducts.length == 0) { + if (cookies.get('cart') != null && cookies.get('cart').length != 0) + setCartProducts(cookies.get('cart')); + } }, []); + useEffect(() => { + cookies.set('cart', cartProducts, { path: '/', SameSite: 'strict', secure: true, maxAge: 2592000 }) + + + }, [cartProducts]); + function addToCart(product, qty) { setCartProducts([...cartProducts, { ...product, quantity: parseFloat(qty), remaining: parseFloat(product.quantity) }]); + } function getProductQuantity(id) {