BrixCafe/app/utils/cartUtils.tsx
2025-04-18 06:09:09 +01:00

22 lines
519 B
TypeScript

import { CartItem } from '@/app/constants/types';
export const calculateTotal = (items: CartItem[]): number => {
return items.reduce((total, item) => {
return total + item.price * item.quantity;
}, 0);
};
export const updateItemQuantity = (
items: CartItem[],
itemId: string,
newQuantity: number
): CartItem[] => {
// Don't allow negative quantities
if (newQuantity < 0) {
return items;
}
return items.map((item) =>
item.id === itemId ? { ...item, quantity: newQuantity } : item
);
};