import "./index.css"; import ConfigBox from "../../Components/ConfigBox"; import FlexibileTextField from "../../Components/TextFields/Flexibile/FlexibileTextField"; import React, { useState } from "react"; import RadioButton from "../../Components/RadioButton"; import CustomizableCheckBox from "../../Components/Checkbox/CustomizableCheckbox"; import Button from "../../Components/Button"; import { Box, MenuItem, Select, Stack, Typography } from "@mui/material"; import { useSelector, useDispatch } from "react-redux"; import { createMonitorValidation } from "../../Validation/validation"; import { createMonitor } from "../../Features/Monitors/monitorsSlice"; import { useNavigate } from "react-router-dom"; import { useTheme } from "@emotion/react"; import WestRoundedIcon from "@mui/icons-material/WestRounded"; const CreateNewMonitor = () => { const MS_PER_MINUTE = 60000; const { user, authToken } = useSelector((state) => state.auth); const dispatch = useDispatch(); const navigate = useNavigate(); const theme = useTheme(); const [errors, setErrors] = useState({}); //General Settings Form const [generalSettings, setGeneralSettings] = useState({ url: "", name: "" }); //Checks Form const [checks, setChecks] = useState({ type: "", // port: "" }); //Incidents Form // const [notifications, setNotifications] = useState({ // viaSms: false, // viaEmail: false, // viaOther: false, // email: "", // }); //Advanced Settings Form const [advancedSettings, setAdvancedSettings] = useState({ frequency: 1, // retries: "", // codes: "", // redirects: "", }); //Proxy Settings Form // const [proxy, setProxy] = useState({ // enabled: false, // protocol: "", // address: "", // proxy_port: "", // }); const handleChange = (event, id, setState, checkbox) => { const { value } = event.target; setState((prev) => ({ ...prev, [id]: checkbox ? true : value, })); const validation = createMonitorValidation.validate( { [id]: value }, { abortEarly: false } ); setErrors((prev) => { const updatedErrors = { ...prev }; if (validation.error) { updatedErrors[id] = validation.error.details[0].message; } else { delete updatedErrors[id]; } return updatedErrors; }); }; // const handleCheck = (id, setState) => { // setState((prev) => ({ // ...prev, // [id]: !prev[id], // })); // }; const handleCreateNewMonitor = async (event) => { event.preventDefault(); //obj to submit let monitor = { ...generalSettings, ...checks, }; const { error } = createMonitorValidation.validate(monitor, { abortEarly: false, }); if (error) { const newErrors = {}; error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); setErrors(newErrors); } else { monitor = { ...monitor, description: monitor.name, userId: user._id, // ...advancedSettings, // TODO frequency should be interval, then we can use spread interval: advancedSettings.frequency * MS_PER_MINUTE, }; try { const action = await dispatch(createMonitor({ authToken, monitor })); if (action.meta.requestStatus === "fulfilled") { navigate("/monitors"); } } catch (error) { alert(error); } } }; //select values // const ports = ["Port 1", "Port 2", "Port 3"]; const frequencies = [1, 2, 3, 4, 5]; return (
); }; export default CreateNewMonitor;