-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateContext.js
110 lines (82 loc) · 3.18 KB
/
StateContext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React, { createContext, useContext, useState, useEffect } from 'react';
import { toast } from 'react-hot-toast'; //POP UP NOTIFICATION
const Context = createContext();
export const StateContext = ({ children }) => {
const [showCart, setShowCart] = useState(false);
const [cartItems, setCartItems] = useState([]);
const [totalPrice, setTotalPrice] = useState(0);
const [totalQuantities, setTotalQuantities] = useState(0);
const [qty, setQty] = useState(1);
let foundProduct;
let index;
const onAdd = (product, quantity) => {
const checkProductInCart = cartItems.find((item) => item._id === product._id);
setTotalPrice((prevTotalPrice) => prevTotalPrice + product.price * quantity);
setTotalQuantities((prevTotalQuantities) => prevTotalQuantities + quantity);
if(checkProductInCart) {
const updatedCartItems = cartItems.map((cartProduct) => {
if(cartProduct._id === product._id) return {
...cartProduct,
quantity: cartProduct.quantity + quantity
}
})
setCartItems(updatedCartItems);
} else {
product.quantity = quantity;
setCartItems([...cartItems, { ...product }]);
}
toast.success(`${qty} ${product.name} added to the cart.`);
//FLASH message
}
const onRemove = (product) => {
foundProduct = cartItems.find((item) => item._id === product._id);
const newCartItems = cartItems.filter((item) => item._id !== product._id);
setTotalPrice((prevTotalPrice) => prevTotalPrice -foundProduct.price * foundProduct.quantity);
setTotalQuantities(prevTotalQuantities => prevTotalQuantities - foundProduct.quantity);
setCartItems(newCartItems);
}
const toggleCartItemQuanitity = (id, value) => {
foundProduct = cartItems.find((item) => item._id === id)
index = cartItems.findIndex((product) => product._id === id);
const newCartItems = cartItems.filter((item) => item._id !== id)
if(value === 'inc') {
setCartItems([...newCartItems, { ...foundProduct, quantity: foundProduct.quantity + 1 } ]);
setTotalPrice((prevTotalPrice) => prevTotalPrice + foundProduct.price)
setTotalQuantities(prevTotalQuantities => prevTotalQuantities + 1)
} else if(value === 'dec') {
if (foundProduct.quantity > 1) {
setCartItems([...newCartItems, { ...foundProduct, quantity: foundProduct.quantity - 1 } ]);
setTotalPrice((prevTotalPrice) => prevTotalPrice - foundProduct.price)
setTotalQuantities(prevTotalQuantities => prevTotalQuantities - 1)
}
}
}
const incQty = () => {
setQty((prevQty) => prevQty + 1);
}
const decQty = () => {
setQty((prevQty) => {
if(prevQty - 1 < 1) return 1;
return prevQty - 1;
});
}
return(
<Context.Provider
value={{
showCart,
setShowCart,
cartItems,
totalPrice,
totalQuantities,
qty,
incQty,
decQty,
onAdd,
toggleCartItemQuanitity,
onRemove
}}>
{children}
</Context.Provider>
)
}
export const useStateContext = () => useContext(Context);