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

412 lines
13 KiB
React
Raw Normal View History

2024-06-25 13:22:28 +03:30
import "./index.css";
import React, { useState } from "react";
2024-07-17 20:15:45 -04:00
import RadioButton from "../../../Components/RadioButton";
import Button from "../../../Components/Button";
2024-07-26 21:57:18 -04:00
import { Box, 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";
import { useNavigate } from "react-router-dom";
import { useTheme } from "@emotion/react";
2024-07-07 18:25:03 -04:00
import WestRoundedIcon from "@mui/icons-material/WestRounded";
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-07-31 15:53:01 -04:00
import { createToast } from "../../../Utils/toastUtils";
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);
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-07-31 15:53:01 -04:00
const [monitor, setMonitor] = useState({
url: "",
name: "",
2024-06-27 17:26:00 -04:00
type: "",
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-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,
name: monitor.name === "" ? monitor.url : monitor.name,
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-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-07-26 12:39:18 -04:00
<Box className="create-monitor">
2024-07-07 18:25:03 -04:00
<Button
level="tertiary"
label="Back"
2024-07-30 19:36:37 -04:00
animate="slideLeft"
2024-07-07 18:25:03 -04:00
img={<WestRoundedIcon />}
onClick={() => navigate("/monitors")}
sx={{
2024-08-02 17:47:22 -04:00
backgroundColor: theme.palette.otherColors.fillGray,
2024-07-07 18:25:03 -04:00
mb: theme.gap.medium,
px: theme.gap.ml,
"& svg.MuiSvgIcon-root": {
2024-07-30 19:36:37 -04:00
mr: theme.gap.small,
fill: theme.palette.otherColors.slateGray,
2024-07-07 18:25:03 -04:00
},
}}
/>
<form
className="create-monitor-form"
onSubmit={handleCreateMonitor}
noValidate
spellCheck="false"
>
<Typography component="h1">Create new monitor</Typography>
<Stack className="config-box">
<Box>
<Typography component="h2">General settings</Typography>
<Typography component="p" mt={theme.gap.small}>
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>
<Stack gap={theme.gap.xl}>
<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="Friendly 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>
</Stack>
<Stack className="config-box">
<Box>
<Typography component="h2">Checks to perform</Typography>
<Typography component="p" mt={theme.gap.small}>
2024-06-18 22:39:58 +03:30
You can always add or remove checks after adding your site.
</Typography>
</Box>
<Stack gap={theme.gap.large}>
2024-07-26 21:57:18 -04:00
<Stack gap={theme.gap.medium}>
<RadioButton
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-07-26 21:57:18 -04:00
<ButtonGroup sx={{ ml: "32px" }}>
<Button
level="secondary"
label="HTTPS"
onClick={() => setHttps(true)}
2024-07-26 21:57:18 -04:00
sx={{
backgroundColor:
https && theme.palette.otherColors.fillGray,
2024-07-26 21:57:18 -04:00
}}
/>
<Button
level="secondary"
label="HTTP"
onClick={() => setHttps(false)}
2024-07-26 21:57:18 -04:00
sx={{
backgroundColor:
!https && theme.palette.otherColors.fillGray,
2024-07-26 21:57:18 -04:00
}}
/>
</ButtonGroup>
) : (
""
)}
</Stack>
2024-06-20 19:53:08 +03:30
<RadioButton
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
/>
<Box className="error-container">
{errors["type"] ? (
<Typography component="p" className="input-error">
{errors["type"]}
</Typography>
) : (
""
)}
</Box>
</Stack>
</Stack>
<Stack className="config-box">
<Box>
<Typography component="h2">Incident notifications</Typography>
<Typography component="p" mt={theme.gap.small}>
2024-07-31 15:53:01 -04:00
When there is an incident, notify users.
</Typography>
</Box>
2024-07-31 15:53:01 -04:00
<Stack gap={theme.gap.medium}>
<Typography component="p" mt={theme.gap.small}>
2024-06-25 13:22:28 +03:30
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={() => console.log("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={() => console.log("disabled")}
2024-07-31 15:53:01 -04:00
isDisabled={true}
/>
{monitor.notifications.some(
(notification) => notification.type === "emails"
) ? (
<Box mx={`calc(${theme.gap.ml} * 2)`}>
<Field
id="notify-email-list"
type="text"
placeholder="name@gmail.com"
value=""
onChange={() => console.log("disabled")}
/>
<Typography mt={theme.gap.small}>
You can separate multiple emails with a comma
</Typography>
</Box>
) : (
""
)}
</Stack>
</Stack>
<Stack className="config-box">
<Box>
<Typography component="h2">Advanced settings</Typography>
</Box>
<Stack gap={theme.gap.large}>
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}
/>
2024-06-27 16:44:56 -04:00
{/* TODO */}
{/* <FlexibileTextField
2024-06-27 16:29:50 -04:00
id="monitor-settings-retries"
title="Maximum retries before the service is marked as down"
2024-06-25 13:22:28 +03:30
type="number"
2024-06-27 16:29:50 -04:00
value={advancedSettings.retries}
onChange={(event) =>
handleChange(event, "retries", setAdvancedSettings)
}
2024-06-25 13:22:28 +03:30
/>
<FlexibileTextField
2024-06-27 16:29:50 -04:00
id="monitor-settings-codes"
title="Accepted status codes"
2024-06-25 13:22:28 +03:30
type="number"
2024-06-27 16:29:50 -04:00
value={advancedSettings.codes}
onChange={(event) =>
handleChange(event, "codes", setAdvancedSettings)
}
2024-06-25 13:22:28 +03:30
/>
<FlexibileTextField
2024-06-27 16:29:50 -04:00
id="monitor-settings-redirects"
title="Maximum redirects"
2024-06-25 13:22:28 +03:30
type="number"
2024-06-27 16:29:50 -04:00
value={advancedSettings.redirects}
onChange={(event) =>
handleChange(event, "redirects", setAdvancedSettings)
}
2024-06-27 16:44:56 -04:00
/> */}
</Stack>
</Stack>
{/* TODO */}
{/*
2024-06-18 22:39:58 +03:30
<ConfigBox
leftLayout={
<div className="config-box-desc">
<div className="config-box-desc-title">Proxy settings</div>
</div>
}
2024-06-25 13:22:28 +03:30
rightLayout={
<div className="proxy-setting-config">
<CustomizableCheckBox
2024-06-27 16:29:50 -04:00
id="monitor-proxy-enable"
2024-06-25 13:22:28 +03:30
title="Enable proxy"
2024-06-27 16:29:50 -04:00
isChecked={proxy.enabled}
handleChange={() => handleCheck("enabled", setProxy)}
2024-06-25 13:22:28 +03:30
/>
2024-06-27 16:29:50 -04:00
<FlexibileTextField
id="monitor-proxy-protocol"
title="Proxy protocol"
value={proxy.protocol}
onChange={(event) => handleChange(event, "protocol", setProxy)}
/>
<FlexibileTextField
id="monitor-proxy-address"
title="Proxy address"
value={proxy.address}
onChange={(event) => handleChange(event, "address", setProxy)}
/>
<FlexibileTextField
id="monitor-proxy-port"
title="Proxy port"
value={proxy.proxy_port}
onChange={(event) => handleChange(event, "proxy_port", setProxy)}
/>
2024-06-25 13:22:28 +03:30
</div>
}
2024-06-27 16:44:56 -04:00
/> */}
<Stack direction="row" justifyContent="flex-end">
<Button
id="create-new-monitor-btn"
level="primary"
label="Create new monitor"
onClick={handleCreateMonitor}
disabled={Object.keys(errors).length !== 0 && true}
/>
</Stack>
</form>
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;