MCPcopy Index your code
hub / github.com/Source-Controller/eCommerce-Fullstack / toggleCartItemQuantity

Function toggleCartItemQuantity

context/StateContext.js:49–65  ·  view source on GitHub ↗
(id, value)

Source from the content-addressed store, hash-verified

47 }
48
49 const toggleCartItemQuantity = (id, value) => {
50 foundProduct = cartItems.find((item) => item._id === id)
51 index = cartItems.findIndex((product) => product._id === id);
52 const newCartItems = cartItems.filter((item) => item._id !== id)
53
54 if(value === 'inc') {
55 setCartItems([...newCartItems, { ...foundProduct, quantity: foundProduct.quantity + 1 } ]);
56 setTotalPrice((prevTotalPrice) => prevTotalPrice + foundProduct.price)
57 setTotalQty(prevTotalQty => prevTotalQty + 1)
58 } else if(value === 'dec') {
59 if (foundProduct.quantity > 1) {
60 setCartItems([...newCartItems, { ...foundProduct, quantity: foundProduct.quantity - 1 } ]);
61 setTotalPrice((prevTotalPrice) => prevTotalPrice - foundProduct.price)
62 setTotalQty(prevTotalQty => prevTotalQty - 1)
63 }
64 }
65 }
66
67 const incQty = () => {
68 setQty((prevQty) => prevQty + 1);

Callers 1

cart.jsFile · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected