Skip to content

Commit

Permalink
fix: Mister apoorva want the itemId in addition of the shortname beca…
Browse files Browse the repository at this point in the history
…use of shitty backend
  • Loading branch information
Tsukoyachi committed Sep 28, 2024
1 parent 0e11bfc commit 52b247a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion libs/services/common/src/lib/kitchen/kitchenService.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions libs/ui/common/src/lib/commandsR/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemProps>) {
Expand All @@ -24,7 +24,7 @@ export function Item(props: Readonly<ItemProps>) {
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)}
/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">
Expand All @@ -39,7 +39,7 @@ export function Item(props: Readonly<ItemProps>) {
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)
}}
/>
)}
Expand Down
9 changes: 5 additions & 4 deletions libs/ui/common/src/lib/commandsR/orderingChoices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface OrderingChoicesProps {
}

export type Cart = {
itemId: string;
shortName: string;
quantity: number;
}[]
Expand Down Expand Up @@ -62,12 +63,12 @@ export function OrderingChoices(props: Readonly<OrderingChoicesProps>) {
);
}

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);
}
}

Expand All @@ -93,7 +94,7 @@ export function OrderingChoices(props: Readonly<OrderingChoicesProps>) {
<Box sx={{ display: 'flex', gap: '8px', flexWrap: 'wrap' }}>
{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 (
<Box key={item._id} sx={{ display: 'inline', flexDirection: 'column', alignItems: 'center' }}>
Expand Down
12 changes: 6 additions & 6 deletions libs/ui/common/src/lib/commandsR/stores/cart.ts
Original file line number Diff line number Diff line change
@@ -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<CartsState>((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;
Expand Down
8 changes: 4 additions & 4 deletions libs/ui/common/src/lib/summary/summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function Summary(props: Readonly<SummaryProps>) {
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;
}
})
Expand Down Expand Up @@ -97,16 +97,16 @@ export function Summary(props: Readonly<SummaryProps>) {

<Box width='90%' marginLeft='5%' marginTop="7%" bgcolor='#FFFFFF' height="62vh">
{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)) ?
<Box key={category}>
<Typography fontSize="4.5vw"
fontWeight="bold"
style={{ color: 'black' }}
variant='h3'>{category}</Typography>
{catalog[category].map((item) => (
(currentTableCart.map(element => element.shortName).includes(item.shortName)) ?
(currentTableCart.map(element => element.itemId).includes(item._id)) ?
<Typography key={item._id} marginLeft={'30px'} style={{ color: 'black' }} fontSize="3vw">
{item.shortName}: {currentTableCart.find(element => element.shortName === item.shortName)?.quantity ?? 0}
{item.shortName}: {currentTableCart.find(element => element.itemId === item._id)?.quantity ?? 0}
</Typography> : ""
))}
</Box> : '')
Expand Down

0 comments on commit 52b247a

Please sign in to comment.