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

343 lines
10 KiB
React
Raw Normal View History

2024-07-30 13:40:31 -04:00
import { useState } from "react";
2024-09-05 14:09:50 -07:00
import { Box, Button, ButtonGroup, Stack, Typography } from "@mui/material";
import { useSelector, useDispatch } from "react-redux";
import { monitorValidation } from "../../../Validation/validation";
import { useNavigate } from "react-router-dom";
2024-07-28 16:51:47 -04:00
import { useTheme } from "@emotion/react";
2024-09-05 14:09:50 -07:00
import { createPageSpeed } from "../../../Features/PageSpeedMonitor/pageSpeedMonitorSlice";
import { createToast } from "../../../Utils/toastUtils";
import { logger } from "../../../Utils/Logger";
import { ConfigBox } from "../../Monitors/styled";
import Radio from "../../../Components/Inputs/Radio";
2024-07-28 16:51:47 -04:00
import Field from "../../../Components/Inputs/Field";
import Select from "../../../Components/Inputs/Select";
import Checkbox from "../../../Components/Inputs/Checkbox";
import Breadcrumbs from "../../../Components/Breadcrumbs";
2024-07-28 16:51:47 -04:00
import "./index.css";
2024-07-30 13:40:31 -04:00
2024-09-05 14:09:50 -07:00
const CreatePageSpeed = () => {
2024-07-30 13:40:31 -04:00
const MS_PER_MINUTE = 60000;
const { user, authToken } = useSelector((state) => state.auth);
2024-09-05 14:09:50 -07:00
const dispatch = useDispatch();
const navigate = useNavigate();
const theme = useTheme();
2024-07-28 16:51:47 -04:00
2024-09-05 14:09:50 -07:00
const idMap = {
"monitor-url": "url",
"monitor-name": "name",
"monitor-checks-http": "type",
"monitor-checks-ping": "type",
"notify-email-default": "notification-email",
};
2024-07-28 16:51:47 -04:00
2024-09-05 14:09:50 -07:00
const [monitor, setMonitor] = useState({
url: "",
2024-09-05 14:09:50 -07:00
name: "",
type: "pagespeed",
notifications: [],
2024-08-06 18:45:20 -04:00
interval: 3,
});
2024-09-05 14:09:50 -07:00
const [https, setHttps] = useState(true);
const [errors, setErrors] = useState({});
2024-09-05 14:09:50 -07:00
const handleChange = (event, name) => {
const { value, id } = event.target;
if (!name) name = idMap[id];
2024-09-05 14:09:50 -07:00
if (name.includes("notification-")) {
name = name.replace("notification-", "");
let hasNotif = monitor.notifications.some(
(notification) => notification.type === name
);
setMonitor((prev) => {
const notifs = [...prev.notifications];
if (hasNotif) {
return {
...prev,
notifications: notifs.filter((notif) => notif.type !== name),
};
} else {
return {
...prev,
notifications: [
...notifs,
name === "email"
? { type: name, address: value }
: // TODO - phone number
{ type: name, phone: value },
],
};
}
});
} else {
setMonitor((prev) => ({
...prev,
[name]: value,
}));
2024-09-05 14:09:50 -07:00
const { error } = monitorValidation.validate(
{ [name]: value },
{ abortEarly: false }
);
setErrors((prev) => {
const updatedErrors = { ...prev };
if (error) updatedErrors[name] = error.details[0].message;
else delete updatedErrors[name];
return updatedErrors;
});
}
};
2024-09-05 14:09:50 -07:00
const handleCreateMonitor = async (event) => {
2024-07-30 13:40:31 -04:00
event.preventDefault();
2024-09-05 14:09:50 -07:00
//obj to submit
let form = {
url: `http${https ? "s" : ""}://` + monitor.url,
name: monitor.name === "" ? monitor.url : monitor.name,
type: monitor.type,
interval: monitor.interval * MS_PER_MINUTE,
2024-07-30 13:40:31 -04:00
};
2024-09-05 14:09:50 -07:00
const { error } = monitorValidation.validate(form, {
abortEarly: false,
});
2024-07-30 13:40:31 -04:00
if (error) {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
setErrors(newErrors);
2024-09-05 14:09:50 -07:00
createToast({ body: "Error validation data." });
2024-07-30 13:40:31 -04:00
} else {
2024-09-05 14:09:50 -07:00
form = {
...form,
description: form.name,
2024-08-28 10:31:37 -07:00
teamId: user.teamId,
2024-09-05 14:09:50 -07:00
userId: user._id,
notifications: monitor.notifications,
2024-07-30 13:40:31 -04:00
};
2024-09-05 14:09:50 -07:00
const action = await dispatch(
createPageSpeed({ authToken, monitor: form })
);
if (action.meta.requestStatus === "fulfilled") {
createToast({ body: "Monitor created successfully!" });
navigate("/pagespeed");
} else {
createToast({ body: "Failed to create monitor." });
2024-07-30 13:40:31 -04:00
}
}
};
2024-09-05 14:09:50 -07:00
//select values
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" },
];
2024-07-28 16:51:47 -04:00
return (
2024-09-05 14:09:50 -07:00
<Box
className="create-monitor"
sx={{
"& h1": {
color: theme.palette.text.primary,
},
}}
>
<Breadcrumbs
list={[
2024-09-05 14:35:55 -07:00
{ name: "pagespeed", path: "/pagespeed" },
{ name: "create", path: `/pagespeed/create` },
]}
2024-08-02 14:46:24 -04:00
/>
<Stack
component="form"
2024-09-05 14:09:50 -07:00
className="create-monitor-form"
onSubmit={handleCreateMonitor}
2024-07-30 13:40:31 -04:00
noValidate
spellCheck="false"
2024-08-28 14:42:38 -04:00
gap={theme.spacing(12)}
2024-09-05 14:09:50 -07:00
mt={theme.spacing(6)}
2024-07-30 13:40:31 -04:00
>
2024-09-05 14:09:50 -07:00
<Typography component="h1">
2024-08-28 14:42:38 -04:00
<Typography
component="span"
fontSize="inherit"
color={theme.palette.text.secondary}
>
Create your{" "}
</Typography>
<Typography component="span" fontSize="inherit" fontWeight="inherit">
2024-09-05 14:09:50 -07:00
Pagespeed monitor
2024-08-28 14:42:38 -04:00
</Typography>
</Typography>
2024-09-05 14:09:50 -07:00
<ConfigBox>
<Box>
<Typography component="h2">General settings</Typography>
<Typography component="p">
Here you can select the URL of the host, together with the type of
monitor.
</Typography>
</Box>
<Stack gap={theme.spacing(15)}>
2024-08-02 14:46:24 -04:00
<Field
2024-09-05 14:09:50 -07:00
type={"url"}
2024-08-02 14:46:24 -04:00
id="monitor-url"
2024-09-05 14:09:50 -07:00
label="URL to monitor"
https={https}
placeholder="google.com"
value={monitor.url}
onChange={handleChange}
error={errors["url"]}
/>
2024-09-05 14:09:50 -07:00
<Field
type="text"
id="monitor-name"
label="Display name"
isOptional={true}
placeholder="Google"
value={monitor.name}
onChange={handleChange}
error={errors["name"]}
/>
2024-08-02 14:46:24 -04:00
</Stack>
2024-09-05 14:09:50 -07:00
</ConfigBox>
<ConfigBox>
<Box>
<Typography component="h2">Checks to perform</Typography>
<Typography component="p">
You can always add or remove checks after adding your site.
2024-08-02 14:46:24 -04:00
</Typography>
2024-09-05 14:09:50 -07:00
</Box>
<Stack gap={theme.spacing(12)}>
<Stack gap={theme.spacing(6)}>
<Radio
id="monitor-checks-http"
title="Website monitoring"
desc="Use HTTP(s) to monitor your website or API endpoint."
size="small"
value="http"
checked={monitor.type === "pagespeed"}
onChange={(event) => handleChange(event)}
2024-08-02 14:46:24 -04:00
/>
2024-09-05 14:09:50 -07:00
<ButtonGroup sx={{ ml: "32px" }}>
<Button
variant="group"
filled={https.toString()}
onClick={() => setHttps(true)}
>
HTTPS
</Button>
<Button
variant="group"
filled={(!https).toString()}
onClick={() => setHttps(false)}
>
HTTP
</Button>
</ButtonGroup>
</Stack>
{errors["type"] ? (
<Box className="error-container">
<Typography
component="p"
className="input-error"
color={theme.palette.error.text}
>
{errors["type"]}
</Typography>
</Box>
) : (
""
)}
</Stack>
</ConfigBox>
<ConfigBox>
<Box>
<Typography component="h2">Incident notifications</Typography>
<Typography component="p">
When there is an incident, notify users.
</Typography>
</Box>
<Stack gap={theme.spacing(6)}>
<Typography component="p">When there is a new incident,</Typography>
<Checkbox
id="notify-sms"
label="Notify via SMS (coming soon)"
isChecked={false}
value=""
onChange={() => logger.warn("disabled")}
isDisabled={true}
/>
<Checkbox
id="notify-email-default"
label={`Notify via email (to ${user.email})`}
isChecked={monitor.notifications.some(
(notification) => notification.type === "email"
)}
value={user?.email}
onChange={(event) => handleChange(event)}
/>
<Checkbox
id="notify-email"
label="Also notify via email to multiple addresses (coming soon)"
isChecked={false}
value=""
onChange={() => logger.warn("disabled")}
isDisabled={true}
/>
{monitor.notifications.some(
(notification) => notification.type === "emails"
) ? (
2024-08-28 18:15:48 -04:00
<Box mx={theme.spacing(16)}>
2024-08-02 14:46:24 -04:00
<Field
2024-09-05 14:09:50 -07:00
id="notify-email-list"
type="text"
placeholder="name@gmail.com"
2024-08-02 14:46:24 -04:00
value=""
onChange={() => logger.warn("disabled")}
2024-08-02 14:46:24 -04:00
/>
2024-08-28 14:42:38 -04:00
<Typography mt={theme.spacing(4)}>
2024-08-02 14:46:24 -04:00
You can separate multiple emails with a comma
</Typography>
</Box>
2024-09-05 14:09:50 -07:00
) : (
""
)}
2024-08-02 14:46:24 -04:00
</Stack>
2024-09-05 14:09:50 -07:00
</ConfigBox>
<ConfigBox>
<Box>
<Typography component="h2">Advanced settings</Typography>
</Box>
<Stack gap={theme.spacing(12)}>
<Select
id="monitor-interval"
label="Check frequency"
value={monitor.interval || 3}
onChange={(event) => handleChange(event, "interval")}
items={frequencies}
/>
</Stack>
</ConfigBox>
<Stack direction="row" justifyContent="flex-end">
<Button
2024-08-31 17:37:55 -04:00
variant="contained"
color="primary"
2024-09-05 14:09:50 -07:00
onClick={handleCreateMonitor}
disabled={Object.keys(errors).length !== 0 && true}
2024-08-31 17:37:55 -04:00
>
2024-09-05 14:09:50 -07:00
Create monitor
</Button>
2024-07-28 16:51:47 -04:00
</Stack>
</Stack>
2024-09-05 14:09:50 -07:00
</Box>
2024-07-28 16:51:47 -04:00
);
};
export default CreatePageSpeed;