-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.txt
88 lines (73 loc) · 2.95 KB
/
task.txt
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
-----------------------------
const finalPrice = parseFloat(price);
// console.log(finalPrice);
useEffect(() => {
const storedCartItems =
JSON.parse(localStorage.getItem("product-cart")) || [];
const storedCartItem = storedCartItems.find((item) => item._id === _id);
setCartItemsData(storedCartItems);
if (storedCartItem) {
setQuantity(storedCartItem.quantity);
const productPrice = parseFloat(storedCartItem.quantity * finalPrice);
setTotalPrice(productPrice);
}
}, [_id, finalPrice, setTotalPrice]);
const handleIncrease = (_id) => {
const updatedCartItems = cartItemsData.map((item) =>
item._id === _id ? { ...item, quantity: item.quantity + 1 } : item
);
localStorage.setItem("product-cart", JSON.stringify(updatedCartItems));
// console.log(updatedCartItems);
setCartItemsData(updatedCartItems);
setQuantity((prevQuantity) => parseFloat(prevQuantity + 1));
setTotalPrice((prevTotal) => prevTotal + finalPrice);
increaseAmount(finalPrice);
// const index = cartItems.findIndex((i) => i._id === _id);
// // const a = cartItems[index];
// if (index !== -1) {
// cartItems[index].quantity += 1;
// localStorage.setItem("product-cart", JSON.stringify(cartItems));
// const updatedQuantity = quantity + 1;
// setQuantity(updatedQuantity);
// const total = parseFloat(updatedQuantity * finalPrice);
// setTotalPrice(total);
// increaseAmount(finalPrice);
// console.log(finalPrice, totalPrice);
// } else {
// console.log("product not found");
// }
};
// console.log(finalPrice, totalPrice);
const handleDecrease = (_id) => {
if (quantity === 1) return;
const updatedCartItems = cartItemsData.map((item) =>
item._id === _id ? { ...item, quantity: item.quantity - 1 } : item
);
localStorage.setItem("product-cart", JSON.stringify(updatedCartItems));
setCartItemsData(updatedCartItems);
setQuantity((prevQuantity) => prevQuantity - 1);
setTotalPrice((prevTotal) => prevTotal - finalPrice);
// const index = cartItems.findIndex((i) => i._id === _id);
// if (index !== -1) {
// cartItems[index].quantity -= 1;
// localStorage.setItem("product-cart", JSON.stringify(cartItems));
// const updatedQuantity = quantity - 1;
// setQuantity(updatedQuantity);
// const total = parseFloat(updatedQuantity * finalPrice);
// setTotalPrice(total);
decreaseAmount(finalPrice);
// } else {
// console.log("product not found");
// }
};
// console.log(quantity);
// const saveCartInLocalStore = (myCart) => {
// localStorage.setItem("product-cart", JSON.stringify(myCart));
// let subt = 0;
// let keys = Object.keys(myCart);
// const getPrice = products?.map((item) => item._id === myCart[_id]);
// for (let i = 0; i < keys.length; i++) {
// subt = getPrice.price * myCart[keys[i]].qty;
// }
// setSubTotal(subt);
// };