Feat(frontend): Toasts

This commit is contained in:
2023-09-04 16:13:20 -04:00
parent 1f7066a2d9
commit 1b9d1ee7d5
4 changed files with 107 additions and 4 deletions

View File

@@ -8,7 +8,7 @@ interface IProps {
const Title = ({ title }: IProps) => (
<>
<Helmet>
<title>Tozo | {title}</title>
<title>Todo | {title}</title>
</Helmet>
<Typography component="h1" variant="h5">
{title}

View File

@@ -0,0 +1,47 @@
import Alert from "@mui/material/Alert";
import Snackbar from "@mui/material/Snackbar";
import React, { useContext, useEffect, useState } from "react";
import { ToastContext, IToast } from "src/ToastContext";
const Toasts = () => {
const { toasts, setToasts } = useContext(ToastContext);
const [open, setOpen] = useState(false);
const [currentToast, setCurrentToast] = useState<IToast | undefined>();
useEffect(() => {
if (!open && toasts.length) {
setCurrentToast(toasts[0]);
setToasts((prev) => prev.slice(1));
setOpen(true);
}
}, [open, setCurrentToast, setOpen, setToasts, toasts]);
const onClose = (event?: React.SyntheticEvent | Event, reason?: string) => {
if (reason !== "clickaway") {
setOpen(false);
}
};
return (
<Snackbar
anchorOrigin={{
horizontal: "center",
vertical: "top",
}}
autoHideDuration={6000}
key={currentToast?.key}
onClose={onClose}
open={open}
TransitionProps={{
onExited: () => setCurrentToast(undefined),
}}
>
<Alert onClose={onClose} severity={currentToast?.category}>
{currentToast?.message}
</Alert>
</Snackbar>
);
};
export default Toasts;