Files
Checkmate/Client/src/Pages/Monitors/CreateMonitor/index.jsx
T

386 lines
12 KiB
React
Raw Normal View History

2024-08-20 17:44:26 +05:30
import { useState, useEffect } from "react";
2024-08-31 14:53:15 -04:00
import { Box, Button, ButtonGroup, Stack, Typography } from "@mui/material";
import { useSelector, useDispatch } from "react-redux";
2024-07-19 18:04:58 -04:00
import { monitorValidation } from "../../../Validation/validation";
2024-07-30 11:25:14 -07:00
import { createUptimeMonitor } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
2024-08-20 17:44:26 +05:30
import { useNavigate, useParams } from "react-router-dom";
import { useTheme } from "@emotion/react";
2024-08-28 14:42:38 -04:00
import { createToast } from "../../../Utils/toastUtils";
import { logger } from "../../../Utils/Logger";
import { ConfigBox } from "../styled";
import Radio from "../../../Components/Inputs/Radio";
2024-07-17 20:15:45 -04:00
import Field from "../../../Components/Inputs/Field";
2024-07-24 14:09:24 -04:00
import Select from "../../../Components/Inputs/Select";
import Checkbox from "../../../Components/Inputs/Checkbox";
2024-08-17 17:39:22 -04:00
import Breadcrumbs from "../../../Components/Breadcrumbs";
2024-09-06 10:09:13 -07:00
import { getUptimeMonitorById } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
2024-08-28 14:42:38 -04:00
import "./index.css";
2024-06-18 22:39:58 +03:30
2024-07-17 20:15:45 -04:00
const CreateMonitor = () => {
const MS_PER_MINUTE = 60000;
const { user, authToken } = useSelector((state) => state.auth);
2024-08-20 17:44:26 +05:30
const { monitors } = useSelector((state) => state.uptimeMonitors);
const dispatch = useDispatch();
const navigate = useNavigate();
const theme = useTheme();
2024-07-31 15:53:01 -04:00
const idMap = {
"monitor-url": "url",
"monitor-name": "name",
"monitor-checks-http": "type",
"monitor-checks-ping": "type",
"notify-email-default": "notification-email",
2024-07-31 15:53:01 -04:00
};
2024-06-27 17:26:00 -04:00
2024-08-20 17:44:26 +05:30
const { monitorId } = useParams();
2024-07-31 15:53:01 -04:00
const [monitor, setMonitor] = useState({
url: "",
name: "",
2024-08-28 14:42:38 -04:00
type: "http",
2024-07-31 15:53:01 -04:00
notifications: [],
2024-07-19 18:04:58 -04:00
interval: 1,
2024-06-27 16:29:50 -04:00
});
const [https, setHttps] = useState(true);
2024-07-31 15:53:01 -04:00
const [errors, setErrors] = useState({});
2024-06-26 21:56:56 +03:30
2024-08-20 17:44:26 +05:30
useEffect(() => {
2024-09-06 10:09:13 -07:00
const fetchMonitor = async () => {
if (monitorId) {
const action = await dispatch(
getUptimeMonitorById({ authToken, monitorId })
);
if (action.payload.success) {
const data = action.payload.data;
const { name, ...rest } = data; //data.name is read-only
if (rest.type === "http") {
const url = new URL(rest.url);
rest.url = url.host;
}
rest.name = `${name} (Clone)`;
rest.interval /= MS_PER_MINUTE;
setMonitor({
...rest,
});
} else {
navigate("/not-found", { replace: true });
createToast({
body: "There was an error cloning the monitor.",
});
}
2024-08-31 01:26:30 +05:30
}
2024-09-06 10:09:13 -07:00
};
fetchMonitor();
2024-08-20 17:44:26 +05:30
}, [monitorId, authToken, monitors]);
2024-07-31 15:53:01 -04:00
const handleChange = (event, name) => {
const { value, id } = event.target;
if (!name) name = idMap[id];
2024-06-27 17:26:00 -04: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-06-27 17:26:00 -04: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-06-26 21:56:56 +03:30
};
2024-07-17 20:15:45 -04:00
const handleCreateMonitor = async (event) => {
2024-06-27 17:26:00 -04:00
event.preventDefault();
//obj to submit
2024-07-31 15:53:01 -04:00
let form = {
2024-07-18 12:11:27 -07:00
url:
2024-07-26 21:57:18 -04:00
//preprending protocol for url
2024-07-31 15:53:01 -04:00
monitor.type === "http"
? `http${https ? "s" : ""}://` + monitor.url
: monitor.url,
2024-08-24 16:54:37 -04:00
name: monitor.name === "" ? monitor.url : monitor.name,
2024-07-31 15:53:01 -04:00
type: monitor.type,
interval: monitor.interval * MS_PER_MINUTE,
2024-06-27 17:26:00 -04:00
};
2024-07-31 15:53:01 -04:00
const { error } = monitorValidation.validate(form, {
2024-07-18 12:11:53 -07:00
abortEarly: false,
});
2024-07-18 12:11:27 -07:00
2024-07-18 12:11:53 -07:00
if (error) {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
setErrors(newErrors);
2024-07-31 15:53:01 -04:00
createToast({ body: "Error validation data." });
2024-07-18 12:11:53 -07:00
} else {
2024-07-31 15:53:01 -04:00
form = {
...form,
description: form.name,
2024-08-26 19:25:19 -07:00
teamId: user.teamId,
2024-07-18 12:11:53 -07:00
userId: user._id,
notifications: monitor.notifications,
2024-07-18 12:11:53 -07:00
};
2024-07-31 15:53:01 -04:00
const action = await dispatch(
createUptimeMonitor({ authToken, monitor: form })
);
if (action.meta.requestStatus === "fulfilled") {
createToast({ body: "Monitor created successfully!" });
navigate("/monitors");
} else {
2024-07-31 16:27:49 -04:00
createToast({ body: "Failed to create monitor." });
2024-07-18 12:11:53 -07:00
}
}
2024-06-27 17:26:00 -04:00
};
2024-06-27 16:44:56 -04:00
2024-06-27 17:26:00 -04:00
//select values
2024-07-24 14:09:24 -04:00
const frequencies = [
{ _id: 1, name: "1 minute" },
{ _id: 2, name: "2 minutes" },
{ _id: 3, name: "3 minutes" },
{ _id: 4, name: "4 minutes" },
{ _id: 5, name: "5 minutes" },
];
2024-06-25 13:22:28 +03:30
2024-06-18 22:39:58 +03:30
return (
2024-09-14 12:36:20 -04:00
<Box className="create-monitor">
2024-08-17 17:39:22 -04:00
<Breadcrumbs
list={[
{ name: "monitors", path: "/monitors" },
{ name: "create", path: `/monitors/create` },
2024-08-17 17:39:22 -04:00
]}
2024-07-07 18:25:03 -04:00
/>
2024-08-17 17:39:22 -04:00
<Stack
component="form"
className="create-monitor-form"
onSubmit={handleCreateMonitor}
noValidate
spellCheck="false"
2024-08-28 14:42:38 -04:00
gap={theme.spacing(12)}
mt={theme.spacing(6)}
>
2024-09-14 12:36:20 -04:00
<Typography component="h1" variant="h1">
<Typography component="span" fontSize="inherit">
Create your{" "}
</Typography>
2024-08-24 18:42:01 -04:00
<Typography
component="span"
2024-09-14 12:36:20 -04:00
variant="h2"
2024-08-24 18:42:01 -04:00
fontSize="inherit"
2024-09-14 12:36:20 -04:00
fontWeight="inherit"
2024-08-24 18:42:01 -04:00
>
monitor
</Typography>
</Typography>
2024-08-28 14:42:38 -04:00
<ConfigBox>
<Box>
<Typography component="h2">General settings</Typography>
2024-08-28 14:42:38 -04:00
<Typography component="p">
2024-06-18 22:39:58 +03:30
Here you can select the URL of the host, together with the type of
monitor.
</Typography>
</Box>
2024-08-28 14:42:38 -04:00
<Stack gap={theme.spacing(15)}>
<Field
2024-07-31 15:53:01 -04:00
type={monitor.type === "http" ? "url" : "text"}
id="monitor-url"
label="URL to monitor"
2024-07-31 15:53:01 -04:00
https={https}
placeholder="google.com"
2024-07-31 15:53:01 -04:00
value={monitor.url}
onChange={handleChange}
error={errors["url"]}
2024-06-18 22:39:58 +03:30
/>
<Field
2024-06-18 22:39:58 +03:30
type="text"
id="monitor-name"
label="Display name"
isOptional={true}
2024-06-18 22:39:58 +03:30
placeholder="Google"
2024-07-31 15:53:01 -04:00
value={monitor.name}
onChange={handleChange}
error={errors["name"]}
2024-06-18 22:39:58 +03:30
/>
</Stack>
2024-08-28 14:42:38 -04:00
</ConfigBox>
<ConfigBox>
<Box>
<Typography component="h2">Checks to perform</Typography>
2024-08-28 14:42:38 -04:00
<Typography component="p">
2024-06-18 22:39:58 +03:30
You can always add or remove checks after adding your site.
</Typography>
</Box>
2024-08-28 14:42:38 -04:00
<Stack gap={theme.spacing(12)}>
<Stack gap={theme.spacing(6)}>
2024-08-27 17:01:28 -04:00
<Radio
2024-07-26 21:57:18 -04:00
id="monitor-checks-http"
title="Website monitoring"
desc="Use HTTP(s) to monitor your website or API endpoint."
size="small"
value="http"
2024-07-31 15:53:01 -04:00
checked={monitor.type === "http"}
onChange={(event) => handleChange(event)}
2024-07-26 21:57:18 -04:00
/>
2024-07-31 15:53:01 -04:00
{monitor.type === "http" ? (
2024-09-14 12:36:20 -04:00
<ButtonGroup sx={{ ml: theme.spacing(16) }}>
2024-07-26 21:57:18 -04:00
<Button
2024-08-31 14:53:15 -04:00
variant="group"
filled={https.toString()}
onClick={() => setHttps(true)}
2024-08-31 14:53:15 -04:00
>
HTTPS
</Button>
2024-07-26 21:57:18 -04:00
<Button
2024-08-31 14:53:15 -04:00
variant="group"
filled={(!https).toString()}
onClick={() => setHttps(false)}
2024-08-31 14:53:15 -04:00
>
HTTP
</Button>
2024-07-26 21:57:18 -04:00
</ButtonGroup>
) : (
""
)}
</Stack>
2024-08-27 17:01:28 -04:00
<Radio
2024-06-27 16:29:50 -04:00
id="monitor-checks-ping"
2024-06-20 19:53:08 +03:30
title="Ping monitoring"
desc="Check whether your server is available or not."
size="small"
2024-06-25 13:22:28 +03:30
value="ping"
2024-07-31 15:53:01 -04:00
checked={monitor.type === "ping"}
onChange={(event) => handleChange(event)}
2024-06-20 19:53:08 +03:30
/>
2024-08-17 17:39:22 -04:00
{errors["type"] ? (
<Box className="error-container">
2024-08-28 14:42:38 -04:00
<Typography
component="p"
className="input-error"
color={theme.palette.error.text}
>
{errors["type"]}
</Typography>
2024-08-17 17:39:22 -04:00
</Box>
) : (
""
)}
</Stack>
2024-08-28 14:42:38 -04:00
</ConfigBox>
<ConfigBox>
<Box>
<Typography component="h2">Incident notifications</Typography>
2024-08-28 14:42:38 -04:00
<Typography component="p">
2024-07-31 15:53:01 -04:00
When there is an incident, notify users.
</Typography>
</Box>
2024-08-28 14:42:38 -04:00
<Stack gap={theme.spacing(6)}>
2024-08-17 17:39:22 -04:00
<Typography component="p">When there is a new incident,</Typography>
2024-07-31 15:53:01 -04:00
<Checkbox
id="notify-sms"
label="Notify via SMS (coming soon)"
isChecked={false}
value=""
onChange={() => logger.warn("disabled")}
2024-07-31 15:53:01 -04:00
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"
2024-07-31 15:53:01 -04:00
label="Also notify via email to multiple addresses (coming soon)"
isChecked={false}
value=""
onChange={() => logger.warn("disabled")}
2024-07-31 15:53:01 -04:00
isDisabled={true}
/>
{monitor.notifications.some(
(notification) => notification.type === "emails"
) ? (
2024-08-28 14:42:38 -04:00
<Box mx={theme.spacing(16)}>
<Field
id="notify-email-list"
type="text"
placeholder="name@gmail.com"
value=""
onChange={() => logger.warn("disabled")}
/>
2024-08-28 14:42:38 -04:00
<Typography mt={theme.spacing(4)}>
You can separate multiple emails with a comma
</Typography>
</Box>
) : (
""
)}
</Stack>
2024-08-28 14:42:38 -04:00
</ConfigBox>
<ConfigBox>
<Box>
<Typography component="h2">Advanced settings</Typography>
</Box>
2024-08-28 14:42:38 -04:00
<Stack gap={theme.spacing(12)}>
2024-07-24 14:09:24 -04:00
<Select
id="monitor-interval"
label="Check frequency"
2024-07-31 15:53:01 -04:00
value={monitor.interval || 1}
onChange={(event) => handleChange(event, "interval")}
2024-07-24 14:09:24 -04:00
items={frequencies}
/>
</Stack>
2024-08-28 14:42:38 -04:00
</ConfigBox>
<Stack direction="row" justifyContent="flex-end">
<Button
2024-08-31 14:53:15 -04:00
variant="contained"
color="primary"
onClick={handleCreateMonitor}
disabled={Object.keys(errors).length !== 0 && true}
2024-08-31 14:53:15 -04:00
>
Create monitor
</Button>
</Stack>
2024-08-17 17:39:22 -04:00
</Stack>
2024-07-26 12:39:18 -04:00
</Box>
2024-06-18 22:39:58 +03:30
);
};
2024-07-17 20:15:45 -04:00
export default CreateMonitor;