Skip to content

Commit

Permalink
make Modal reusable by passing props
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaBin committed Sep 8, 2024
1 parent b7193bd commit 09d1f89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export function List({ data, userId }) {
const [showModal, setShowModal] = useState(false);

const dataEmpty = userId && !data.length;
const message = {
header: 'Tip:',
promptMSG: 'Your shopping list is empty. Go to Manage List to start!',
btnText: 'Go to Manage List',
};

useEffect(() => {
if (dataEmpty) {
Expand Down Expand Up @@ -35,7 +40,9 @@ export function List({ data, userId }) {
<p>
Hello from the <code>/list</code> page!
</p>
{showModal && dataEmpty && <BasicModal dataEmpty={dataEmpty} />}
{showModal && dataEmpty && (
<BasicModal dataEmpty={dataEmpty} message={message} />
)}

<form onSubmit={clearInput}>
<label htmlFor="item-name"> Item name:</label>
Expand Down
8 changes: 4 additions & 4 deletions src/views/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const style = (theme) => ({
fontSize: 20,
});

export default function BasicModal({ dataEmpty }) {
export default function BasicModal({ dataEmpty, message }) {
const [open, setOpen] = React.useState(dataEmpty);
const handleClose = () => setOpen(false);
const navigate = useNavigate();
Expand All @@ -34,10 +34,10 @@ export default function BasicModal({ dataEmpty }) {
>
<Box sx={style(theme)}>
<Typography id="modal-modal-title" variant="h4" component="h2">
Tip:
{message.header}
</Typography>
<Typography id="modal-modal-description" sx={{ mt: 2 }} variant="h5">
Your shopping list is empty. Go to Manage List to start!
{message.promptMSG}
</Typography>
<Button
sx={{
Expand All @@ -48,7 +48,7 @@ export default function BasicModal({ dataEmpty }) {
size="large"
onClick={() => navigate('/manage-list')}
>
Go to Manage List
{message.btnText}
</Button>
</Box>
</Modal>
Expand Down

0 comments on commit 09d1f89

Please sign in to comment.