From e940c4fd62830297762d5c1a7ad45269bb16eeba Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Wed, 31 Jul 2024 16:26:36 -0400 Subject: [PATCH] Added notification checkbox to monitor state --- .../Pages/Monitors/CreateMonitor/index.jsx | 80 +++++++++++++++---- 1 file changed, 64 insertions(+), 16 deletions(-) diff --git a/Client/src/Pages/Monitors/CreateMonitor/index.jsx b/Client/src/Pages/Monitors/CreateMonitor/index.jsx index 773d427e9..5281fb07f 100644 --- a/Client/src/Pages/Monitors/CreateMonitor/index.jsx +++ b/Client/src/Pages/Monitors/CreateMonitor/index.jsx @@ -26,8 +26,7 @@ const CreateMonitor = () => { "monitor-name": "name", "monitor-checks-http": "type", "monitor-checks-ping": "type", - "notify-sms": "sms", - "notify-email-default": "email", + "notify-email-default": "notification-email", }; const [monitor, setMonitor] = useState({ @@ -43,24 +42,57 @@ const CreateMonitor = () => { const handleChange = (event, name) => { const { value, id } = event.target; if (!name) name = idMap[id]; - setMonitor((prev) => ({ - ...prev, - [name]: value, - })); - const { error } = monitorValidation.validate( - { [name]: value }, - { abortEarly: false } - ); + 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, + })); - setErrors((prev) => { - const updatedErrors = { ...prev }; - if (error) updatedErrors[name] = error.details[0].message; - else delete updatedErrors[name]; - return updatedErrors; - }); + 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 handleNotifications = (event) => { + // const { value } = event.target; + // setMonitor((prev) => ({ ...prev, notifications: value })); + // }; + const handleCreateMonitor = async (event) => { event.preventDefault(); //obj to submit @@ -91,6 +123,7 @@ const CreateMonitor = () => { ...form, description: form.name, userId: user._id, + notifications: monitor.notifications, }; const action = await dispatch( createUptimeMonitor({ authToken, monitor: form }) @@ -113,6 +146,8 @@ const CreateMonitor = () => { { _id: 5, name: "5 minutes" }, ]; + console.log(monitor); + return (