-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend-workflow): add state management to landingPage.tsx
- Loading branch information
Showing
6 changed files
with
114 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
libs/ui/common/src/lib/tables/stores/currentSelectedGroup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}}), | ||
})); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: []}), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}), | ||
), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |