Skip to content

Commit

Permalink
feat(frontend-workflow): add state management to landingPage.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Apoorva64 committed Sep 22, 2024
1 parent ee60eb5 commit a57f971
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 70 deletions.
7 changes: 4 additions & 3 deletions libs/ui/common/src/lib/tables/landingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import TableSquare from './tableSquare';
import {Button, Typography, Box, Grid2 as Grid} from "@mui/material";
import * as React from 'react';
import {useState} from 'react';
import {useTables} from "./stores/tables";

export function LandingPage() {
const [totalPeople, setTotalPeople] = useState(0);
const navigate = useNavigate();

const tables = useTables(state => state.tables);
const validateTables = () => {
console.log({totalPeople})
navigate("/offers")
Expand All @@ -19,9 +20,9 @@ export function LandingPage() {
Tables
</Typography>
<Grid container spacing={4} justifyContent={"center"} alignItems={"center"} marginTop="60px" maxHeight='60vh' overflow='auto' maxWidth={800} >
{Array.from(Array(15)).map((_, index) => (
{tables.map((value, index) => (
<Grid key={index} >
<TableSquare tableNumber={index} totalPeople={totalPeople} setTotalPeople={setTotalPeople}></TableSquare>
<TableSquare tableNumber={value.id}></TableSquare>
</Grid>
))}
</Grid>
Expand Down
44 changes: 9 additions & 35 deletions libs/ui/common/src/lib/tables/nbPeopleSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@

import {ReactNode, useState} from 'react';
import * as React from 'react';
import {
Unstable_NumberInput as BaseNumberInput,
NumberInputProps,
} from '@mui/base/Unstable_NumberInput';
import { styled } from '@mui/system';
import {NumberInputProps, Unstable_NumberInput as BaseNumberInput,} from '@mui/base/Unstable_NumberInput';
import {styled} from '@mui/system';
import RemoveIcon from '@mui/icons-material/Remove';
import AddIcon from '@mui/icons-material/Add';

Expand All @@ -23,14 +18,14 @@ const NumberInput = React.forwardRef(function CustomNumberInput(
} as any}
slotProps={{
incrementButton: {
children: <AddIcon fontSize="small" />,
children: <AddIcon fontSize="small"/>,
className: 'increment',
},
decrementButton: {
children: <RemoveIcon fontSize="small" />,
children: <RemoveIcon fontSize="small"/>,
},
}}
{...props }
{...props}
ref={ref}
/>
);
Expand Down Expand Up @@ -61,7 +56,7 @@ const grey = {
};

const StyledInputRoot = styled('div')(
({ theme }) => `
({theme}) => `
font-family: 'IBM Plex Sans', sans-serif;
font-weight: 400;
color: ${theme.palette.mode === 'dark' ? grey[300] : grey[500]};
Expand All @@ -73,7 +68,7 @@ const StyledInputRoot = styled('div')(
);

const StyledInput = styled('input')(
({ theme }) => `
({theme}) => `
font-size: 0.875rem;
font-family: inherit;
font-weight: 400;
Expand Down Expand Up @@ -108,7 +103,7 @@ const StyledInput = styled('input')(
);

const StyledButton = styled('button')(
({ theme }) => `
({theme}) => `
font-family: 'IBM Plex Sans', sans-serif;
font-size: 0.875rem;
box-sizing: border-box;
Expand Down Expand Up @@ -144,26 +139,5 @@ const StyledButton = styled('button')(
}
`,
);
export function NbPeopleSelector({totalPeople, setTotalPeople}) {
const [nbPeople, setNbPeople] = useState(0)

const addPerson = () => {
setNbPeople(nbPeople + 1)
setTotalPeople(totalPeople + 1)
};

const removePerson = () => {
if (nbPeople > 0){
setNbPeople(nbPeople - 1)
}
if (totalPeople > 0){
setTotalPeople(totalPeople - 1)
}
};

return (
<NumberInput aria-label="Table Input" min={1} max={99} defaultValue={1} />
);
}

export default NbPeopleSelector;
export default NumberInput;
25 changes: 25 additions & 0 deletions libs/ui/common/src/lib/tables/stores/currentSelectedGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {create} from "zustand/index";

interface CurrentSelectedGroupState {
tables: { [tableId: number]: number };
setTable: (tableId: number, numberOfPeople: number) => void;
removeTable: (tableId: number) => void;
resetTables: () => void;
}

export const useCurrentSelectedGroup = create<CurrentSelectedGroupState>((set) => ({
tables: {},
setTable: (tableId, numberOfPeople) => set((state) => {
const tables = {...state.tables};
tables[tableId] = numberOfPeople;
return {tables};
}),
removeTable: (tableId) => set((state) => {
const tables = {...state.tables};
delete tables[tableId];
return {tables};
}
),
resetTables: () => set({tables: {}}),
}));

18 changes: 18 additions & 0 deletions libs/ui/common/src/lib/tables/stores/groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {create} from "zustand/index";

interface GroupsState {
groups: { tables: { [tableId: number]: number }, offer: string }[];
addGroup: (tables: { [tableId: number]: number }, offer: string) => void;
resetGroups: () => void;
}

export const useGroups = create<GroupsState>((set) => ({
groups: [],
addGroup: (tables, offer) => set((state) => ({
groups: [...state.groups, {
tables,
offer
}]
})),
resetGroups: () => set({groups: []}),
}));
12 changes: 12 additions & 0 deletions libs/ui/common/src/lib/tables/stores/tables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {create} from "zustand/index";

interface TablesState {
tables: { id: number }[];
}

export const useTables = create<TablesState>((set, get) => ({
tables: Array.from(Array(15)).map((_, i) => ({
id: i + 1,
}),
),
}));
78 changes: 46 additions & 32 deletions libs/ui/common/src/lib/tables/tableSquare.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
import NbPeopleSelector from './nbPeopleSelector';
import { purple, red } from '@mui/material/colors';
import NumberInput from './nbPeopleSelector';

import { Button, Typography, Box } from "@mui/material";
import {Box, Button, Typography} from "@mui/material";
import * as React from 'react';
import {useState} from 'react';
import {styled} from "@mui/system";
import {useCurrentSelectedGroup} from "./stores/currentSelectedGroup";

export function TableSquare({tableNumber, totalPeople, setTotalPeople}) {
const [showPeopleSelector, setShowPeopleSelector] = useState(false)
const showHidePeopleSelector = () => {
showPeopleSelector ? setShowPeopleSelector(false) : setShowPeopleSelector(true);
export function TableSquare({tableNumber}) {
const [showPeopleSelector, setShowPeopleSelector] = useState(false)
const currentTablePeople = useCurrentSelectedGroup(state => state.tables[tableNumber]);
const setTable = useCurrentSelectedGroup(state => state.setTable);
const removeTable = useCurrentSelectedGroup(state => state.removeTable);
const showHidePeopleSelector = () => {
setShowPeopleSelector(!showPeopleSelector)
if (!showPeopleSelector) {
setTable(tableNumber, 1)
} else {
removeTable(tableNumber)
}
}
return (
<Box width="fit-content">
<Button onClick={showHidePeopleSelector}
variant="contained"
sx={{
aspectRatio: 1,

return (
<Box width="fit-content">
<Button onClick={showHidePeopleSelector}
variant="contained"
sx={{aspectRatio: 1,
}}
color={showPeopleSelector ? 'success' : 'primary'}>
<Box
display="flex"
alignItems="center"
justifyContent="center"
width={120} // width of the square box
height={100} // height of the square box
>
<Typography variant="h1" component="h2" fontWeight="bold">
{tableNumber}
</Typography>
</Box>
</Button>
<Box margin="5%"></Box>
{showPeopleSelector ? <NumberInput aria-label="Table Input"
min={1}
max={99}
value={currentTablePeople}
onChange={(e, value) => setTable(tableNumber, value as number)}

}}
color={showPeopleSelector? 'success':'primary' }>
<Box
display="flex"
alignItems="center"
justifyContent="center"
width={120} // width of the square box
height={100} // height of the square box
>
<Typography variant="h1" component="h2" fontWeight="bold" >
{tableNumber}
</Typography>
</Box>
</Button>
<Box margin="5%"></Box>
{ showPeopleSelector ? <NbPeopleSelector totalPeople={totalPeople} setTotalPeople={setTotalPeople}/> : <></> }
<Box margin="2%"></Box>
/> : <></>}
<Box margin="2%"></Box>

</Box>
);
</Box>
);
}

export default TableSquare;

0 comments on commit a57f971

Please sign in to comment.