From 047479426a2e36989e0f0e567cfb242a93724652 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Fri, 27 Dec 2024 17:02:21 +0800 Subject: [PATCH] refactor(ui): clean code --- ui/src/components/core/DrawerForm.tsx | 17 +- .../workflow/node/ApplyNodeForm.tsx | 21 +-- ui/src/pages/workflows/WorkflowDetail.tsx | 154 ++++++++---------- 3 files changed, 82 insertions(+), 110 deletions(-) diff --git a/ui/src/components/core/DrawerForm.tsx b/ui/src/components/core/DrawerForm.tsx index e59312d8..a561ef77 100644 --- a/ui/src/components/core/DrawerForm.tsx +++ b/ui/src/components/core/DrawerForm.tsx @@ -1,4 +1,5 @@ -import { useControllableValue } from "ahooks"; +import { useTranslation } from "react-i18next"; +import { useControllableValue } from "ahooks"; import { Button, Drawer, Form, Space, type DrawerProps, type FormProps, type ModalProps } from "antd"; import { useAntdForm, useTriggerElement } from "@/hooks"; @@ -25,14 +26,20 @@ const DrawerForm = = any>({ className, style, children, + cancelText, + cancelButtonProps, form, drawerProps, + okText, + okButtonProps, title, trigger, width, onFinish, ...props }: DrawerFormProps) => { + const { t } = useTranslation(); + const [open, setOpen] = useControllableValue(props, { valuePropName: "open", defaultValuePropName: "defaultOpen", @@ -76,9 +83,11 @@ const DrawerForm = = any>({ - - + } diff --git a/ui/src/components/workflow/node/ApplyNodeForm.tsx b/ui/src/components/workflow/node/ApplyNodeForm.tsx index 385b3d0e..7d5907d4 100644 --- a/ui/src/components/workflow/node/ApplyNodeForm.tsx +++ b/ui/src/components/workflow/node/ApplyNodeForm.tsx @@ -119,9 +119,8 @@ const ApplyNodeForm = ({ data }: ApplyNodeFormProps) => { /> + } @@ -215,9 +214,8 @@ const ApplyNodeForm = ({ data }: ApplyNodeFormProps) => { /> + } @@ -330,7 +328,6 @@ const FormFieldEmailSelect = ({ const FormFieldDomainsModalForm = ({ data, - disabled, trigger, onFinish, }: { @@ -371,7 +368,6 @@ const FormFieldDomainsModalForm = ({ return ( void; -}) => { +const FormFieldNameserversModalForm = ({ data, trigger, onFinish }: { data: string; trigger?: React.ReactNode; onFinish?: (data: string) => void }) => { const { t } = useTranslation(); const formSchema = z.object({ @@ -432,7 +418,6 @@ const FormFieldNameserversModalForm = ({ return ( { title={workflow.name} extra={[ - {t("common.button.edit")}} onFinish={handleBaseInfoFormFinish} /> + {t("common.button.edit")}} onFinish={handleBaseInfoFormFinish} /> @@ -182,93 +183,70 @@ const WorkflowDetail = () => { ); }; -const WorkflowBaseInfoModalForm = memo( - ({ - initialValues, - trigger, - onFinish, - }: { - initialValues: Pick; - trigger?: React.ReactNode; - onFinish?: (values: Pick) => Promise; - }) => { - const { t } = useTranslation(); +const WorkflowBaseInfoModalForm = ({ + data, + trigger, + onFinish, +}: { + data: Pick; + trigger?: React.ReactNode; + onFinish?: (values: Pick) => Promise; +}) => { + const { t } = useTranslation(); - const [open, setOpen] = useState(false); + const formSchema = z.object({ + name: z + .string({ message: t("workflow.baseinfo.form.name.placeholder") }) + .trim() + .min(1, t("workflow.baseinfo.form.name.placeholder")) + .max(64, t("common.errmsg.string_max", { max: 64 })), + description: z + .string({ message: t("workflow.baseinfo.form.description.placeholder") }) + .trim() + .min(0, t("workflow.baseinfo.form.description.placeholder")) + .max(256, t("common.errmsg.string_max", { max: 256 })) + .nullish(), + }); + const formRule = createSchemaFieldRule(formSchema); + const { + form: formInst, + formPending, + formProps, + ...formApi + } = useAntdForm>({ + initialValues: data, + onSubmit: async () => { + const ret = await onFinish?.(formInst.getFieldsValue(true)); + if (ret != null && !ret) return false; + return true; + }, + }); - const triggerDom = useTriggerElement(trigger, { onClick: () => setOpen(true) }); + const handleFinish = async () => { + return formApi.submit(); + }; - const formSchema = z.object({ - name: z - .string({ message: t("workflow.baseinfo.form.name.placeholder") }) - .trim() - .min(1, t("workflow.baseinfo.form.name.placeholder")) - .max(64, t("common.errmsg.string_max", { max: 64 })), - description: z - .string({ message: t("workflow.baseinfo.form.description.placeholder") }) - .trim() - .min(0, t("workflow.baseinfo.form.description.placeholder")) - .max(256, t("common.errmsg.string_max", { max: 256 })) - .nullish(), - }); - const formRule = createSchemaFieldRule(formSchema); - const { - form: formInst, - formPending, - formProps, - ...formApi - } = useAntdForm>({ - initialValues: initialValues, - onSubmit: async () => { - const ret = await onFinish?.(formInst.getFieldsValue(true)); - if (ret != null && !ret) return; - - setOpen(false); - }, - }); - - const handleClickOk = async () => { - formApi.submit(); - }; - - const handleClickCancel = () => { - if (formPending) return Promise.reject(); - - setOpen(false); - }; - - return ( - <> - {triggerDom} - - setOpen(false)} - cancelButtonProps={{ disabled: formPending }} - closable - confirmLoading={formPending} - destroyOnClose - okText={t("common.button.save")} - open={open} - title={t(`workflow.baseinfo.modal.title`)} - width={480} - onOk={handleClickOk} - onCancel={handleClickCancel} - > -
-
- - - - - - - -
-
-
- - ); - } -); + return ( + + + + + + + + + ); +}; export default WorkflowDetail;