-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from bcgov/zustand-setup
Setup Modal provider with zustand
- Loading branch information
Showing
9 changed files
with
158 additions
and
35 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from "react"; | ||
import { useModal } from "./modalStore"; | ||
import { Box, Modal } from "@mui/material"; | ||
|
||
const ModalProvider: React.FC = () => { | ||
const { modalContent, setClose, isOpen } = useModal(); | ||
|
||
return ( | ||
<Modal open={isOpen} onClose={setClose}> | ||
<Box>{modalContent}</Box> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default ModalProvider; |
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,55 @@ | ||
import { Button, Grid, Typography } from "@mui/material"; | ||
import { useModal } from "./modalStore"; | ||
import { modalStyle } from "./constants"; | ||
|
||
interface UpdateModalProps { | ||
header: string; | ||
subText: { text: string; bold?: boolean }[]; | ||
} | ||
|
||
const UpdateModal = ({ header, subText }: UpdateModalProps) => { | ||
const { setClose } = useModal(); | ||
|
||
return ( | ||
<Grid | ||
container | ||
direction="row" | ||
justifyContent="flex-start" | ||
alignItems="space-between" | ||
rowSpacing={2} | ||
sx={{ ...modalStyle }} | ||
> | ||
<Grid container direction="row" item xs={12}> | ||
<Grid item xs={12}> | ||
<Typography variant="h1" sx={{ mb: 2 }}> | ||
{header} | ||
</Typography> | ||
</Grid> | ||
</Grid> | ||
<Grid container direction="row" item xs={12}> | ||
{subText.map((subtext) => ( | ||
<Grid item xs={12}> | ||
<Typography variant="body1" sx={{ mb: 1 }}> | ||
{subtext.text} | ||
</Typography> | ||
</Grid> | ||
))} | ||
<Grid | ||
item | ||
container | ||
direction={{ xs: "column", sm: "row" }} | ||
xs={12} | ||
justifyContent="flex-end" | ||
spacing={1} | ||
sx={{ mt: "1em" }} | ||
> | ||
<Button onClick={setClose} sx={{ m: 1 }}> | ||
Close | ||
</Button> | ||
</Grid> | ||
</Grid> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default UpdateModal; |
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 { theme } from "@/styles/theme"; | ||
|
||
export const modalStyle = { | ||
position: 'absolute', | ||
top: '50%', | ||
left: '48%', | ||
transform: 'translate(-50%, -50%)', | ||
maxWidth: 'min(95vw, 700px)', | ||
maxHeight: '95vh', | ||
bgcolor: 'background.paper', | ||
boxShadow: 10, | ||
pt: 2, | ||
px: 4, | ||
pb: 3, | ||
m: 1, | ||
overflowY: 'scroll', | ||
color: theme.palette.text.primary, | ||
}; |
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,45 @@ | ||
import {create} from 'zustand' | ||
import { User } from "@/models/User"; | ||
import { Plan } from '@/models/Plan'; | ||
|
||
|
||
// Define the ModalData type | ||
export type ModalData = { | ||
user?: User | ||
plan?: Plan | ||
} | ||
|
||
// Define the store state and actions | ||
interface ModalStore { | ||
data: ModalData | ||
isOpen: boolean | ||
modalContent: React.ReactNode | null | ||
setOpen: (modal: React.ReactNode, fetchData?: () => Promise<ModalData>) => Promise<void> | ||
setClose: () => void | ||
} | ||
|
||
// Create the Zustand store | ||
export const useModal = create<ModalStore>((set) => ({ | ||
data: {}, | ||
isOpen: false, | ||
modalContent: null, | ||
|
||
setOpen: async (modal, fetchData) => { | ||
if (modal) { | ||
const fetchedData = fetchData ? await fetchData() : {} | ||
set((state) => ({ | ||
data: { ...state.data, ...fetchedData }, | ||
modalContent: modal, | ||
isOpen: true, | ||
})) | ||
} | ||
}, | ||
|
||
setClose: () => { | ||
set({ | ||
isOpen: false, | ||
data: {}, | ||
modalContent: null, | ||
}) | ||
}, | ||
})) |
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