Added notification checkbox to monitor state
This commit is contained in:
1 parent
01854dd5e7
commit
e940c4fd62
1 file changed
+64
-16
@@ -26,8 +26,7 @@ const CreateMonitor = () => {
|
|||||||
"monitor-name": "name",
|
"monitor-name": "name",
|
||||||
"monitor-checks-http": "type",
|
"monitor-checks-http": "type",
|
||||||
"monitor-checks-ping": "type",
|
"monitor-checks-ping": "type",
|
||||||
"notify-sms": "sms",
|
"notify-email-default": "notification-email",
|
||||||
"notify-email-default": "email",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const [monitor, setMonitor] = useState({
|
const [monitor, setMonitor] = useState({
|
||||||
@@ -43,24 +42,57 @@ const CreateMonitor = () => {
|
|||||||
const handleChange = (event, name) => {
|
const handleChange = (event, name) => {
|
||||||
const { value, id } = event.target;
|
const { value, id } = event.target;
|
||||||
if (!name) name = idMap[id];
|
if (!name) name = idMap[id];
|
||||||
setMonitor((prev) => ({
|
|
||||||
...prev,
|
|
||||||
[name]: value,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { error } = monitorValidation.validate(
|
if (name.includes("notification-")) {
|
||||||
{ [name]: value },
|
name = name.replace("notification-", "");
|
||||||
{ abortEarly: false }
|
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 { error } = monitorValidation.validate(
|
||||||
const updatedErrors = { ...prev };
|
{ [name]: value },
|
||||||
if (error) updatedErrors[name] = error.details[0].message;
|
{ abortEarly: false }
|
||||||
else delete updatedErrors[name];
|
);
|
||||||
return updatedErrors;
|
|
||||||
});
|
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) => {
|
const handleCreateMonitor = async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
//obj to submit
|
//obj to submit
|
||||||
@@ -91,6 +123,7 @@ const CreateMonitor = () => {
|
|||||||
...form,
|
...form,
|
||||||
description: form.name,
|
description: form.name,
|
||||||
userId: user._id,
|
userId: user._id,
|
||||||
|
notifications: monitor.notifications,
|
||||||
};
|
};
|
||||||
const action = await dispatch(
|
const action = await dispatch(
|
||||||
createUptimeMonitor({ authToken, monitor: form })
|
createUptimeMonitor({ authToken, monitor: form })
|
||||||
@@ -113,6 +146,8 @@ const CreateMonitor = () => {
|
|||||||
{ _id: 5, name: "5 minutes" },
|
{ _id: 5, name: "5 minutes" },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
console.log(monitor);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className="create-monitor">
|
<Box className="create-monitor">
|
||||||
<Button
|
<Button
|
||||||
@@ -272,15 +307,26 @@ const CreateMonitor = () => {
|
|||||||
<Checkbox
|
<Checkbox
|
||||||
id="notify-sms"
|
id="notify-sms"
|
||||||
label="Notify via SMS (coming soon)"
|
label="Notify via SMS (coming soon)"
|
||||||
|
isChecked={false}
|
||||||
|
value=""
|
||||||
|
onChange={() => console.log("disabled")}
|
||||||
isDisabled={true}
|
isDisabled={true}
|
||||||
/>
|
/>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
id="notify-email-default"
|
id="notify-email-default"
|
||||||
label={`Notify via email (to ${user.email})`}
|
label={`Notify via email (to ${user.email})`}
|
||||||
|
isChecked={monitor.notifications.some(
|
||||||
|
(notification) => notification.type === "email"
|
||||||
|
)}
|
||||||
|
value={user?.email}
|
||||||
|
onChange={(event) => handleChange(event)}
|
||||||
/>
|
/>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
id="notify-email"
|
id="notify-email"
|
||||||
label="Also notify via email to multiple addresses (coming soon)"
|
label="Also notify via email to multiple addresses (coming soon)"
|
||||||
|
isChecked={false}
|
||||||
|
value=""
|
||||||
|
onChange={() => console.log("disabled")}
|
||||||
isDisabled={true}
|
isDisabled={true}
|
||||||
/>
|
/>
|
||||||
<Box mx={`calc(${theme.gap.ml} * 2)`}>
|
<Box mx={`calc(${theme.gap.ml} * 2)`}>
|
||||||
@@ -288,6 +334,8 @@ const CreateMonitor = () => {
|
|||||||
id="notify-email-list"
|
id="notify-email-list"
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="name@gmail.com"
|
placeholder="name@gmail.com"
|
||||||
|
value=""
|
||||||
|
onChange={() => console.log("disabled")}
|
||||||
/>
|
/>
|
||||||
<Typography mt={theme.gap.small}>
|
<Typography mt={theme.gap.small}>
|
||||||
You can separate multiple emails with a comma
|
You can separate multiple emails with a comma
|
||||||
|
|||||||
Reference in new issue
Block a user