From 52b247a0d592fd20ab4fbb9a2e8089d2972b22ba Mon Sep 17 00:00:00 2001 From: Tsukoyachi Date: Sat, 28 Sep 2024 17:57:08 +0200 Subject: [PATCH] fix: Mister apoorva want the itemId in addition of the shortname because of shitty backend --- .../common/src/lib/kitchen/kitchenService.ts | 2 +- libs/ui/common/src/lib/commandsR/Item.tsx | 6 +++--- libs/ui/common/src/lib/commandsR/orderingChoices.tsx | 9 +++++---- libs/ui/common/src/lib/commandsR/stores/cart.ts | 12 ++++++------ libs/ui/common/src/lib/summary/summary.tsx | 8 ++++---- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/libs/services/common/src/lib/kitchen/kitchenService.ts b/libs/services/common/src/lib/kitchen/kitchenService.ts index 8a0e2af..3177191 100644 --- a/libs/services/common/src/lib/kitchen/kitchenService.ts +++ b/libs/services/common/src/lib/kitchen/kitchenService.ts @@ -1,7 +1,7 @@ import { OrderSummary } from './kitchenServiceWorkflow'; export interface MonsieurAxelMenvoie { - cart: { shortName: string; quantity: number }[]; + cart: { itemId: string, shortName: string; quantity: number }[]; groupId: string; tableNumber: number; } diff --git a/libs/ui/common/src/lib/commandsR/Item.tsx b/libs/ui/common/src/lib/commandsR/Item.tsx index 8e7d4b5..63b08e6 100644 --- a/libs/ui/common/src/lib/commandsR/Item.tsx +++ b/libs/ui/common/src/lib/commandsR/Item.tsx @@ -8,7 +8,7 @@ type ItemProps = { item: MenuItem; isSelected: boolean; tableNumber: number; - handleSelectItem: (shortName: string) => void; + handleSelectItem: (itemId: string, shortName: string) => void; } export function Item(props: Readonly) { @@ -24,7 +24,7 @@ export function Item(props: Readonly) { sx={{ width: "100%", minWidth: 120, height: 100 }} image={props.item.image} title={props.item.shortName} - onClick={() => props.handleSelectItem(props.item.shortName)} + onClick={() => props.handleSelectItem(props.item._id, props.item.shortName)} /> @@ -39,7 +39,7 @@ export function Item(props: Readonly) { max={99} value={count} onChange={(e, value) => { - updateItem(props.tableNumber, props.item.shortName, value as number) + updateItem(props.tableNumber, props.item._id, props.item.shortName, value as number) }} /> )} diff --git a/libs/ui/common/src/lib/commandsR/orderingChoices.tsx b/libs/ui/common/src/lib/commandsR/orderingChoices.tsx index 901330c..aeb7fed 100644 --- a/libs/ui/common/src/lib/commandsR/orderingChoices.tsx +++ b/libs/ui/common/src/lib/commandsR/orderingChoices.tsx @@ -13,6 +13,7 @@ interface OrderingChoicesProps { } export type Cart = { + itemId: string; shortName: string; quantity: number; }[] @@ -62,12 +63,12 @@ export function OrderingChoices(props: Readonly) { ); } - function handleSelectItem(shortName: string) { + function handleSelectItem(itemId: string, shortName: string) { if (currentTableCart.find(element => element.shortName === shortName) !== undefined) { - updateItem(props.tableNumber, shortName, 0); + updateItem(props.tableNumber, itemId, shortName, 0); } else { - updateItem(props.tableNumber, shortName, 1); + updateItem(props.tableNumber, itemId, shortName, 1); } } @@ -93,7 +94,7 @@ export function OrderingChoices(props: Readonly) { {catalog[category].length > 0 ? ( catalog[category].map((item) => { - const isSelected = Boolean(currentTableCart.find(element => element.shortName === item.shortName)); + const isSelected = Boolean(currentTableCart.find(element => element.itemId === item._id)); return ( diff --git a/libs/ui/common/src/lib/commandsR/stores/cart.ts b/libs/ui/common/src/lib/commandsR/stores/cart.ts index 25175bb..cac4788 100644 --- a/libs/ui/common/src/lib/commandsR/stores/cart.ts +++ b/libs/ui/common/src/lib/commandsR/stores/cart.ts @@ -1,25 +1,25 @@ import {create} from "zustand/index"; export interface CartsState { - carts: {[tableNumber: number]: {shortName: string, quantity: number}[]}; - updateItem: (tableNumber: number, shortName: string, quantity: number) => void; + carts: {[tableNumber: number]: {itemId: string, shortName: string, quantity: number}[]}; + updateItem: (tableNumber: number ,itemId: string, shortName: string, quantity: number) => void; resetCart: (tableNumber: number) => void; } export const useCarts = create((set) => ({ carts: {}, - updateItem: (tableNumber, shortName, quantity) => set((state) => { + updateItem: (tableNumber, itemId, shortName, quantity) => set((state) => { const currentCart = state.carts[tableNumber] || []; - const itemIndex = currentCart.findIndex(element => element.shortName === shortName); + const itemIndex = currentCart.findIndex(element => element.itemId === itemId); return { carts: { ...state.carts, [tableNumber]: // Use square brackets to use the variable `tableNumber` as a key - (itemIndex === -1) ? [...currentCart, {shortName, quantity}].filter(element => element.quantity > 0) + (itemIndex === -1) ? [...currentCart, {itemId, shortName, quantity}].filter(element => element.quantity > 0) : currentCart.map(element => { - if (element.shortName === shortName) { + if (element.itemId === itemId) { element.quantity = quantity; } return element; diff --git a/libs/ui/common/src/lib/summary/summary.tsx b/libs/ui/common/src/lib/summary/summary.tsx index 3c5f675..d6451f4 100644 --- a/libs/ui/common/src/lib/summary/summary.tsx +++ b/libs/ui/common/src/lib/summary/summary.tsx @@ -58,7 +58,7 @@ export function Summary(props: Readonly) { currentTableCart.forEach(element => { Object.keys(catalog).forEach(category => { catalog[category].forEach(item => { - if (item.shortName === element.shortName) { + if (item._id === element.itemId) { totalPrice += element.quantity * item.price; } }) @@ -97,16 +97,16 @@ export function Summary(props: Readonly) { {Object.keys(catalog).map((category) => ( - (catalog[category].filter(element => currentTableCart.map(element => element.shortName).includes(element.shortName)) ? + (catalog[category].filter(element => currentTableCart.map(element => element.itemId).includes(element._id)) ? {category} {catalog[category].map((item) => ( - (currentTableCart.map(element => element.shortName).includes(item.shortName)) ? + (currentTableCart.map(element => element.itemId).includes(item._id)) ? - {item.shortName}: {currentTableCart.find(element => element.shortName === item.shortName)?.quantity ?? 0} + {item.shortName}: {currentTableCart.find(element => element.itemId === item._id)?.quantity ?? 0} : "" ))} : '')