MCPcopy Create free account
hub / github.com/Source-Controller/eCommerce-Fullstack / toggleCartItemQuantity

Function toggleCartItemQuantity

context/StateContext2.js:51–69  ·  view source on GitHub ↗
(id, value)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected