submit
This commit is contained in:
1 parent
796960aeb2
commit
86a55e3cfe
1 file changed
+61
-59
@@ -7,9 +7,10 @@ import { useTheme } from "@mui/material/styles";
|
|||||||
|
|
||||||
import { useEffect, useMemo } from "react";
|
import { useEffect, useMemo } from "react";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
import { Controller, useForm } from "react-hook-form";
|
import { Controller, useForm } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { useGet } from "@/Hooks/UseApi";
|
import { useGet, usePost } from "@/Hooks/UseApi";
|
||||||
import { useNotificationForm } from "@/Hooks/useNotificationForm";
|
import { useNotificationForm } from "@/Hooks/useNotificationForm";
|
||||||
import type { NotificationFormData } from "@/Validation/notifications";
|
import type { NotificationFormData } from "@/Validation/notifications";
|
||||||
import type { Notification } from "@/Types/Notification";
|
import type { Notification } from "@/Types/Notification";
|
||||||
@@ -17,73 +18,74 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { NotificationChannels } from "@/Types/Notification";
|
import { NotificationChannels } from "@/Types/Notification";
|
||||||
|
|
||||||
const NotificationsCreatePage = () => {
|
const NotificationsCreatePage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { notificationId } = useParams<{ notificationId: string }>();
|
const navigate = useNavigate();
|
||||||
const isEditMode = Boolean(notificationId);
|
const { notificationId } = useParams<{ notificationId: string }>();
|
||||||
|
const isEditMode = Boolean(notificationId);
|
||||||
|
|
||||||
const { data: existingNotification } = useGet<Notification>(
|
const { data: existingNotification } = useGet<Notification>(
|
||||||
isEditMode ? `/notifications/${notificationId}` : null
|
isEditMode ? `/notifications/${notificationId}` : null
|
||||||
);
|
);
|
||||||
|
|
||||||
const { schema, defaults } = useNotificationForm({ data: existingNotification });
|
const { post, loading: isSubmitting } = usePost<NotificationFormData, Notification>();
|
||||||
|
|
||||||
const form = useForm<NotificationFormData>({
|
const { schema, defaults } = useNotificationForm({ data: existingNotification });
|
||||||
resolver: zodResolver(schema),
|
|
||||||
defaultValues: defaults,
|
|
||||||
});
|
|
||||||
|
|
||||||
const {
|
const form = useForm<NotificationFormData>({
|
||||||
control,
|
resolver: zodResolver(schema),
|
||||||
watch,
|
defaultValues: defaults,
|
||||||
reset,
|
});
|
||||||
handleSubmit,
|
|
||||||
} = form;
|
|
||||||
|
|
||||||
useEffect(() => {
|
const { control, watch, reset, handleSubmit } = form;
|
||||||
reset(defaults);
|
|
||||||
}, [defaults, reset]);
|
|
||||||
|
|
||||||
const watchedType = watch("type");
|
useEffect(() => {
|
||||||
|
reset(defaults);
|
||||||
|
}, [defaults, reset]);
|
||||||
|
|
||||||
const addressConfig = useMemo(() => {
|
const watchedType = watch("type");
|
||||||
if (watchedType === "pager_duty") {
|
|
||||||
return {
|
|
||||||
title: t("pages.notifications.form.pagerDuty.title"),
|
|
||||||
description: t("pages.notifications.form.pagerDuty.description"),
|
|
||||||
fieldLabel: t("pages.notifications.form.pagerDuty.optionIntegrationKey"),
|
|
||||||
placeholder: t("pages.notifications.form.pagerDuty.placeholder"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (watchedType === "email") {
|
|
||||||
return {
|
|
||||||
title: t("pages.notifications.form.address.title"),
|
|
||||||
description: t("pages.notifications.form.address.description"),
|
|
||||||
fieldLabel: t("pages.notifications.form.address.optionAddress"),
|
|
||||||
placeholder: t("pages.notifications.form.address.placeholderEmail"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
title: t("pages.notifications.form.address.title"),
|
|
||||||
description: t("pages.notifications.form.address.description"),
|
|
||||||
fieldLabel: t("pages.notifications.form.address.optionAddress"),
|
|
||||||
placeholder: t("pages.notifications.form.address.placeholderWebhook"),
|
|
||||||
};
|
|
||||||
}, [watchedType, t]);
|
|
||||||
|
|
||||||
const onSubmit = (data: NotificationFormData) => {
|
const addressConfig = useMemo(() => {
|
||||||
console.log(data);
|
if (watchedType === "pager_duty") {
|
||||||
};
|
return {
|
||||||
|
title: t("pages.notifications.form.pagerDuty.title"),
|
||||||
|
description: t("pages.notifications.form.pagerDuty.description"),
|
||||||
|
fieldLabel: t("pages.notifications.form.pagerDuty.optionIntegrationKey"),
|
||||||
|
placeholder: t("pages.notifications.form.pagerDuty.placeholder"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (watchedType === "email") {
|
||||||
|
return {
|
||||||
|
title: t("pages.notifications.form.address.title"),
|
||||||
|
description: t("pages.notifications.form.address.description"),
|
||||||
|
fieldLabel: t("pages.notifications.form.address.optionAddress"),
|
||||||
|
placeholder: t("pages.notifications.form.address.placeholderEmail"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
title: t("pages.notifications.form.address.title"),
|
||||||
|
description: t("pages.notifications.form.address.description"),
|
||||||
|
fieldLabel: t("pages.notifications.form.address.optionAddress"),
|
||||||
|
placeholder: t("pages.notifications.form.address.placeholderWebhook"),
|
||||||
|
};
|
||||||
|
}, [watchedType, t]);
|
||||||
|
|
||||||
const handleTest = () => {
|
const onSubmit = async (data: NotificationFormData) => {
|
||||||
console.log("Test notification");
|
const result = await post("/notifications", data);
|
||||||
};
|
if (result) {
|
||||||
|
navigate("/notifications");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
const handleTest = () => {
|
||||||
<BasePage
|
console.log("Test notification");
|
||||||
component="form"
|
};
|
||||||
onSubmit={handleSubmit(onSubmit)}
|
|
||||||
>
|
return (
|
||||||
|
<BasePage
|
||||||
|
component="form"
|
||||||
|
onSubmit={handleSubmit(onSubmit)}
|
||||||
|
>
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.notifications.form.notificationName.title")}
|
title={t("pages.notifications.form.notificationName.title")}
|
||||||
subtitle={t("pages.notifications.form.notificationName.description")}
|
subtitle={t("pages.notifications.form.notificationName.description")}
|
||||||
@@ -232,7 +234,7 @@ const NotificationsCreatePage = () => {
|
|||||||
{t("common.buttons.test")}
|
{t("common.buttons.test")}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
loading={false}
|
loading={isSubmitting}
|
||||||
type="submit"
|
type="submit"
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
|
|||||||
Reference in new issue
Block a user