Files
Checkmate/Client/src/Pages/PageSpeed/Configure/index.jsx
T

387 lines
13 KiB
React
Raw Normal View History

2024-08-08 16:33:32 -04:00
import { useEffect, useState } from "react";
import { useTheme } from "@emotion/react";
2024-09-03 12:07:59 -07:00
import { Box, Button, Modal, Stack, Typography } from "@mui/material";
2024-08-08 16:33:32 -04:00
import { useDispatch, useSelector } from "react-redux";
import { useNavigate, useParams } from "react-router";
2024-08-08 16:53:50 -04:00
import {
deletePageSpeed,
2024-09-03 12:07:59 -07:00
getPagespeedMonitorById,
2024-08-27 12:07:30 -07:00
getPageSpeedByTeamId,
2024-08-08 16:53:50 -04:00
updatePageSpeed,
2024-09-03 12:07:59 -07:00
pausePageSpeed,
2024-08-08 16:53:50 -04:00
} from "../../../Features/PageSpeedMonitor/pageSpeedMonitorSlice";
import { monitorValidation } from "../../../Validation/validation";
2024-08-08 16:37:23 -04:00
import { createToast } from "../../../Utils/toastUtils";
2024-08-22 12:23:52 -04:00
import { logger } from "../../../Utils/Logger";
2024-08-08 16:33:32 -04:00
import Field from "../../../Components/Inputs/Field";
import Select from "../../../Components/Inputs/Select";
import Checkbox from "../../../Components/Inputs/Checkbox";
import PauseCircleOutlineIcon from "@mui/icons-material/PauseCircleOutline";
2024-08-17 17:59:49 -04:00
import Breadcrumbs from "../../../Components/Breadcrumbs";
2024-08-22 12:23:52 -04:00
import PulseDot from "../../../Components/Animated/PulseDot";
2024-09-03 12:07:59 -07:00
import LoadingButton from "@mui/lab/LoadingButton";
import PlayCircleOutlineRoundedIcon from "@mui/icons-material/PlayCircleOutlineRounded";
import SkeletonLayout from "./skeleton";
2024-08-08 16:33:32 -04:00
import "./index.css";
import useUtils from "../../Monitors/utils";
2024-08-08 15:41:15 -04:00
const PageSpeedConfigure = () => {
2024-08-08 16:33:32 -04:00
const theme = useTheme();
const navigate = useNavigate();
const dispatch = useDispatch();
const MS_PER_MINUTE = 60000;
const { authToken } = useSelector((state) => state.auth);
2024-09-03 12:07:59 -07:00
const { isLoading } = useSelector((state) => state.pageSpeedMonitors);
2024-08-08 16:33:32 -04:00
const { monitorId } = useParams();
const [monitor, setMonitor] = useState({});
const [errors, setErrors] = useState({});
const { determineState, statusColor } = useUtils();
2024-08-08 16:33:32 -04:00
const frequencies = [
{ _id: 3, name: "3 minutes" },
{ _id: 5, name: "5 minutes" },
{ _id: 10, name: "10 minutes" },
{ _id: 20, name: "20 minutes" },
{ _id: 60, name: "1 hour" },
{ _id: 1440, name: "1 day" },
{ _id: 10080, name: "1 week" },
];
useEffect(() => {
2024-09-03 12:07:59 -07:00
const fetchMonitor = async () => {
try {
const action = await dispatch(
getPagespeedMonitorById({ authToken, monitorId })
);
if (getPagespeedMonitorById.fulfilled.match(action)) {
const monitor = action.payload.data;
setMonitor(monitor);
} else if (getPagespeedMonitorById.rejected.match(action)) {
throw new Error(action.error.message);
}
} catch (error) {
logger.error("Error fetching monitor of id: " + monitorId);
navigate("/not-found", { replace: true });
}
};
fetchMonitor();
}, [dispatch, authToken, monitorId, navigate]);
2024-08-08 16:33:32 -04:00
const handleChange = (event, id) => {
2024-08-08 16:53:50 -04:00
let { value } = event.target;
2024-08-08 17:15:39 -04:00
if (id === "interval") {
2024-08-08 16:53:50 -04:00
value = value * MS_PER_MINUTE;
}
setMonitor((prev) => ({ ...prev, [id]: value }));
2024-08-08 16:33:32 -04:00
const { error } = monitorValidation.validate(
{ [id]: value },
{ abortEarly: false }
);
setErrors((prev) => {
const newErrors = { ...prev };
if (error) newErrors[id] = error.details[0].message;
else delete newErrors[id];
return newErrors;
});
};
2024-09-03 12:07:59 -07:00
const handlePause = async () => {
try {
const action = await dispatch(pausePageSpeed({ authToken, monitorId }));
if (pausePageSpeed.fulfilled.match(action)) {
const monitor = action.payload.data;
setMonitor(monitor);
} else if (pausePageSpeed.rejected.match(action)) {
throw new Error(action.error.message);
}
} catch (error) {
logger.error("Error pausing monitor: " + monitorId);
createToast({ body: "Failed to pause monitor" });
}
};
2024-08-08 16:53:50 -04:00
const handleSave = async (event) => {
2024-08-08 16:33:32 -04:00
event.preventDefault();
2024-08-08 16:37:23 -04:00
const action = await dispatch(
2024-08-08 16:53:50 -04:00
updatePageSpeed({ authToken, monitor: monitor })
2024-08-08 16:37:23 -04:00
);
2024-08-08 16:53:50 -04:00
if (action.meta.requestStatus === "fulfilled") {
createToast({ body: "Monitor updated successfully!" });
2024-08-27 12:07:30 -07:00
dispatch(getPageSpeedByTeamId(authToken));
2024-08-08 16:53:50 -04:00
} else {
createToast({ body: "Failed to update monitor." });
}
};
const [isOpen, setIsOpen] = useState(false);
const handleRemove = async (event) => {
event.preventDefault();
const action = await dispatch(deletePageSpeed({ authToken, monitor }));
2024-08-08 16:37:23 -04:00
if (action.meta.requestStatus === "fulfilled") {
navigate("/pagespeed");
} else {
createToast({ body: "Failed to delete monitor." });
}
2024-08-08 16:33:32 -04:00
};
return (
2024-08-28 14:42:38 -04:00
<Stack className="configure-pagespeed" gap={theme.spacing(12)}>
2024-09-03 12:07:59 -07:00
{Object.keys(monitor).length === 0 ? (
2024-08-08 17:15:39 -04:00
<SkeletonLayout />
) : (
<>
<Breadcrumbs
list={[
{ name: "pagespeed", path: "/pagespeed" },
{ name: "details", path: `/pagespeed/${monitorId}` },
{ name: "configure", path: `/pagespeed/configure/${monitorId}` },
]}
2024-08-08 17:15:39 -04:00
/>
<Stack
component="form"
2024-08-08 17:15:39 -04:00
noValidate
spellCheck="false"
onSubmit={handleSave}
flex={1}
2024-08-28 14:42:38 -04:00
gap={theme.spacing(12)}
2024-08-08 17:15:39 -04:00
>
2024-08-28 14:42:38 -04:00
<Stack direction="row" gap={theme.spacing(2)}>
<PulseDot color={statusColor[determineState(monitor)]} />
2024-08-08 17:15:39 -04:00
<Box>
<Typography
component="h1"
2024-08-28 14:42:38 -04:00
mb={theme.spacing(2)}
2024-08-08 17:15:39 -04:00
sx={{ lineHeight: 1 }}
2024-08-28 14:42:38 -04:00
color={theme.palette.text.primary}
2024-08-08 17:15:39 -04:00
>
{monitor?.url}
</Typography>
<Typography
component="span"
sx={{
color: statusColor[determineState(monitor)],
2024-08-08 17:15:39 -04:00
}}
>
Your pagespeed monitor is {determineState(monitor)}.
2024-08-08 17:15:39 -04:00
</Typography>
</Box>
<Box alignSelf="flex-end" ml="auto">
2024-09-03 12:07:59 -07:00
<LoadingButton
onClick={handlePause}
loading={isLoading}
2024-08-31 17:37:55 -04:00
variant="contained"
color="secondary"
2024-08-08 17:15:39 -04:00
sx={{
2024-08-28 14:42:38 -04:00
pl: theme.spacing(4),
pr: theme.spacing(6),
2024-08-08 17:15:39 -04:00
"& svg": {
2024-08-28 14:42:38 -04:00
mr: theme.spacing(2),
2024-08-31 17:37:55 -04:00
"& path": {
stroke: theme.palette.other.icon,
strokeWidth: 0.1,
},
2024-08-08 17:15:39 -04:00
},
}}
2024-08-31 17:37:55 -04:00
>
2024-09-03 12:07:59 -07:00
{monitor?.isActive ? (
<>
<PauseCircleOutlineIcon />
Pause
</>
) : (
<>
<PlayCircleOutlineRoundedIcon />
Resume
</>
)}
</LoadingButton>
<LoadingButton
loading={isLoading}
2024-08-31 17:37:55 -04:00
variant="contained"
color="error"
onClick={() => setIsOpen(true)}
2024-08-08 17:15:39 -04:00
sx={{
2024-08-28 14:42:38 -04:00
ml: theme.spacing(6),
2024-08-08 17:15:39 -04:00
}}
2024-08-31 17:37:55 -04:00
>
Remove
2024-09-03 12:07:59 -07:00
</LoadingButton>
2024-08-08 17:15:39 -04:00
</Box>
</Stack>
2024-08-28 14:42:38 -04:00
<Stack
border={1}
borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius}
backgroundColor={theme.palette.background.main}
p={theme.spacing(20)}
pl={theme.spacing(15)}
gap={theme.spacing(20)}
sx={{
"& h3, & p": {
color: theme.palette.text.secondary,
},
}}
>
2024-08-08 17:15:39 -04:00
<Stack direction="row">
<Typography component="h3">Monitor display name</Typography>
2024-08-08 16:33:32 -04:00
<Field
2024-08-08 17:15:39 -04:00
type="text"
id="monitor-name"
placeholder="Example monitor"
value={monitor?.name || ""}
onChange={(event) => handleChange(event, "name")}
error={errors.name}
2024-08-08 16:33:32 -04:00
/>
2024-08-08 17:15:39 -04:00
</Stack>
2024-08-28 14:42:38 -04:00
<Stack
direction="row"
sx={{
".MuiInputBase-root:has(> .Mui-disabled)": {
backgroundColor: theme.palette.background.accent,
},
}}
>
2024-08-08 17:15:39 -04:00
<Typography component="h3">URL</Typography>
<Field
type="url"
id="monitor-url"
placeholder="random.website.com"
value={monitor?.url?.replace("http://", "") || ""}
onChange={(event) => handleChange(event, "url")}
error={errors.url}
disabled={true}
/>
</Stack>
<Stack direction="row">
<Typography component="h3">Check frequency</Typography>
<Select
id="monitor-frequency"
items={frequencies}
value={monitor?.interval / MS_PER_MINUTE || 3}
onChange={(event) => handleChange(event, "interval")}
/>
</Stack>
<Stack direction="row">
<Typography component="h3">
Incidents notifications{" "}
<Typography component="span">(coming soon)</Typography>
2024-08-08 16:33:32 -04:00
</Typography>
2024-08-28 14:42:38 -04:00
<Stack
className="section-disabled"
backgroundColor={theme.palette.background.fill}
>
<Typography mb={theme.spacing(4)}>
2024-08-08 17:15:39 -04:00
When there is a new incident,
</Typography>
<Checkbox
id="notify-sms"
label="Notify via SMS (coming soon)"
isChecked={false}
isDisabled={true}
/>
<Checkbox
id="notify-email"
label="Notify via email (to gorkem.cetin@bluewavelabs.ca)"
isChecked={false}
/>
<Checkbox
id="notify-emails"
label="Notify via email to following emails"
isChecked={false}
/>
2024-08-28 14:42:38 -04:00
<Box mx={theme.spacing(16)}>
2024-08-08 17:15:39 -04:00
<Field
id="notify-emails-list"
placeholder="notifications@gmail.com"
value=""
onChange={() => logger.warn("disabled")}
2024-08-08 17:15:39 -04:00
error=""
/>
2024-08-28 14:42:38 -04:00
<Typography mt={theme.spacing(4)}>
2024-08-08 17:15:39 -04:00
You can separate multiple emails with a comma
</Typography>
</Box>
</Stack>
</Stack>
2024-08-08 16:33:32 -04:00
</Stack>
<Stack direction="row" justifyContent="flex-end" mt="auto">
2024-09-03 12:07:59 -07:00
<LoadingButton
loading={isLoading}
2024-08-08 17:15:39 -04:00
type="submit"
2024-08-31 17:37:55 -04:00
variant="contained"
color="primary"
2024-08-08 17:15:39 -04:00
onClick={handleSave}
2024-08-31 17:37:55 -04:00
sx={{ px: theme.spacing(12) }}
>
Save
2024-09-03 12:07:59 -07:00
</LoadingButton>
2024-08-08 17:15:39 -04:00
</Stack>
</Stack>
2024-08-08 17:15:39 -04:00
</>
)}
2024-08-08 16:33:32 -04:00
<Modal
aria-labelledby="modal-delete-pagespeed-monitor"
aria-describedby="delete-pagespeed-monitor-confirmation"
open={isOpen}
onClose={() => setIsOpen(false)}
disablePortal
>
<Stack
2024-08-28 14:42:38 -04:00
gap={theme.spacing(2)}
2024-08-08 16:33:32 -04:00
sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: 400,
2024-08-28 14:42:38 -04:00
bgcolor: theme.palette.background.main,
border: 1,
borderColor: theme.palette.border.light,
borderRadius: theme.shape.borderRadius,
2024-08-08 16:33:32 -04:00
boxShadow: 24,
2024-08-28 14:42:38 -04:00
p: theme.spacing(15),
2024-08-08 16:33:32 -04:00
"&:focus": {
outline: "none",
},
}}
>
2024-08-28 14:42:38 -04:00
<Typography
id="modal-delete-pagespeed-monitor"
component="h2"
color={theme.palette.text.secondary}
>
2024-08-08 16:33:32 -04:00
Do you really want to delete this monitor?
</Typography>
2024-08-28 14:42:38 -04:00
<Typography
id="delete-pagespeed-monitor-confirmation"
color={theme.palette.text.tertiary}
>
2024-08-08 16:33:32 -04:00
Once deleted, this monitor cannot be retrieved.
</Typography>
<Stack
direction="row"
2024-08-28 14:42:38 -04:00
gap={theme.spacing(4)}
mt={theme.spacing(12)}
2024-08-08 16:33:32 -04:00
justifyContent="flex-end"
>
<Button
2024-08-31 17:37:55 -04:00
variant="text"
color="info"
2024-08-08 16:33:32 -04:00
onClick={() => setIsOpen(false)}
2024-08-31 17:37:55 -04:00
>
Cancel
</Button>
<Button variant="contained" color="error" onClick={handleRemove}>
Delete
</Button>
2024-08-08 16:33:32 -04:00
</Stack>
</Stack>
</Modal>
</Stack>
2024-08-08 16:33:32 -04:00
);
2024-08-08 15:41:15 -04:00
};
export default PageSpeedConfigure;