diff --git a/src/context/CartContextProvider.tsx b/src/context/CartContextProvider.tsx index 512d04d..9751846 100644 --- a/src/context/CartContextProvider.tsx +++ b/src/context/CartContextProvider.tsx @@ -1,55 +1,48 @@ -import React, { createContext, useEffect, useState } from 'react'; +import React, { createContext, useEffect, useState } from 'react' -import { Produts } from '../types/products.types'; +import { Produts } from '../types/products.types' type CartContextType = { - userCart: Produts[]; - addProduts: (id: number) => void; - removeProduts: (id: number) => void; - removeAll: () => void; + userCart: Produts[] + addProduts: (id: number) => void + removeProduts: (id: number) => void + removeAll: () => void shop: Produts[]; -}; +} type CartProps = { - children: React.ReactNode; -}; + children: React.ReactNode +} -export const CartContext = createContext({} as CartContextType); +export const CartContext = createContext({} as CartContextType) const CartContextProvider = ({ children }: CartProps) => { - - const [userCart, setUserCart] = useState([]); - const [shop, setShop] = useState([]); - - useEffect(() => { - - fetch("https://fakestoreapi.com/products") - .then(res => res.json()) - .then(data => { - console.log("data =>", data); - setShop(data); - }) - .catch(err => console.log(err)); - - }, []) - - const addProduts = (id: number) => { - - }; - - const removeProduts = (id: number) => { - - }; - - const removeAll = () => { - - }; - - return ( - - {children} - - ); -}; - -export default CartContextProvider; \ No newline at end of file + const [userCart, setUserCart] = useState([]) + const [shop, setShop] = useState([]) + + useEffect(() => { + fetch('https://fakestoreapi.com/products') + .then((res) => res.json()) + .then((data) => { + console.log('data =>', data) + setShop(data) + }) + .catch((err) => console.log(err)) + }, []) + + const addProduts = (id: number) => {} + + const removeProduts = (id: number) => setUserCart(prevProducts => prevProducts.filter(product => product.id !== id)); + + const removeAll = () => setUserCart([]); + + return ( + + {children} + + ) +} + +export default CartContextProvider