diff --git a/Client/src/Components/Inputs/Search/index.jsx b/Client/src/Components/Inputs/Search/index.jsx
index fbfd9b00c..268c83572 100644
--- a/Client/src/Components/Inputs/Search/index.jsx
+++ b/Client/src/Components/Inputs/Search/index.jsx
@@ -1,5 +1,12 @@
import PropTypes from "prop-types";
-import { Box, ListItem, Autocomplete, TextField } from "@mui/material";
+import {
+ Box,
+ ListItem,
+ Autocomplete,
+ TextField,
+ Stack,
+ Typography,
+} from "@mui/material";
import { useTheme } from "@emotion/react";
import SearchIcon from "../../../assets/icons/search.svg?react";
@@ -49,6 +56,7 @@ const Search = ({
sx,
multiple = false,
isAdorned = true,
+ error,
}) => {
const theme = useTheme();
@@ -69,24 +77,40 @@ const Search = ({
options={options}
getOptionLabel={(option) => option[filteredBy]}
renderInput={(params) => (
- }),
- }}
- sx={{
- "& fieldset": {
- borderColor: theme.palette.border.light,
- borderRadius: theme.shape.borderRadius,
- },
- "& .MuiOutlinedInput-root:hover:not(:has(input:focus)):not(:has(textarea:focus)) fieldset":
- {
+
+ }),
+ }}
+ sx={{
+ "& fieldset": {
borderColor: theme.palette.border.light,
+ borderRadius: theme.shape.borderRadius,
},
- }}
- />
+ "& .MuiOutlinedInput-root:hover:not(:has(input:focus)):not(:has(textarea:focus)) fieldset":
+ {
+ borderColor: theme.palette.border.light,
+ },
+ }}
+ />
+ {error && (
+
+ {error}
+
+ )}
+
)}
filterOptions={(options, { inputValue }) => {
const filtered = options.filter((option) =>
@@ -159,6 +183,7 @@ Search.propTypes = {
handleChange: PropTypes.func,
isAdorned: PropTypes.bool,
sx: PropTypes.object,
+ error: PropTypes.string,
};
export default Search;
diff --git a/Client/src/Pages/Maintenance/CreateMaintenance/index.jsx b/Client/src/Pages/Maintenance/CreateMaintenance/index.jsx
index b7417377e..71aa57966 100644
--- a/Client/src/Pages/Maintenance/CreateMaintenance/index.jsx
+++ b/Client/src/Pages/Maintenance/CreateMaintenance/index.jsx
@@ -7,9 +7,8 @@ import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "@mui/x-date-pickers";
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker";
-
+import { maintenanceWindowValidation } from "../../../Validation/validation";
import dayjs from "dayjs";
-
import Select from "../../../Components/Inputs/Select";
import Field from "../../../Components/Inputs/Field";
import Breadcrumbs from "../../../Components/Breadcrumbs";
@@ -17,10 +16,27 @@ import CalendarIcon from "../../../assets/icons/calendar.svg?react";
import "./index.css";
import Search from "../../../Components/Inputs/Search";
import { networkService } from "../../../main";
+import {
+ MS_PER_SECOND,
+ MS_PER_MINUTE,
+ MS_PER_HOUR,
+ MS_PER_DAY,
+} from "../../../Utils/timeUtils";
+
+const MS_LOOKUP = {
+ seconds: MS_PER_SECOND,
+ minutes: MS_PER_MINUTE,
+ hours: MS_PER_HOUR,
+ days: MS_PER_DAY,
+};
const repeatConfig = [
{ _id: 0, name: "Don't repeat", value: "none" },
- { _id: 1, name: "Repeat daily", value: "daily" },
+ {
+ _id: 1,
+ name: "Repeat daily",
+ value: "daily",
+ },
{ _id: 2, name: "Repeat weekly", value: "weekly" },
];
@@ -28,7 +44,10 @@ const durationConfig = [
{ _id: 0, name: "seconds" },
{ _id: 1, name: "minutes" },
{ _id: 2, name: "hours" },
- { _id: 3, name: "days" },
+ {
+ _id: 3,
+ name: "days",
+ },
];
const getValueById = (config, id) => {
@@ -38,8 +57,11 @@ const getValueById = (config, id) => {
const getIdByValue = (config, name) => {
const item = config.find((config) => {
- if (config.value) return config.value === name;
- else return config.name === name;
+ if (config.value) {
+ return config.value === name;
+ } else {
+ return config.name === name;
+ }
});
return item ? item._id : null;
};
@@ -51,13 +73,14 @@ const CreateMaintenance = () => {
const [search, setSearch] = useState("");
const [form, setForm] = useState({
repeat: "none",
- startDate: dayjs().toISOString(),
- startTime: dayjs().toISOString(),
+ startDate: dayjs(),
+ startTime: dayjs(),
duration: "",
durationUnit: "seconds",
name: "",
monitors: [],
});
+ const [errors, setErrors] = useState({});
useEffect(() => {
const fetchMonitors = async () => {
@@ -72,20 +95,82 @@ const CreateMaintenance = () => {
fetchMonitors();
}, [authToken, user]);
+ const buildErrors = (prev, id, error) => {
+ const updatedErrors = { ...prev };
+ if (error) {
+ updatedErrors[id] = error.details[0].message;
+ } else {
+ delete updatedErrors[id];
+ }
+ return updatedErrors;
+ };
+
const handleSearch = (value) => {
setSearch(value);
};
const handleSelectMonitors = (monitors) => {
setForm({ ...form, monitors });
+ const { error } = maintenanceWindowValidation.validate(
+ { monitors },
+ { abortEarly: false }
+ );
+ setErrors((prev) => {
+ return buildErrors(prev, "monitors", error);
+ });
};
const handleFormChange = (key, value) => {
setForm({ ...form, [key]: value });
+ const { error } = maintenanceWindowValidation.validate(
+ { [key]: value },
+ { abortEarly: false }
+ );
+ setErrors((prev) => {
+ return buildErrors(prev, key, error);
+ });
};
const handleTimeChange = (key, newTime) => {
setForm({ ...form, [key]: newTime });
+ const { error } = maintenanceWindowValidation.validate(
+ { [key]: newTime },
+ { abortEarly: false }
+ );
+ setErrors((prev) => {
+ return buildErrors(prev, key, error);
+ });
+ };
+
+ const handleSubmit = () => {
+ const { error } = maintenanceWindowValidation.validate(form, {
+ abortEarly: false,
+ });
+
+ // If errors, return early
+ if (error) {
+ const newErrors = {};
+ error.details.forEach((err) => {
+ newErrors[err.path[0]] = err.message;
+ });
+ setErrors(newErrors);
+ console.log(error);
+ return;
+ }
+ // Build timestamp for maintenance window from startDate and startTime
+ const startTimestamp = dayjs(form.startDate);
+ startTimestamp
+ .set("hour", form.startTime.hour())
+ .set("minute", form.startTime.minute())
+ .set("second", form.startTime.second())
+ .set("millisecond", form.startTime.millisecond());
+
+ // Build end timestamp for maintenance window
+ const MS_MULTIPLIER = MS_LOOKUP[form.durationUnit];
+ const durationInMs = form.duration * MS_MULTIPLIER;
+ const endTimeStamp = startTimestamp.add(durationInMs);
+ console.log(startTimestamp.toISOString());
+ console.log(endTimeStamp.toISOString());
};
return (
@@ -134,7 +219,7 @@ const CreateMaintenance = () => {
borderLeftColor={theme.palette.border.light}
>