Files
Checkmate/client/src/Pages/Notifications/create/index.jsx
T

249 lines
6.4 KiB
React
Raw Normal View History

2025-06-09 12:57:30 +08:00
// Components
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import Breadcrumbs from "../../../Components/Breadcrumbs";
import Button from "@mui/material/Button";
import ConfigBox from "../../../Components/ConfigBox";
import Box from "@mui/material/Box";
import Select from "../../../Components/Inputs/Select";
import TextInput from "../../../Components/Inputs/TextInput";
// Utils
import { useState } from "react";
import { useTheme } from "@emotion/react";
2025-06-10 11:13:19 +08:00
import {
useCreateNotification,
useGetNotificationById,
useEditNotification,
2025-06-10 12:07:31 +08:00
useTestNotification,
2025-07-02 17:14:39 +01:00
useDeleteNotification,
2025-06-10 11:13:19 +08:00
} from "../../../Hooks/useNotifications";
2025-06-18 14:42:26 +08:00
import { notificationValidation } from "../../../Validation/validation";
2025-06-09 12:57:30 +08:00
import { createToast } from "../../../Utils/toastUtils";
import { useTranslation } from "react-i18next";
2025-07-02 17:14:39 +01:00
import { useParams, useNavigate } from "react-router-dom";
2025-06-18 14:42:26 +08:00
import {
NOTIFICATION_TYPES,
TITLE_MAP,
DESCRIPTION_MAP,
LABEL_MAP,
PLACEHOLDER_MAP,
} from "../utils";
2025-06-09 12:57:30 +08:00
// Setup
const CreateNotifications = () => {
2025-06-10 11:13:19 +08:00
const { notificationId } = useParams();
2025-07-02 17:14:39 +01:00
const navigate = useNavigate();
2025-06-09 12:57:30 +08:00
const theme = useTheme();
2025-07-04 11:53:25 +01:00
const [createNotification, isCreating] = useCreateNotification();
const [editNotification, isEditing] = useEditNotification();
const [testNotification, isTesting] = useTestNotification();
const [deleteNotification, isDeleting] = useDeleteNotification();
2025-06-10 12:07:31 +08:00
2025-06-09 12:57:30 +08:00
const BREADCRUMBS = [
{ name: "notifications", path: "/notifications" },
2025-07-02 22:32:08 +01:00
{
name: notificationId ? "edit" : "create",
path: notificationId ? `/notifications/${notificationId}` : "/notifications/create",
},
2025-06-09 12:57:30 +08:00
];
// Redux state
// local state
const [notification, setNotification] = useState({
notificationName: "",
address: "",
type: NOTIFICATION_TYPES[0]._id,
});
const [errors, setErrors] = useState({});
const { t } = useTranslation();
2025-07-04 11:53:25 +01:00
const [notificationIsLoading] = useGetNotificationById(
2025-06-10 11:13:19 +08:00
notificationId,
setNotification
);
2025-06-09 12:57:30 +08:00
// handlers
const onSubmit = (e) => {
e.preventDefault();
const form = {
...notification,
type: NOTIFICATION_TYPES.find((type) => type._id === notification.type).value,
};
let error = null;
2025-06-18 14:42:26 +08:00
error = notificationValidation.validate(form, { abortEarly: false }).error;
2025-06-09 12:57:30 +08:00
if (error) {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
2025-06-18 14:42:26 +08:00
console.log(JSON.stringify(newErrors));
console.log(JSON.stringify(form, null, 2));
2025-06-09 12:57:30 +08:00
createToast({ body: "Please check the form for errors." });
setErrors(newErrors);
return;
}
2025-06-10 11:13:19 +08:00
if (notificationId) {
editNotification(notificationId, form);
} else {
createNotification(form);
}
2025-06-09 12:57:30 +08:00
};
const onChange = (e) => {
const { name, value } = e.target;
const newNotification = { ...notification, [name]: value };
2025-06-18 14:42:26 +08:00
const { error } = notificationValidation.extract(name).validate(value);
2025-06-09 12:57:30 +08:00
setErrors((prev) => ({
...prev,
2025-06-18 14:42:26 +08:00
[name]: error?.message,
2025-06-09 12:57:30 +08:00
}));
setNotification(newNotification);
};
2025-06-10 12:07:31 +08:00
const onTestNotification = () => {
const form = {
...notification,
type: NOTIFICATION_TYPES.find((type) => type._id === notification.type).value,
};
let error = null;
2025-06-18 14:42:26 +08:00
error = notificationValidation.validate(form, { abortEarly: false }).error;
2025-06-10 12:07:31 +08:00
if (error) {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
createToast({ body: "Please check the form for errors." });
setErrors(newErrors);
return;
}
testNotification(form);
};
2025-07-02 17:14:39 +01:00
const onDelete = () => {
if (notificationId) {
2025-07-04 11:53:25 +01:00
deleteNotification(notificationId, () => navigate("/notifications"));
2025-07-02 17:14:39 +01:00
}
};
2025-06-18 14:42:26 +08:00
const type = NOTIFICATION_TYPES.find((type) => type._id === notification.type).value;
2025-06-09 12:57:30 +08:00
return (
<Stack gap={theme.spacing(10)}>
<Breadcrumbs list={BREADCRUMBS} />
<Typography variant="h1">{t("createNotifications.title")}</Typography>
<Stack
component="form"
onSubmit={onSubmit}
noValidate
gap={theme.spacing(12)}
mt={theme.spacing(6)}
>
<ConfigBox>
<Box>
<Typography component="h2">
{t("createNotifications.nameSettings.title")}
</Typography>
<Typography component="p">
{t("createNotifications.nameSettings.description")}
</Typography>
</Box>
<Stack gap={theme.spacing(12)}>
<TextInput
label={t("createNotifications.nameSettings.nameLabel")}
name="notificationName"
placeholder={t("createNotifications.nameSettings.namePlaceholder")}
value={notification.notificationName}
onChange={onChange}
error={Boolean(errors.notificationName)}
helperText={errors["notificationName"]}
/>
</Stack>
</ConfigBox>
<ConfigBox>
<Box>
<Typography component="h2">
{t("createNotifications.typeSettings.title")}
</Typography>
<Typography component="p">
{t("createNotifications.typeSettings.description")}
</Typography>
</Box>
<Stack gap={theme.spacing(12)}>
<Select
items={NOTIFICATION_TYPES}
label="Type"
name="type"
value={notification.type}
onChange={onChange}
/>
</Stack>
</ConfigBox>
2025-06-18 14:42:26 +08:00
<ConfigBox>
<Box>
<Typography component="h2">{t(TITLE_MAP[type])}</Typography>
<Typography component="p">{t(DESCRIPTION_MAP[type])}</Typography>
</Box>
<Stack gap={theme.spacing(12)}>
<TextInput
label={t(LABEL_MAP[type])}
name="address"
placeholder={t(PLACEHOLDER_MAP[type])}
value={notification.address}
onChange={onChange}
error={Boolean(errors.address)}
helperText={errors["address"]}
/>
</Stack>
</ConfigBox>
2025-06-09 12:57:30 +08:00
<Stack
direction="row"
justifyContent="flex-end"
2025-06-10 12:07:31 +08:00
spacing={theme.spacing(2)}
2025-06-09 12:57:30 +08:00
>
2025-06-10 12:07:31 +08:00
<Button
2025-06-20 10:08:13 +08:00
loading={isTesting}
2025-06-10 12:07:31 +08:00
variant="contained"
color="secondary"
onClick={onTestNotification}
>
{t("testNotification", "Test notification")}
2025-06-10 12:07:31 +08:00
</Button>
2025-07-02 17:14:39 +01:00
{notificationId && (
<Button
loading={isDeleting}
variant="contained"
color="error"
onClick={onDelete}
>
{t("delete", "Delete")}
2025-07-02 17:14:39 +01:00
</Button>
)}
2025-06-09 12:57:30 +08:00
<Button
2025-06-10 11:13:19 +08:00
loading={isCreating || isEditing || notificationIsLoading}
2025-06-09 12:57:30 +08:00
type="submit"
variant="contained"
color="accent"
>
{t("submit", "Submit")}
2025-06-09 12:57:30 +08:00
</Button>
</Stack>
</Stack>
</Stack>
);
};
export default CreateNotifications;