| 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); |