add the All select option to monitor maintenance window for all monitors same as status pages
This commit is contained in:
1 parent
6ec6d582d8
commit
2c1a01bb6a
2 files changed
+157
-5
No files matched your search
@@ -0,0 +1,107 @@
|
|||||||
|
// Components
|
||||||
|
import { Stack, Typography } from "@mui/material";
|
||||||
|
import ReorderRoundedIcon from "@mui/icons-material/ReorderRounded";
|
||||||
|
import DeleteIcon from "@mui/icons-material/Delete";
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
import { DragDropContext, Droppable, Draggable } from "@hello-pangea/dnd";
|
||||||
|
import { useTheme } from "@emotion/react";
|
||||||
|
import Checkbox from "../../../../../Components/Inputs/Checkbox";
|
||||||
|
const MonitorListItem = ({
|
||||||
|
monitor,
|
||||||
|
innerRef,
|
||||||
|
draggableProps,
|
||||||
|
dragHandleProps,
|
||||||
|
onDelete,
|
||||||
|
}) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
return (
|
||||||
|
<Stack
|
||||||
|
direction={"row"}
|
||||||
|
{...draggableProps}
|
||||||
|
{...dragHandleProps}
|
||||||
|
ref={innerRef}
|
||||||
|
gap={theme.spacing(4)}
|
||||||
|
margin={theme.spacing(4)}
|
||||||
|
padding={theme.spacing(4)}
|
||||||
|
borderRadius={theme.shape.borderRadius}
|
||||||
|
alignItems={"center"}
|
||||||
|
justifyContent={"start"}
|
||||||
|
border={`1px solid ${theme.palette.primary.lowContrast}`}
|
||||||
|
>
|
||||||
|
<ReorderRoundedIcon />
|
||||||
|
<Typography>{monitor.name}</Typography>
|
||||||
|
<DeleteIcon
|
||||||
|
sx={{ marginLeft: "auto" }}
|
||||||
|
onClick={() => {
|
||||||
|
onDelete(monitor);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const MonitorList = ({ selectedMonitors, setSelectedMonitors }) => {
|
||||||
|
const onDelete = (monitorToDelete) => {
|
||||||
|
const newMonitors = selectedMonitors.filter(
|
||||||
|
(monitor) => monitor._id !== monitorToDelete._id
|
||||||
|
);
|
||||||
|
setSelectedMonitors(newMonitors);
|
||||||
|
};
|
||||||
|
const reorder = (list, startIndex, endIndex) => {
|
||||||
|
const result = Array.from(list);
|
||||||
|
const [removed] = result.splice(startIndex, 1);
|
||||||
|
result.splice(endIndex, 0, removed);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
const onDragEnd = (result) => {
|
||||||
|
// dropped outside the list
|
||||||
|
if (!result.destination) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reorderedMonitors = reorder(
|
||||||
|
selectedMonitors,
|
||||||
|
result.source.index,
|
||||||
|
result.destination.index
|
||||||
|
);
|
||||||
|
|
||||||
|
setSelectedMonitors(reorderedMonitors);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<DragDropContext onDragEnd={onDragEnd}>
|
||||||
|
<Droppable droppableId="droppable">
|
||||||
|
{(provided, snapshot) => (
|
||||||
|
<Stack
|
||||||
|
{...provided.droppableProps}
|
||||||
|
ref={provided.innerRef}
|
||||||
|
>
|
||||||
|
{selectedMonitors?.map((monitor, index) => (
|
||||||
|
<Draggable
|
||||||
|
key={monitor._id}
|
||||||
|
draggableId={monitor._id}
|
||||||
|
index={index}
|
||||||
|
>
|
||||||
|
{(provided, snapshot) => (
|
||||||
|
<MonitorListItem
|
||||||
|
monitor={monitor}
|
||||||
|
innerRef={provided.innerRef}
|
||||||
|
draggableProps={provided.draggableProps}
|
||||||
|
dragHandleProps={provided.dragHandleProps}
|
||||||
|
onDelete={onDelete}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Draggable>
|
||||||
|
))}
|
||||||
|
{provided.placeholder}
|
||||||
|
</Stack>
|
||||||
|
)}
|
||||||
|
</Droppable>
|
||||||
|
</DragDropContext>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MonitorList;
|
||||||
@@ -9,6 +9,8 @@ import { DatePicker } from "@mui/x-date-pickers/DatePicker";
|
|||||||
import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker";
|
import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker";
|
||||||
import { maintenanceWindowValidation } from "../../../Validation/validation";
|
import { maintenanceWindowValidation } from "../../../Validation/validation";
|
||||||
import { createToast } from "../../../Utils/toastUtils";
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
|
import MonitorList from "./Components/MonitorList";
|
||||||
|
import Checkbox from "../../../Components/Inputs/Checkbox";
|
||||||
|
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import Select from "../../../Components/Inputs/Select";
|
import Select from "../../../Components/Inputs/Select";
|
||||||
@@ -129,6 +131,7 @@ const CreateMaintenance = () => {
|
|||||||
monitors: [],
|
monitors: [],
|
||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
const [selectedMonitors, setSelectedMonitors] = useState([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchMonitors = async () => {
|
const fetchMonitors = async () => {
|
||||||
@@ -165,6 +168,7 @@ const CreateMaintenance = () => {
|
|||||||
durationUnit,
|
durationUnit,
|
||||||
monitors: monitor ? [monitor] : [],
|
monitors: monitor ? [monitor] : [],
|
||||||
});
|
});
|
||||||
|
setSelectedMonitors(monitor ? [monitor] : []);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createToast({ body: "Failed to fetch data" });
|
createToast({ body: "Failed to fetch data" });
|
||||||
logger.error("Failed to fetch monitors", error);
|
logger.error("Failed to fetch monitors", error);
|
||||||
@@ -175,6 +179,11 @@ const CreateMaintenance = () => {
|
|||||||
fetchMonitors();
|
fetchMonitors();
|
||||||
}, [user]);
|
}, [user]);
|
||||||
|
|
||||||
|
// Sync form.monitors with selectedMonitors
|
||||||
|
useEffect(() => {
|
||||||
|
setForm((prev) => ({ ...prev, monitors: selectedMonitors }));
|
||||||
|
}, [selectedMonitors]);
|
||||||
|
|
||||||
const handleSearch = (value) => {
|
const handleSearch = (value) => {
|
||||||
setSearch(value);
|
setSearch(value);
|
||||||
};
|
};
|
||||||
@@ -212,6 +221,33 @@ const CreateMaintenance = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleMonitorsChange = (selected) => {
|
||||||
|
if (selected.some((m) => m._id === "__all__")) {
|
||||||
|
if (selectedMonitors.length !== monitors.length) {
|
||||||
|
setSelectedMonitors(monitors);
|
||||||
|
} else {
|
||||||
|
setSelectedMonitors([]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Ensure all are objects
|
||||||
|
const selectedObjs = selected
|
||||||
|
.map((sel) =>
|
||||||
|
typeof sel === "string" ? monitors.find((m) => m._id === sel) : sel
|
||||||
|
)
|
||||||
|
.filter(Boolean);
|
||||||
|
setSelectedMonitors(selectedObjs);
|
||||||
|
}
|
||||||
|
const { error } = maintenanceWindowValidation.validate(
|
||||||
|
{ monitors: selected },
|
||||||
|
{ abortEarly: false }
|
||||||
|
);
|
||||||
|
setErrors((prev) => {
|
||||||
|
return buildErrors(prev, "monitors", error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const allSelected = selectedMonitors.length === monitors.length && monitors.length > 0;
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
if (hasValidationErrors(form, maintenanceWindowValidation, setErrors)) return;
|
if (hasValidationErrors(form, maintenanceWindowValidation, setErrors)) return;
|
||||||
// Build timestamp for maintenance window from startDate and startTime
|
// Build timestamp for maintenance window from startDate and startTime
|
||||||
@@ -267,6 +303,10 @@ const CreateMaintenance = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add Select All option to the dropdown
|
||||||
|
const selectAllOption = { _id: "__all__", name: "Select All" };
|
||||||
|
const monitorOptions = [selectAllOption, ...monitors];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className="create-maintenance">
|
<Box className="create-maintenance">
|
||||||
<Breadcrumbs
|
<Breadcrumbs
|
||||||
@@ -526,18 +566,23 @@ const CreateMaintenance = () => {
|
|||||||
label={t("addMonitors")}
|
label={t("addMonitors")}
|
||||||
multiple={true}
|
multiple={true}
|
||||||
isAdorned={false}
|
isAdorned={false}
|
||||||
options={monitors ? monitors : []}
|
options={monitorOptions}
|
||||||
filteredBy="name"
|
filteredBy="name"
|
||||||
secondaryLabel={"type"}
|
secondaryLabel={"type"}
|
||||||
inputValue={search}
|
inputValue={search}
|
||||||
value={form.monitors}
|
value={selectedMonitors}
|
||||||
handleInputChange={handleSearch}
|
handleInputChange={setSearch}
|
||||||
handleChange={handleSelectMonitors}
|
handleChange={handleMonitorsChange}
|
||||||
error={errors["monitors"]}
|
error={errors["monitors"]}
|
||||||
disabled={maintenanceWindowId !== undefined}
|
disabled={form.isAllMonitors}
|
||||||
|
/>
|
||||||
|
<MonitorList
|
||||||
|
selectedMonitors={selectedMonitors}
|
||||||
|
setSelectedMonitors={setSelectedMonitors}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
|
|
||||||
<Box
|
<Box
|
||||||
ml="auto"
|
ml="auto"
|
||||||
display="inline-block"
|
display="inline-block"
|
||||||
|
|||||||
Reference in new issue
Block a user