From d0406815d0faf9b7e7dab9499ba62fc73f3d3479 Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Wed, 31 Jul 2024 18:39:44 -0400 Subject: [PATCH] Added notif checkboxes to configure page --- Client/src/Pages/Monitors/Configure/index.css | 3 + Client/src/Pages/Monitors/Configure/index.jsx | 158 +++++++++++++++--- 2 files changed, 134 insertions(+), 27 deletions(-) diff --git a/Client/src/Pages/Monitors/Configure/index.css b/Client/src/Pages/Monitors/Configure/index.css index 005c97188..ab52b028a 100644 --- a/Client/src/Pages/Monitors/Configure/index.css +++ b/Client/src/Pages/Monitors/Configure/index.css @@ -51,6 +51,9 @@ position: absolute; top: 100%; } +.configure-monitor .MuiInputBase-root:has(> .Mui-disabled){ + background-color: var(--env-var-color-13); +} .MuiInputBase-root:has(#monitor-interval) { height: 34px; diff --git a/Client/src/Pages/Monitors/Configure/index.jsx b/Client/src/Pages/Monitors/Configure/index.jsx index 9a2f44d5f..31faf2c63 100644 --- a/Client/src/Pages/Monitors/Configure/index.jsx +++ b/Client/src/Pages/Monitors/Configure/index.jsx @@ -20,6 +20,8 @@ import { getUptimeMonitorsByUserId, deleteUptimeMonitor, } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice"; +import Checkbox from "../../../Components/Inputs/Checkbox"; + /** * Parses a URL string and returns a URL object. * @@ -55,7 +57,7 @@ const Configure = () => { const navigate = useNavigate(); const theme = useTheme(); const dispatch = useDispatch(); - const { authToken } = useSelector((state) => state.auth); + const { user, authToken } = useSelector((state) => state.auth); const { monitors } = useSelector((state) => state.uptimeMonitors); const [monitor, setMonitor] = useState(); const [duration, setDuration] = useState(0); @@ -67,6 +69,7 @@ const Configure = () => { "monitor-name": "name", "monitor-checks-http": "type", "monitor-checks-ping": "type", + "notify-email-default": "notification-email", }; useEffect(() => { @@ -80,27 +83,55 @@ const Configure = () => { const handleChange = (event, name) => { let { value, id } = event.target; if (!name) name = idMap[id]; - if (name === "interval") { - value = value * MS_PER_MINUTE; + + 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 { + if (name === "interval") { + value = value * MS_PER_MINUTE; + } + setMonitor((prev) => ({ + ...prev, + [name]: value, + })); + + const validation = monitorValidation.validate( + { [name]: value }, + { abortEarly: false } + ); + + setErrors((prev) => { + const updatedErrors = { ...prev }; + + if (validation.error) + updatedErrors[name] = validation.error.details[0].message; + else delete updatedErrors[name]; + return updatedErrors; + }); } - setMonitor((prev) => ({ - ...prev, - [name]: value, - })); - - const validation = monitorValidation.validate( - { [name]: value }, - { abortEarly: false } - ); - - setErrors((prev) => { - const updatedErrors = { ...prev }; - - if (validation.error) - updatedErrors[name] = validation.error.details[0].message; - else delete updatedErrors[name]; - return updatedErrors; - }); }; const handleSubmit = async (event) => { @@ -139,6 +170,8 @@ const Configure = () => { const parsedUrl = parseUrl(monitor?.url); const protocol = parsedUrl?.protocol?.replace(":", "") || ""; + var lastChecked = formatDurationRounded(getLastChecked(monitor?.checks)); + return (