diff --git a/libs/ui/common/src/lib/commandsR/stores/cart.ts b/libs/ui/common/src/lib/commandsR/stores/cart.ts index 75176e6..ce8516c 100644 --- a/libs/ui/common/src/lib/commandsR/stores/cart.ts +++ b/libs/ui/common/src/lib/commandsR/stores/cart.ts @@ -2,67 +2,38 @@ import {create} from "zustand/index"; export interface CartsState { carts: {[tableNumber: number]: {itemId: string, quantity: number}[]}; - addItem: (tableNumber: number, itemId: string, quantity: number) => void; - removeItem: (itableNumber: number, itemId: string, quantity: number) => void; + updateItem: (itableNumber: number, itemId: string, quantity: number) => void; resetCart: (tableNumber: number) => void; } export const useCarts = create((set) => ({ carts: {}, - addItem: (tableNumber, itemId, quantity) => set((state) => { - const currentCart = state.carts[tableNumber] || []; - - // Check if item exists - const itemIndex = currentCart.findIndex( - (element) => element.itemId === itemId - ); - - return { - carts: { - ...state.carts, - [tableNumber]: - itemIndex !== -1 - ? currentCart.map((item, index) => - index === itemIndex - ? { ...item, quantity: item.quantity + quantity } - : item - ) - : [...currentCart, { itemId, quantity }], - }, - }; - }), - removeItem: (tableNumber, itemId, quantity) => - set((state) => { - const currentCart = state.carts[tableNumber] || []; - - const itemIndex = currentCart.findIndex( - (element) => element.itemId === itemId - ); - - if (itemIndex === -1) return state; - - const updatedCart = currentCart - .map((item, index) => - index === itemIndex - ? { ...item, quantity: item.quantity - quantity } - : item - ) - .filter((item) => item.quantity > 0); - - return { - carts: { - ...state.carts, - [tableNumber]: updatedCart, - }, - }; - }), - resetCart: (tableNumber) => - set((state) => ({ - carts: { - ...state.carts, - [tableNumber]: [], - }, - }) - ), + updateItem: (tableNumber, itemId, quantity) => set((state) => { + const currentCart = state.carts[tableNumber] || []; + + const itemIndex = currentCart.findIndex(element => element.itemId === itemId); + + return { + carts: { + ...state.carts, + tableNumber: + (itemIndex === -1) ? [...currentCart, {itemId, quantity}].filter(element => element.quantity > 0) + : currentCart.map(element => { + if (element.itemId === itemId) { + element.quantity = quantity + } + return element + }).filter(element => element.quantity > 0) + } + } + }), + resetCart: (tableNumber) => + set((state) => ({ + carts: { + ...state.carts, + [tableNumber]: [], + }, + }) + ), }) );