Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 4x 4x 2x 3x 2x 6x 4x 2x 3x 42x 2x | export function addItem(cart, product) {
const existing = cart.find((item) => item.product.id === product.id);
if (existing) {
return cart.map((item) =>
item.product.id === product.id
? { ...item, quantity: item.quantity + 1 }
: item,
);
}
return [...cart, { product, quantity: 1 }];
}
export function removeItem(cart, productId) {
return cart.filter((item) => item.product.id !== productId);
}
export function updateQuantity(cart, productId, quantity) {
if (quantity <= 0) return removeItem(cart, productId);
return cart.map((item) =>
item.product.id === productId ? { ...item, quantity } : item,
);
}
export function getSubtotal(cart) {
return cart.reduce((total, item) => total + item.product.price * item.quantity, 0);
}
export function getItemCount(cart) {
return cart.reduce((total, item) => total + item.quantity, 0);
}
|