Skip to content

Commit

Permalink
Merge branch 'master' into client-libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Apoorva64 committed Sep 23, 2024
2 parents 2449b57 + 9b74c7d commit c299669
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 136 deletions.
14 changes: 9 additions & 5 deletions libs/ui/common/src/lib/commandsR/commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NavBar from '../utils/navbar';
import Orders from '../orders/orders';
import BackButton from '../utils/backButton';
import OrderingChoices from './orederingChoices';
import { setSelectedTableById, tablesData } from '../utils/tableUtils';
import { setSelectedTableById, tablesMenu } from '../utils/tableUtils';
//SpeedDial imports
import SpeedDialIcon from '@mui/material/SpeedDialIcon';
import SpeedDialAction from '@mui/material/SpeedDialAction';
Expand All @@ -13,11 +13,14 @@ import GroupsIcon from '@mui/icons-material/Groups';
import CloseIcon from '@mui/icons-material/Close';
import DollarIcon from '@mui/icons-material/AttachMoney';
import { useNavigate, useParams } from 'react-router-dom';
import { useTableSummary } from './stores/tableSummary';
import Summary from '../summary/summary';

export function Commands() {
const navigate = useNavigate();
const [selectedTable, setSelectedTable] = useState(tablesData[1]);
const { groupId } = useParams();
const {groupId} = useParams();
const [selectedTable, setSelectedTable] = useState(tablesMenu[0]);
const haveCurrentCommand = useTableSummary(state=>state.tables).length > 0;

const speedDialActions = [
{ icon: <TableRestaurantIcon />, name: 'Table Payment', operation: onClickTableBilling },
Expand Down Expand Up @@ -53,7 +56,7 @@ export function Commands() {
<NavBar
groupId={groupId ?? ''}
setSelectedTable={(tableId: number) =>
setSelectedTableById(tablesData.map(element => element.id), tableId, setSelectedTable)
setSelectedTableById(tablesMenu, tableId, setSelectedTable)
}
setSelectedTableParentFunction={setSelectedTable}
/>
Expand All @@ -76,7 +79,8 @@ export function Commands() {
<Box sx={{ flex: 1, display: 'flex', flexDirection: 'column' }}>
<BackButton onClick={onClickBackButton} color={'black'} top={20} left={150}></BackButton>
{selectedTable && <OrderingChoices selectedTable={selectedTable} />}
<Orders></Orders>
{haveCurrentCommand && <Summary></Summary>}
{!haveCurrentCommand && <Orders></Orders>}
</Box>
</Box>
</div>
Expand Down
36 changes: 36 additions & 0 deletions libs/ui/common/src/lib/commandsR/orederingChoices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import React, { useState } from 'react';
import { Box, Button, Typography } from '@mui/material';
import './orderingChoices.css';
import { useCurrentSelectedOrder } from './stores/currentSelectedOrder';
import { useTableSummary } from './stores/tableSummary';
import { classicMenu } from '../utils/tableUtils';

export const OrderingChoices = ({ selectedTable }) => {
const { orders, id: tableId } = selectedTable;
const { setOrder } = useCurrentSelectedOrder();

const [selectedOrders, setSelectedOrders] = useState({});
const addOrUpdateOrder = useTableSummary(state=>state.addOrUpdateOrder);
const decreaseOrderQuantity = useTableSummary(state=>state.decreaseOrderQuantity);
const tables = useTableSummary(state=>state.tables);
console.log(tables);

const handleSelectOrder = (category, index) => {

setSelectedOrders(prev => {
const isSelected = prev[tableId]?.[category]?.[index];

Expand Down Expand Up @@ -43,6 +50,21 @@ export const OrderingChoices = ({ selectedTable }) => {
const newCount = currentCount + 1;

setOrder(category, index, newCount);
const order = {
category: category,
name: classicMenu?.[category]?.[index],
quantity: 1,
price: 2.5,
}
addOrUpdateOrder(tableId, order);

//const [tableSummaryContent, arrayId] = getTableIfExist(summaryContent);
//Add to orders

//summaryContent[arrayId] = tableSummaryContent;
//setSummaryContent(summaryContent);



return {
...prev,
Expand All @@ -63,6 +85,13 @@ export const OrderingChoices = ({ selectedTable }) => {
const newCount = Math.max(0, currentCount - 1);

setOrder(category, index, newCount);
const order = {
category: category,
name: classicMenu?.[category]?.[index],
quantity: 1,
price: 2.5,
}
decreaseOrderQuantity(tableId, order)

return {
...prev,
Expand Down Expand Up @@ -146,3 +175,10 @@ export const OrderingChoices = ({ selectedTable }) => {
};

export default OrderingChoices;



function getTableIfExist(summaryContent): [any, any] {
throw new Error('Function not implemented.');
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { create } from "zustand";

interface CurrentSelectedOrderState {
export interface CurrentSelectedOrderState {
orders: { [category: string]: { [orderId: number]: number } };
setOrder: (category: string, orderId: number, count: number) => void;
resetOrders: () => void;
}


export const useCurrentSelectedOrder = create<CurrentSelectedOrderState>((set) => ({
orders: {},
setOrder: (category, orderId, count) => set((state) => {
Expand Down
126 changes: 126 additions & 0 deletions libs/ui/common/src/lib/commandsR/stores/tableSummary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { TableView } from "@mui/icons-material";
import { create } from "zustand";


// Represents an order for a specific item
interface Order {
category: string;
name: string;
quantity: number;
price: number;
}

// Represents a table with its orders
interface Table {
id: number; // This is the table number
orders: Order[];
}

export interface TableState {
tables: Table[];
addOrUpdateOrder: (tableId: number, newOrder: Order) => void;
decreaseOrderQuantity: (tableId: number, newOrder: Order) => void;
}

export const useTableSummary = create<TableState>((set) => ({
tables: [],
addOrUpdateOrder: (tableId, newOrder) =>
set((state) => ({
tables: updateTable(state, tableId, newOrder)
})),
decreaseOrderQuantity: (tableId, newOrder) =>
set((state) => ({
tables: decreaseItem(state, tableId, newOrder)
}))
}));

const updateTable = (state: TableState, tableId: number, newOrder: Order) => {
let tableFound = false;
for(const tableKey in state.tables){
const table = state.tables[tableKey]
if(table.id === tableId){
console.log(tableId)
tableFound = true;
const existingOrder = table.orders.find(
(order) =>
order.name === newOrder.name &&
order.category === newOrder.category
);
if (existingOrder) {

return mergeTables(state.tables, {
...table,
orders: table.orders.map((order) =>
order === existingOrder
? { ...order, quantity: order.quantity + newOrder.quantity }
: order
)
});
} else {
// Add new order if not found
return mergeTables(state.tables, {
...table,
orders: [...table.orders, newOrder]
});
}
}
}
if(tableFound==false){
return mergeTables(state.tables, {
"id": tableId,
"orders":[
newOrder
]
})
}

}

const decreaseItem = (state: TableState, tableId: number, newOrder: Order) => {
for(const tableKey in state.tables){
const table = state.tables[tableKey]
if (table.id === tableId) {
let newTable = {
...table,
orders: table.orders
.map((order) => {
if (order.name === newOrder.name && order.category === newOrder.category) {
// Decrease the quantity or remove the order if the quantity is zero or below
const updatedQuantity = order.quantity - newOrder.quantity;
if (updatedQuantity > 0) {
return { ...order, quantity: updatedQuantity };
}
return null; // Remove order if quantity becomes zero or less
}
return order;
})
.filter(Boolean) as Order[] // Remove null values (orders with quantity 0)
};
return mergeTables(state.tables, newTable);
}
}
}



function mergeTables(tables: Table[], table: Table): any {
// Check if the table with the same id exists
const tableIndex = tables.findIndex(t => t.id === table.id);
if (table.orders.length <1){
return [
...tables.slice(0, tableIndex),
...tables.slice(tableIndex + 1)
];
}
if (tableIndex !== -1) {
// Replace the existing table with the new one
return [
...tables.slice(0, tableIndex),
table,
...tables.slice(tableIndex + 1)
];
} else {
// Append the new table to the list
return [...tables, table];
}
}
12 changes: 7 additions & 5 deletions libs/ui/common/src/lib/orders/commandNumber.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Button, Typography, Box } from "@mui/material";
import { Button } from "@mui/material";
import { useState } from 'react';

const CommandNumber = ({ number, status }) => {
const backgroundColor = status === 'completed' ? 'green' : 'orange';
const CommandNumber = ({ number, status, isServed, isSelected, onClick }) => {
const backgroundColor = isSelected ? 'blue' : (status === 'completed' ? 'green' : 'orange'); // Mettre en surbrillance si sélectionné
return (
<Box
<Button
sx={{
width: 50,
height: 50,
Expand All @@ -18,9 +18,11 @@ const CommandNumber = ({ number, status }) => {
margin: 1,
boxShadow: '0px 4px 8px rgba(0,0,0,0.2)',
}}
onClick={onClick}
>
{number}
</Box>
</Button>
);
};

export default CommandNumber;
Loading

0 comments on commit c299669

Please sign in to comment.