format notifications to return object and added to validation list
This commit is contained in:
1 parent
a320967949
commit
9e773f6917
3 files changed
+32
-23
No files matched your search
@@ -96,9 +96,8 @@ export const getInfrastructureMonitorsByTeamId = createAsyncThunk(
|
||||
|
||||
export const updateInfrastructureMonitor = createAsyncThunk(
|
||||
"infrastructureMonitors/updateMonitor",
|
||||
async (data, thunkApi) => {
|
||||
async ({ monitorId, monitor }, thunkApi) => {
|
||||
try {
|
||||
const { monitor } = data;
|
||||
const updatedFields = {
|
||||
name: monitor.name,
|
||||
description: monitor.description,
|
||||
@@ -107,8 +106,9 @@ export const updateInfrastructureMonitor = createAsyncThunk(
|
||||
threshold: monitor.threshold,
|
||||
};
|
||||
const res = await networkService.updateMonitor({
|
||||
monitorId: monitor._id,
|
||||
updatedFields: updatedFields,
|
||||
monitorId,
|
||||
monitor,
|
||||
updatedFields,
|
||||
});
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
|
||||
@@ -70,6 +70,7 @@ const CreateInfrastructureMonitor = () => {
|
||||
url: "",
|
||||
name: "",
|
||||
notifications: [],
|
||||
notify_email: false,
|
||||
interval: 0.25,
|
||||
cpu: false,
|
||||
usage_cpu: "",
|
||||
@@ -89,7 +90,8 @@ const CreateInfrastructureMonitor = () => {
|
||||
setInfrastructureMonitor({
|
||||
url: monitor.url.replace(/^https?:\/\//, ""),
|
||||
name: monitor.name || "",
|
||||
notifications: monitor.notifications || [],
|
||||
notifications: monitor.notifications?.filter(n => typeof n === "object") || [],
|
||||
notify_email: (monitor.notifications?.length ?? 0) > 0,
|
||||
interval: monitor.interval / MS_PER_MINUTE,
|
||||
cpu: monitor.cpu || false,
|
||||
usage_cpu: monitor.usage_cpu || "",
|
||||
@@ -108,6 +110,14 @@ const CreateInfrastructureMonitor = () => {
|
||||
const handleCreateInfrastructureMonitor = async (event) => {
|
||||
event.preventDefault();
|
||||
|
||||
const formattedNotifications = infrastructureMonitor.notifications.map((n) =>
|
||||
typeof n === "string" ? { type: "email", address: n } : n
|
||||
);
|
||||
|
||||
if (infrastructureMonitor.notify_email) {
|
||||
formattedNotifications.push({ type: "email", address: user.email });
|
||||
}
|
||||
|
||||
// Build the form
|
||||
let form = {
|
||||
url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
|
||||
@@ -133,6 +143,7 @@ const CreateInfrastructureMonitor = () => {
|
||||
? { usage_temperature: infrastructureMonitor.usage_temperature }
|
||||
: {}),
|
||||
secret: infrastructureMonitor.secret,
|
||||
notifications: formattedNotifications,
|
||||
};
|
||||
|
||||
const { error } = infrastructureMonitorValidation.validate(form, {
|
||||
@@ -220,30 +231,24 @@ const CreateInfrastructureMonitor = () => {
|
||||
};
|
||||
|
||||
const handleNotifications = (event, type) => {
|
||||
const { value } = event.target;
|
||||
const { value, checked } = event.target;
|
||||
let notifications = [...infrastructureMonitor.notifications];
|
||||
const notificationExists = notifications.some((notification) => {
|
||||
if (notification.type === type && notification.address === value) {
|
||||
return true;
|
||||
|
||||
if (checked) {
|
||||
if (!notifications.some((n) => n.type === type && n.address === value)) {
|
||||
notifications.push({ type, address: value });
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (notificationExists) {
|
||||
notifications = notifications.filter((notification) => {
|
||||
if (notification.type === type && notification.address === value) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
} else {
|
||||
notifications.push({ type, address: value });
|
||||
notifications = notifications.filter((n) => !(n.type === type && n.address === value));
|
||||
}
|
||||
|
||||
|
||||
setInfrastructureMonitor((prev) => ({
|
||||
...prev,
|
||||
notifications,
|
||||
notify_email: checked,
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
return (
|
||||
<Box className="create-infrastructure-monitor">
|
||||
@@ -363,9 +368,7 @@ const CreateInfrastructureMonitor = () => {
|
||||
<Checkbox
|
||||
id="notify-email-default"
|
||||
label={`Notify via email (to ${user.email})`}
|
||||
isChecked={infrastructureMonitor.notifications.some(
|
||||
(notification) => notification.type === "email"
|
||||
)}
|
||||
isChecked={infrastructureMonitor.notify_email}
|
||||
value={user?.email}
|
||||
onChange={(event) => handleNotifications(event, "email")}
|
||||
/>
|
||||
|
||||
@@ -349,6 +349,12 @@ const infrastructureMonitorValidation = joi.object({
|
||||
"number.base": "Frequency must be a number.",
|
||||
"any.required": "Frequency is required.",
|
||||
}),
|
||||
notifications: joi.array().items(
|
||||
joi.object({
|
||||
type: joi.string().valid("email").required(),
|
||||
address: joi.string().email({ tlds: { allow: false } }).required(),
|
||||
})
|
||||
)
|
||||
});
|
||||
|
||||
export {
|
||||
|
||||
Reference in new issue
Block a user