(product, quantity)
| 14 | let index; |
| 15 | |
| 16 | const onAdd = (product, quantity) => { |
| 17 | // check if product already in the cart |
| 18 | const checkProductInCart = cartItems.find((item) => item._id === product._id); |
| 19 | |
| 20 | setTotalPrice((prevTotalPrice) => prevTotalPrice + product.price * quantity); |
| 21 | setTotalQty((prevTotalQty) => prevTotalQty + quantity); |
| 22 | |
| 23 | // if product already in cart, then increase the qty of product instead of add the same product in cart |
| 24 | if(checkProductInCart) { |
| 25 | // update the actual item in the cart |
| 26 | const updatedCartItems = cartItems.map((cartProduct) => { |
| 27 | if(cartProduct._id === product._id) return { |
| 28 | ...cartProduct, |
| 29 | quantity: cartProduct.quantity + quantity |
| 30 | } |
| 31 | }) |
| 32 | |
| 33 | setCartItems(updatedCartItems); |
| 34 | } else { |
| 35 | // if when dont have the item in the cart |
| 36 | product.quantity = quantity; |
| 37 | setCartItems([...cartItems, {...product}]); |
| 38 | } |
| 39 | toast.success(`${qty} ${product.name} added to the cart`); |
| 40 | } |
| 41 | |
| 42 | const onRemove = (product) => { |
| 43 | foundProduct = cartItems.find((item) => item._id === product._id) |
nothing calls this directly
no outgoing calls
no test coverage detected