simplify creatign notifications
This commit is contained in:
1 parent
a86bc59e09
commit
440ce8a12b
4 files changed
+67
-298
No files matched your search
@@ -17,15 +17,17 @@ import {
|
|||||||
useEditNotification,
|
useEditNotification,
|
||||||
useTestNotification,
|
useTestNotification,
|
||||||
} from "../../../Hooks/useNotifications";
|
} from "../../../Hooks/useNotifications";
|
||||||
import {
|
import { notificationValidation } from "../../../Validation/validation";
|
||||||
notificationEmailValidation,
|
|
||||||
notificationWebhookValidation,
|
|
||||||
notificationPagerDutyValidation,
|
|
||||||
} from "../../../Validation/validation";
|
|
||||||
import { createToast } from "../../../Utils/toastUtils";
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import { NOTIFICATION_TYPES } from "../utils";
|
import {
|
||||||
|
NOTIFICATION_TYPES,
|
||||||
|
TITLE_MAP,
|
||||||
|
DESCRIPTION_MAP,
|
||||||
|
LABEL_MAP,
|
||||||
|
PLACEHOLDER_MAP,
|
||||||
|
} from "../utils";
|
||||||
|
|
||||||
// Setup
|
// Setup
|
||||||
|
|
||||||
@@ -49,11 +51,6 @@ const CreateNotifications = () => {
|
|||||||
notificationName: "",
|
notificationName: "",
|
||||||
address: "",
|
address: "",
|
||||||
type: NOTIFICATION_TYPES[0]._id,
|
type: NOTIFICATION_TYPES[0]._id,
|
||||||
config: {
|
|
||||||
webhookUrl: "",
|
|
||||||
platform: "",
|
|
||||||
routingKey: "",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -71,42 +68,17 @@ const CreateNotifications = () => {
|
|||||||
type: NOTIFICATION_TYPES.find((type) => type._id === notification.type).value,
|
type: NOTIFICATION_TYPES.find((type) => type._id === notification.type).value,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (form.type === "slack" || form.type === "discord") {
|
|
||||||
form.type = "webhook";
|
|
||||||
}
|
|
||||||
|
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
if (form.type === "email") {
|
error = notificationValidation.validate(form, { abortEarly: false }).error;
|
||||||
error = notificationEmailValidation.validate(
|
|
||||||
{ notificationName: form.notificationName, address: form.address },
|
|
||||||
{ abortEarly: false }
|
|
||||||
).error;
|
|
||||||
} else if (form.type === "webhook") {
|
|
||||||
form.config = {
|
|
||||||
platform: form.config.platform,
|
|
||||||
webhookUrl: form.config.webhookUrl,
|
|
||||||
};
|
|
||||||
error = notificationWebhookValidation.validate(
|
|
||||||
{ notificationName: form.notificationName, config: form.config },
|
|
||||||
{ abortEarly: false }
|
|
||||||
).error;
|
|
||||||
} else if (form.type === "pager_duty") {
|
|
||||||
form.config = {
|
|
||||||
platform: form.config.platform,
|
|
||||||
routingKey: form.config.routingKey,
|
|
||||||
};
|
|
||||||
error = notificationPagerDutyValidation.validate(
|
|
||||||
{ notificationName: form.notificationName, config: form.config },
|
|
||||||
{ abortEarly: false }
|
|
||||||
).error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
const newErrors = {};
|
const newErrors = {};
|
||||||
error.details.forEach((err) => {
|
error.details.forEach((err) => {
|
||||||
newErrors[err.path[0]] = err.message;
|
newErrors[err.path[0]] = err.message;
|
||||||
});
|
});
|
||||||
|
console.log(JSON.stringify(newErrors));
|
||||||
|
console.log(JSON.stringify(form, null, 2));
|
||||||
createToast({ body: "Please check the form for errors." });
|
createToast({ body: "Please check the form for errors." });
|
||||||
setErrors(newErrors);
|
setErrors(newErrors);
|
||||||
return;
|
return;
|
||||||
@@ -124,63 +96,10 @@ const CreateNotifications = () => {
|
|||||||
|
|
||||||
const newNotification = { ...notification, [name]: value };
|
const newNotification = { ...notification, [name]: value };
|
||||||
|
|
||||||
// Handle config/platform initialization if type is webhook
|
const { error } = notificationValidation.extract(name).validate(value);
|
||||||
|
|
||||||
const type = NOTIFICATION_TYPES.find(
|
|
||||||
(type) => type._id === newNotification.type
|
|
||||||
).value;
|
|
||||||
|
|
||||||
if (type === "email") {
|
|
||||||
newNotification.config = null;
|
|
||||||
} else if (type === "slack" || type === "webhook" || type === "discord") {
|
|
||||||
newNotification.address = "";
|
|
||||||
newNotification.config = newNotification.config || {};
|
|
||||||
if (name === "config") {
|
|
||||||
newNotification.config = value;
|
|
||||||
}
|
|
||||||
if (type === "webhook") {
|
|
||||||
newNotification.config.platform = "webhook";
|
|
||||||
}
|
|
||||||
if (type === "slack") {
|
|
||||||
newNotification.config.platform = "slack";
|
|
||||||
}
|
|
||||||
if (type === "discord") {
|
|
||||||
newNotification.config.platform = "discord";
|
|
||||||
}
|
|
||||||
} else if (type === "pager_duty") {
|
|
||||||
newNotification.config = newNotification.config || {};
|
|
||||||
if (name === "config") {
|
|
||||||
newNotification.config = value;
|
|
||||||
}
|
|
||||||
newNotification.config.platform = "pager_duty";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Field-level validation
|
|
||||||
let fieldError;
|
|
||||||
|
|
||||||
if (name === "notificationName") {
|
|
||||||
const { error } = notificationEmailValidation.extract(name).validate(value);
|
|
||||||
fieldError = error?.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === "email" && name === "address") {
|
|
||||||
const { error } = notificationEmailValidation.extract(name).validate(value);
|
|
||||||
fieldError = error?.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
(type === "slack" || type === "webhook" || type === "discord") &&
|
|
||||||
name === "config"
|
|
||||||
) {
|
|
||||||
// Validate only webhookUrl inside config
|
|
||||||
const { error } = notificationWebhookValidation.extract("config").validate(value);
|
|
||||||
fieldError = error?.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set field-level error
|
|
||||||
setErrors((prev) => ({
|
setErrors((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
[name]: fieldError,
|
[name]: error?.message,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
setNotification(newNotification);
|
setNotification(newNotification);
|
||||||
@@ -192,36 +111,9 @@ const CreateNotifications = () => {
|
|||||||
type: NOTIFICATION_TYPES.find((type) => type._id === notification.type).value,
|
type: NOTIFICATION_TYPES.find((type) => type._id === notification.type).value,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (form.type === "slack" || form.type === "discord") {
|
|
||||||
form.type = "webhook";
|
|
||||||
}
|
|
||||||
|
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
if (form.type === "email") {
|
error = notificationValidation.validate(form, { abortEarly: false }).error;
|
||||||
error = notificationEmailValidation.validate(
|
|
||||||
{ notificationName: form.notificationName, address: form.address },
|
|
||||||
{ abortEarly: false }
|
|
||||||
).error;
|
|
||||||
} else if (form.type === "webhook") {
|
|
||||||
form.config = {
|
|
||||||
platform: form.config.platform,
|
|
||||||
webhookUrl: form.config.webhookUrl,
|
|
||||||
};
|
|
||||||
error = notificationWebhookValidation.validate(
|
|
||||||
{ notificationName: form.notificationName, config: form.config },
|
|
||||||
{ abortEarly: false }
|
|
||||||
).error;
|
|
||||||
} else if (form.type === "pager_duty") {
|
|
||||||
form.config = {
|
|
||||||
platform: form.config.platform,
|
|
||||||
routingKey: form.config.routingKey,
|
|
||||||
};
|
|
||||||
error = notificationPagerDutyValidation.validate(
|
|
||||||
{ notificationName: form.notificationName, config: form.config },
|
|
||||||
{ abortEarly: false }
|
|
||||||
).error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
const newErrors = {};
|
const newErrors = {};
|
||||||
@@ -236,6 +128,7 @@ const CreateNotifications = () => {
|
|||||||
testNotification(form);
|
testNotification(form);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const type = NOTIFICATION_TYPES.find((type) => type._id === notification.type).value;
|
||||||
return (
|
return (
|
||||||
<Stack gap={theme.spacing(10)}>
|
<Stack gap={theme.spacing(10)}>
|
||||||
<Breadcrumbs list={BREADCRUMBS} />
|
<Breadcrumbs list={BREADCRUMBS} />
|
||||||
@@ -287,164 +180,24 @@ const CreateNotifications = () => {
|
|||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
{notification.type === 1 && (
|
<ConfigBox>
|
||||||
<ConfigBox>
|
<Box>
|
||||||
<Box>
|
<Typography component="h2">{t(TITLE_MAP[type])}</Typography>
|
||||||
<Typography component="h2">
|
<Typography component="p">{t(DESCRIPTION_MAP[type])}</Typography>
|
||||||
{t("createNotifications.emailSettings.title")}
|
</Box>
|
||||||
</Typography>
|
<Stack gap={theme.spacing(12)}>
|
||||||
<Typography component="p">
|
<TextInput
|
||||||
{t("createNotifications.emailSettings.description")}
|
label={t(LABEL_MAP[type])}
|
||||||
</Typography>
|
name="address"
|
||||||
</Box>
|
placeholder={t(PLACEHOLDER_MAP[type])}
|
||||||
<Stack gap={theme.spacing(12)}>
|
value={notification.address}
|
||||||
<TextInput
|
onChange={onChange}
|
||||||
label={t("createNotifications.emailSettings.emailLabel")}
|
error={Boolean(errors.address)}
|
||||||
name="address"
|
helperText={errors["address"]}
|
||||||
placeholder={t("createNotifications.emailSettings.emailPlaceholder")}
|
/>
|
||||||
value={notification.address}
|
</Stack>
|
||||||
onChange={onChange}
|
</ConfigBox>
|
||||||
error={Boolean(errors.address)}
|
|
||||||
helperText={errors["address"]}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
</ConfigBox>
|
|
||||||
)}
|
|
||||||
{notification.type === 2 && (
|
|
||||||
<ConfigBox>
|
|
||||||
<Box>
|
|
||||||
<Typography component="h2">
|
|
||||||
{t("createNotifications.slackSettings.title")}
|
|
||||||
</Typography>
|
|
||||||
<Typography component="p">
|
|
||||||
{t("createNotifications.slackSettings.description")}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
<Stack gap={theme.spacing(12)}>
|
|
||||||
<TextInput
|
|
||||||
label={t("createNotifications.slackSettings.webhookLabel")}
|
|
||||||
value={notification.config.webhookUrl || ""}
|
|
||||||
error={Boolean(errors.config)}
|
|
||||||
helperText={errors["config"]}
|
|
||||||
onChange={(e) => {
|
|
||||||
const updatedConfig = {
|
|
||||||
...notification.config,
|
|
||||||
webhookUrl: e.target.value,
|
|
||||||
};
|
|
||||||
|
|
||||||
onChange({
|
|
||||||
target: {
|
|
||||||
name: "config",
|
|
||||||
value: updatedConfig,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
</ConfigBox>
|
|
||||||
)}
|
|
||||||
{notification.type === 3 && (
|
|
||||||
<ConfigBox>
|
|
||||||
<Box>
|
|
||||||
<Typography component="h2">
|
|
||||||
{t("createNotifications.pagerdutySettings.title")}
|
|
||||||
</Typography>
|
|
||||||
<Typography component="p">
|
|
||||||
{t("createNotifications.pagerdutySettings.description")}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
<Stack gap={theme.spacing(12)}>
|
|
||||||
<TextInput
|
|
||||||
label={t("createNotifications.pagerdutySettings.integrationKeyLabel")}
|
|
||||||
placeholder={t(
|
|
||||||
"createNotifications.pagerdutySettings.integrationKeyPlaceholder"
|
|
||||||
)}
|
|
||||||
value={notification.config.routingKey || ""}
|
|
||||||
error={Boolean(errors.config)}
|
|
||||||
helperText={errors["config"]}
|
|
||||||
onChange={(e) => {
|
|
||||||
const updatedConfig = {
|
|
||||||
...notification.config,
|
|
||||||
routingKey: e.target.value,
|
|
||||||
};
|
|
||||||
|
|
||||||
onChange({
|
|
||||||
target: {
|
|
||||||
name: "config",
|
|
||||||
value: updatedConfig,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
</ConfigBox>
|
|
||||||
)}
|
|
||||||
{notification.type === 4 && (
|
|
||||||
<ConfigBox>
|
|
||||||
<Box>
|
|
||||||
<Typography component="h2">
|
|
||||||
{t("createNotifications.genericWebhookSettings.title")}
|
|
||||||
</Typography>
|
|
||||||
<Typography component="p">
|
|
||||||
{t("createNotifications.genericWebhookSettings.description")}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
<Stack gap={theme.spacing(12)}>
|
|
||||||
<TextInput
|
|
||||||
label={t("createNotifications.genericWebhookSettings.webhookLabel")}
|
|
||||||
value={notification.config.webhookUrl || ""}
|
|
||||||
error={Boolean(errors.config)}
|
|
||||||
helperText={errors["config"]}
|
|
||||||
onChange={(e) => {
|
|
||||||
const updatedConfig = {
|
|
||||||
...notification.config,
|
|
||||||
webhookUrl: e.target.value,
|
|
||||||
};
|
|
||||||
|
|
||||||
onChange({
|
|
||||||
target: {
|
|
||||||
name: "config",
|
|
||||||
value: updatedConfig,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
</ConfigBox>
|
|
||||||
)}
|
|
||||||
{notification.type === 5 && (
|
|
||||||
<ConfigBox>
|
|
||||||
<Box>
|
|
||||||
<Typography component="h2">
|
|
||||||
{t("createNotifications.discordSettings.title")}
|
|
||||||
</Typography>
|
|
||||||
<Typography component="p">
|
|
||||||
{t("createNotifications.discordSettings.description")}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
<Stack gap={theme.spacing(12)}>
|
|
||||||
<TextInput
|
|
||||||
label={t("createNotifications.discordSettings.webhookLabel")}
|
|
||||||
value={notification.config.webhookUrl || ""}
|
|
||||||
error={Boolean(errors.config)}
|
|
||||||
helperText={errors["config"]}
|
|
||||||
onChange={(e) => {
|
|
||||||
const updatedConfig = {
|
|
||||||
...notification.config,
|
|
||||||
webhookUrl: e.target.value,
|
|
||||||
};
|
|
||||||
|
|
||||||
onChange({
|
|
||||||
target: {
|
|
||||||
name: "config",
|
|
||||||
value: updatedConfig,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
</ConfigBox>
|
|
||||||
)}
|
|
||||||
<Stack
|
<Stack
|
||||||
direction="row"
|
direction="row"
|
||||||
justifyContent="flex-end"
|
justifyContent="flex-end"
|
||||||
|
|||||||
@@ -44,27 +44,11 @@ const Notifications = () => {
|
|||||||
return row.notificationName;
|
return row.notificationName;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
id: "type",
|
|
||||||
content: "Type",
|
|
||||||
render: (row) => {
|
|
||||||
return row.type;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
id: "target",
|
id: "target",
|
||||||
content: "Target",
|
content: "Target",
|
||||||
render: (row) => {
|
render: (row) => {
|
||||||
const type = row.type;
|
return row.address;
|
||||||
if (type === "email") {
|
|
||||||
return row.address;
|
|
||||||
}
|
|
||||||
if (type === "webhook") {
|
|
||||||
return row.config?.webhookUrl;
|
|
||||||
}
|
|
||||||
if (type === "pager_duty") {
|
|
||||||
return row.config?.routingKey;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,3 +5,35 @@ export const NOTIFICATION_TYPES = [
|
|||||||
{ _id: 4, name: "Webhook", value: "webhook" },
|
{ _id: 4, name: "Webhook", value: "webhook" },
|
||||||
{ _id: 5, name: "Discord", value: "discord" },
|
{ _id: 5, name: "Discord", value: "discord" },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export const TITLE_MAP = {
|
||||||
|
email: "createNotifications.emailSettings.title",
|
||||||
|
slack: "createNotifications.slackSettings.title",
|
||||||
|
pager_duty: "createNotifications.pagerDutySettings.title",
|
||||||
|
webhook: "createNotifications.webhookSettings.title",
|
||||||
|
discord: "createNotifications.discordSettings.title",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const DESCRIPTION_MAP = {
|
||||||
|
email: "createNotifications.emailSettings.description",
|
||||||
|
slack: "createNotifications.slackSettings.description",
|
||||||
|
pager_duty: "createNotifications.pagerDutySettings.description",
|
||||||
|
webhook: "createNotifications.webhookSettings.description",
|
||||||
|
discord: "createNotifications.discordSettings.description",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const LABEL_MAP = {
|
||||||
|
email: "createNotifications.emailSettings.emailLabel",
|
||||||
|
slack: "createNotifications.slackSettings.webhookLabel",
|
||||||
|
pager_duty: "createNotifications.pagerDutySettings.integrationKeyLabel",
|
||||||
|
webhook: "createNotifications.webhookSettings.webhookLabel",
|
||||||
|
discord: "createNotifications.discordSettings.webhookLabel",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const PLACEHOLDER_MAP = {
|
||||||
|
email: "createNotifications.emailSettings.emailPlaceholder",
|
||||||
|
slack: "createNotifications.slackSettings.webhookPlaceholder",
|
||||||
|
pager_duty: "createNotifications.pagerDutySettings.integrationKeyPlaceholder",
|
||||||
|
webhook: "createNotifications.webhookSettings.webhookPlaceholder",
|
||||||
|
discord: "createNotifications.discordSettings.webhookPlaceholder",
|
||||||
|
};
|
||||||
@@ -707,13 +707,13 @@
|
|||||||
"webhookLabel": "Slack webhook URL",
|
"webhookLabel": "Slack webhook URL",
|
||||||
"webhookPlaceholder": "https://hooks.slack.com/services/..."
|
"webhookPlaceholder": "https://hooks.slack.com/services/..."
|
||||||
},
|
},
|
||||||
"pagerdutySettings": {
|
"pagerDutySettings": {
|
||||||
"title": "PagerDuty",
|
"title": "PagerDuty",
|
||||||
"description": "Configure your PagerDuty integration here",
|
"description": "Configure your PagerDuty integration here",
|
||||||
"integrationKeyLabel": "Integration key",
|
"integrationKeyLabel": "Integration key",
|
||||||
"integrationKeyPlaceholder": "1234567890"
|
"integrationKeyPlaceholder": "1234567890"
|
||||||
},
|
},
|
||||||
"genericWebhookSettings": {
|
"webhookSettings": {
|
||||||
"title": "Webhook",
|
"title": "Webhook",
|
||||||
"description": "Configure your webhook here",
|
"description": "Configure your webhook here",
|
||||||
"webhookLabel": "Webhook URL",
|
"webhookLabel": "Webhook URL",
|
||||||
|
|||||||
Reference in new issue
Block a user