Files
Checkmate/Client/src/Pages/Maintenance/CreateMaintenanceWindow/index.jsx
T

300 lines
8.0 KiB
React
Raw Normal View History

2024-08-03 09:40:08 +03:30
import { Box, Stack, Typography } from "@mui/material";
import "./index.css";
import { useState } from "react";
2024-08-03 09:40:08 +03:30
import Button from "../../../Components/Button";
2024-08-19 13:23:17 -04:00
import Back from "../../../assets/icons/left-arrow-long.svg?react";
2024-08-03 09:40:08 +03:30
import Select from "../../../Components/Inputs/Select";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import dayjs from "dayjs";
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker";
import Field from "../../../Components/Inputs/Field";
import { maintenanceWindowValidation } from "../../../Validation/validation";
import { logger } from "../../../Utils/Logger";
import { createToast } from "../../../Utils/toastUtils";
2024-08-03 09:40:08 +03:30
const directory = {
title: "Create a maintenance window",
description: "Your pings won’t be sent in this time frame.",
};
const repeatOptions = [
{ _id: 1, name: "Don't repeat" },
{ _id: 2, name: "Repeat daily" },
{ _id: 3, name: "Repeat weekly" },
];
const configOptionTitle = (title, description) => {
return (
<Stack width="40%" gap={1}>
<Typography
style={{
fontWeight: 600,
fontSize: "var(--env-var-font-size-medium)",
}}
>
{title}
</Typography>
{description && (
<Typography
style={{
fontSize: "var(--env-var-font-size-small)",
}}
>
{description}
</Typography>
)}
</Stack>
);
};
2024-08-08 09:13:46 +03:30
const durationOptions = [
2024-08-07 22:46:48 +03:30
{ _id: "minutes", name: "minutes" },
{ _id: "hours", name: "hours" },
{ _id: "days", name: "days" },
];
2024-08-03 09:40:08 +03:30
const CreateNewMaintenanceWindow = () => {
const [values, setValues] = useState({
repeat: 1,
date: dayjs(),
startTime: dayjs(),
2024-08-08 09:13:46 +03:30
duration: "60",
2024-08-06 18:41:00 +03:30
unit: "minutes",
displayName: "",
AddMonitors: "",
2024-08-03 09:40:08 +03:30
});
const [errors, setErrors] = useState({});
2024-08-03 09:40:08 +03:30
const handleChange = (event, name) => {
const { value } = event.target;
setValues((prev) => ({
...prev,
[name]: value,
}));
};
const handleSubmit = async () => {
const data = {
repeat: values.repeat,
date: values.date.format("YYYY-MM-DD"),
startTime: values.startTime.format("HH:mm"),
duration: values.duration,
unit: values.unit,
displayName: values.displayName,
addMonitors: values.AddMonitors,
};
const { error } = maintenanceWindowValidation.validate(data, {
abortEarly: false,
});
logger.log("error: ", error);
2024-08-22 10:01:18 +03:30
if (!error || error.details.length === 0) {
setErrors({});
} else {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
setErrors(newErrors);
createToast({
body:
error.details && error.details.length > 0
? error.details[0].message
: "Error validating data",
});
logger.error("Validation errors:", error.details);
}
logger.log("Submitting data: ", data);
};
2024-08-03 09:40:08 +03:30
const configOptions = [
{
title: "Repeat",
component: (
<Select
onChange={(e) => handleChange(e, "repeat")}
id="repeat-mode"
items={repeatOptions}
value={values.repeat}
/>
),
},
{
title: "Date",
component: (
<LocalizationProvider
className="date-localization-provider"
dateAdapter={AdapterDayjs}
>
<DatePicker
className="date-picker"
defaultValue={values.date}
onChange={(date) =>
handleChange({ target: { value: date } }, "date")
}
/>
2024-08-03 09:40:08 +03:30
</LocalizationProvider>
),
},
{
title: "Start time",
component: (
<LocalizationProvider
className="time-localization-provider"
dateAdapter={AdapterDayjs}
>
<MobileTimePicker
className="time-picker"
defaultValue={values.startTime}
onChange={(time) =>
handleChange({ target: { value: time } }, "startTime")
}
/>
2024-08-03 09:40:08 +03:30
</LocalizationProvider>
),
},
{
title: "Duration",
component: (
<Stack className="duration-config" gap={2} direction="row">
<Field
id="duration-value"
placeholder="60"
2024-08-08 09:13:46 +03:30
onChange={(e) => handleChange(e, "duration")}
value={values.duration}
2024-08-21 22:50:42 +03:30
error={errors.duration}
2024-08-22 10:01:18 +03:30
type="number"
/>
<Select
2024-08-06 18:41:00 +03:30
onChange={(e) => handleChange(e, "unit")}
id="duration-unit"
2024-08-08 09:13:46 +03:30
items={durationOptions}
2024-08-06 18:41:00 +03:30
value={values.unit}
/>
</Stack>
),
},
{
title: "Display name",
component: (
<Field
id="display-name"
placeholder="Maintanence at __ : __ for ___ minutes"
value={values.displayName}
onChange={(e) => handleChange(e, "displayName")}
error={errors.displayName}
/>
),
},
{
title: "Add monitors",
component: (
2024-08-06 18:22:54 +03:30
<Stack
className="add-monitors-fields"
sx={{ width: "60%", maxWidth: "380px" }}
gap={2}
>
<Field
id="add-monitors"
placeholder="Start typing to search for current monitors"
value={values.AddMonitors}
onChange={(e) => handleChange(e, "AddMonitors")}
2024-08-21 22:50:42 +03:30
error={errors.addMonitors}
/>
<Typography
sx={{
2024-08-05 21:41:28 +03:30
width: "fit-content",
fontSize: "var(--env-var-font-size-small)",
borderBottom: "1px dashed var(--env-var-color-3)",
2024-08-05 21:25:21 +03:30
paddingBottom: "4px",
}}
>
Add all monitors to this maintenance window
</Typography>
</Stack>
),
},
2024-08-03 09:40:08 +03:30
];
return (
<div className="create-maintenance-window">
<Stack gap={3}>
<Button
id="btn-back"
sx={{
width: "100px",
height: "30px",
gap: "10px",
backgroundColor: "var(--env-var-color-32)",
color: "var(--env-var-color-5)",
}}
label="Back"
level="tertiary"
img={<Back />}
/>
<Box>
<Typography
sx={{
fontSize: "var(--env-var-font-size-large)",
fontWeight: "600",
color: "var(--env-var-color-5)",
}}
>
{directory.title}
</Typography>
<Typography sx={{ fontSize: "var(--env-var-font-size-medium)" }}>
{directory.description}
</Typography>
</Box>
<Stack
className="maintenance-options"
2024-08-03 09:40:08 +03:30
gap={5}
paddingY={4}
paddingX={8}
paddingBottom={10}
2024-08-03 09:40:08 +03:30
sx={{
border: "1px solid var(--env-var-color-16)",
borderRadius: "var(--env-var-radius-1)",
2024-08-08 09:13:46 +03:30
backgroundColor: "var(--env-var-color-0)",
2024-08-03 09:40:08 +03:30
}}
>
{configOptions.map((item, index) => (
<Stack key={index} display="-webkit-inline-box">
{item.title && configOptionTitle(item.title, item.description)}
2024-08-03 09:40:08 +03:30
{item.component && item.component}
</Stack>
))}
</Stack>
<Stack justifyContent="end" direction="row" marginTop={3}>
2024-08-09 11:09:28 +03:30
<Button
sx={{
"&:hover": {
backgroundColor: "transparent",
boxShadow: "none",
},
}}
level="tertiary"
label="Cancel"
/>
<Button
sx={{
"&:hover": {
backgroundColor: "#1570EF",
boxShadow: "none",
},
}}
level="primary"
label="Create"
onClick={handleSubmit}
2024-08-09 11:09:28 +03:30
/>
</Stack>
2024-08-03 09:40:08 +03:30
</Stack>
</div>
);
};
export default CreateNewMaintenanceWindow;