Skip to content

Commit

Permalink
remove-products
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadpy8 committed Aug 27, 2023
1 parent 7125dd5 commit 3cdbd66
Showing 1 changed file with 39 additions and 46 deletions.
85 changes: 39 additions & 46 deletions src/context/CartContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -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<Produts[]>([]);
const [shop, setShop] = useState<Produts[]>([]);

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 (
<CartContext.Provider value={{removeAll, addProduts, removeProduts,shop, userCart }}>
{children}
</CartContext.Provider>
);
};

export default CartContextProvider;
const [userCart, setUserCart] = useState<Produts[]>([])
const [shop, setShop] = useState<Produts[]>([])

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 (
<CartContext.Provider
value={{ removeAll, addProduts, removeProduts, shop, userCart }}
>
{children}
</CartContext.Provider>
)
}

export default CartContextProvider

0 comments on commit 3cdbd66

Please sign in to comment.