import { memo, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router"; import { RightOutlined as RightOutlinedIcon } from "@ant-design/icons"; import { Button, Form, Input, Select } from "antd"; import { createSchemaFieldRule } from "antd-zod"; import { produce } from "immer"; import { z } from "zod"; import { notifyChannelsMap } from "@/domain/settings"; import { type WorkflowNode } from "@/domain/workflow"; import { useAntdForm, useZustandShallowSelector } from "@/hooks"; import { useNotifyChannelsStore } from "@/stores/notify"; import { useWorkflowStore } from "@/stores/workflow"; import { usePanel } from "../PanelProvider"; export type NotifyNodeFormProps = { data: WorkflowNode; }; const initFormModel = () => { return { subject: "Completed!", message: "Your workflow has been completed on Certimate.", }; }; const NotifyNodeForm = ({ data }: NotifyNodeFormProps) => { const { t } = useTranslation(); const { channels, loadedAtOnce: channelsLoadedAtOnce, fetchChannels, } = useNotifyChannelsStore(useZustandShallowSelector(["channels", "loadedAtOnce", "fetchChannels"])); useEffect(() => { fetchChannels(); }, [fetchChannels]); const { updateNode } = useWorkflowStore(useZustandShallowSelector(["updateNode"])); const { hidePanel } = usePanel(); const formSchema = z.object({ subject: z .string({ message: t("workflow_node.notify.form.subject.placeholder") }) .min(1, t("workflow_node.notify.form.subject.placeholder")) .max(1000, t("common.errmsg.string_max", { max: 1000 })), message: z .string({ message: t("workflow_node.notify.form.message.placeholder") }) .min(1, t("workflow_node.notify.form.message.placeholder")) .max(1000, t("common.errmsg.string_max", { max: 1000 })), channel: z.string({ message: t("workflow_node.notify.form.channel.placeholder") }).min(1, t("workflow_node.notify.form.channel.placeholder")), }); const formRule = createSchemaFieldRule(formSchema); const { form: formInst, formPending, formProps, } = useAntdForm>({ initialValues: data?.config ?? initFormModel(), onSubmit: async (values) => { await formInst.validateFields(); await updateNode( produce(data, (draft) => { draft.config = { ...values }; draft.validated = true; }) ); hidePanel(); }, }); return (