import { useState, useEffect } from "react"; import { Box, ButtonGroup, Stack, Typography } from "@mui/material"; import { useSelector, useDispatch } from "react-redux"; import { monitorValidation } from "../../../Validation/validation"; import { createUptimeMonitor } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice"; import { useNavigate, useParams } from "react-router-dom"; import { useTheme } from "@emotion/react"; import { createToast } from "../../../Utils/toastUtils"; import { logger } from "../../../Utils/Logger"; import { ConfigBox } from "../styled"; import Radio from "../../../Components/Inputs/Radio"; import Button from "../../../Components/Button"; import Field from "../../../Components/Inputs/Field"; import Select from "../../../Components/Inputs/Select"; import Checkbox from "../../../Components/Inputs/Checkbox"; import Breadcrumbs from "../../../Components/Breadcrumbs"; import "./index.css"; const CreateMonitor = () => { const MS_PER_MINUTE = 60000; const { user, authToken } = useSelector((state) => state.auth); const { monitors } = useSelector((state) => state.uptimeMonitors); const dispatch = useDispatch(); const navigate = useNavigate(); const theme = useTheme(); const idMap = { "monitor-url": "url", "monitor-name": "name", "monitor-checks-http": "type", "monitor-checks-ping": "type", "notify-email-default": "notification-email", }; const { monitorId } = useParams(); const [monitor, setMonitor] = useState({ url: "", name: "", type: "http", notifications: [], interval: 1, }); const [https, setHttps] = useState(true); const [errors, setErrors] = useState({}); useEffect(() => { if(monitorId){ const data = monitors.find((monitor) => monitor._id === monitorId); if (!data) { console.error("Error fetching monitor of id: " + monitorId); navigate("/not-found", {replace: true}); } const {name, ...rest} = data; //data.name is read-only if(rest.type == 'http') rest.url = rest.url.substring(8, rest.url.length) rest.name = `${name} (Clone)`; rest.interval /= 60000; setMonitor({ ...rest, }); } }, [monitorId, authToken, monitors]); const handleChange = (event, name) => { const { value, id } = event.target; if (!name) name = idMap[id]; 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, })); 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; }); } }; const handleCreateMonitor = async (event) => { event.preventDefault(); //obj to submit let form = { url: //preprending protocol for url 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, }; const { error } = monitorValidation.validate(form, { abortEarly: false, }); if (error) { const newErrors = {}; error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); setErrors(newErrors); createToast({ body: "Error validation data." }); } else { form = { ...form, description: form.name, teamId: user.teamId, userId: user._id, notifications: monitor.notifications, }; const action = await dispatch( createUptimeMonitor({ authToken, monitor: form }) ); if (action.meta.requestStatus === "fulfilled") { createToast({ body: "Monitor created successfully!" }); navigate("/monitors"); } else { createToast({ body: "Failed to create monitor." }); } } }; //select values 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" }, ]; return ( Create your{" "} monitor General settings Here you can select the URL of the host, together with the type of monitor. Checks to perform You can always add or remove checks after adding your site. handleChange(event)} /> {monitor.type === "http" ? (