From 6b2d8fdd4a441b4b983362d1f6cac6759a4b6bb1 Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 14 Apr 2022 16:01:29 +0800 Subject: [PATCH] feat: onSuccess for action settings --- .../client/src/block-provider/hooks/index.ts | 49 +++++++++------ .../antd/action/Action.Designer.tsx | 61 ++++++++++++++++++- .../antd/form-v2/Form.Designer.tsx | 58 +----------------- .../buttons/FormActionInitializers.tsx | 6 ++ 4 files changed, 97 insertions(+), 77 deletions(-) diff --git a/packages/client/src/block-provider/hooks/index.ts b/packages/client/src/block-provider/hooks/index.ts index 84cde3291..406942c66 100644 --- a/packages/client/src/block-provider/hooks/index.ts +++ b/packages/client/src/block-provider/hooks/index.ts @@ -51,27 +51,23 @@ export const useCreateActionProps = () => { }); __parent?.service?.refresh?.(); setVisible?.(false); - if (visible !== undefined) { + const onSuccess = actionSchema?.['x-action-settings']?.onSuccess; + if (!onSuccess?.successMessage) { return; } - const onSuccess = field?.decoratorProps?.onSuccess; - if (typeof onSuccess === 'function') { - onSuccess({ form }); - } else { - Modal.success({ - title: onSuccess?.successMessage || t('Submitted successfully!'), - onOk: async () => { - await form.reset(); - if (onSuccess?.redirecting && onSuccess?.redirectTo) { - if (isURL(onSuccess.redirectTo)) { - window.location.href = onSuccess.redirectTo; - } else { - history.push(onSuccess.redirectTo); - } + Modal.success({ + title: onSuccess?.successMessage, + onOk: async () => { + await form.reset(); + if (onSuccess?.redirecting && onSuccess?.redirectTo) { + if (isURL(onSuccess.redirectTo)) { + window.location.href = onSuccess.redirectTo; + } else { + history.push(onSuccess.redirectTo); } - }, - }); - } + } + }, + }); }, }; }; @@ -82,6 +78,7 @@ export const useUpdateActionProps = () => { const { resource, __parent } = useBlockRequestContext(); const { setVisible } = useActionContext(); const actionSchema = useFieldSchema(); + const history = useHistory(); return { async onClick() { const skipValidator = actionSchema?.['x-action-settings']?.skipValidator; @@ -101,6 +98,22 @@ export const useUpdateActionProps = () => { __parent?.__parent?.service?.refresh?.(); } setVisible?.(false); + const onSuccess = actionSchema?.['x-action-settings']?.onSuccess; + if (onSuccess?.successMessage) { + Modal.success({ + title: onSuccess?.successMessage, + onOk: async () => { + await form.reset(); + if (onSuccess?.redirecting && onSuccess?.redirectTo) { + if (isURL(onSuccess.redirectTo)) { + window.location.href = onSuccess.redirectTo; + } else { + history.push(onSuccess.redirectTo); + } + } + }, + }); + } }, }; }; diff --git a/packages/client/src/schema-component/antd/action/Action.Designer.tsx b/packages/client/src/schema-component/antd/action/Action.Designer.tsx index 52eb414a5..390bdb299 100644 --- a/packages/client/src/schema-component/antd/action/Action.Designer.tsx +++ b/packages/client/src/schema-component/antd/action/Action.Designer.tsx @@ -1,5 +1,6 @@ import { ISchema, useField, useFieldSchema } from '@formily/react'; import React from 'react'; +import { useTranslation } from 'react-i18next'; import { useDesignable } from '../..'; import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings'; @@ -7,6 +8,7 @@ export const ActionDesigner = (props) => { const field = useField(); const fieldSchema = useFieldSchema(); const { dn } = useDesignable(); + const { t } = useTranslation(); const isPopupAction = ['create', 'update', 'view'].includes(fieldSchema['x-action'] || ''); return ( @@ -79,7 +81,7 @@ export const ActionDesigner = (props) => { )} {fieldSchema?.['x-action-settings'] && ( { fieldSchema['x-action-settings'].skipValidator = value; @@ -127,11 +129,66 @@ export const ActionDesigner = (props) => { }} /> )} + {fieldSchema?.['x-action-settings'] && ( + { + fieldSchema['x-action-settings']['onSuccess'] = onSuccess; + dn.emit('patch', { + schema: { + ['x-uid']: fieldSchema['x-uid'], + 'x-action-settings': fieldSchema['x-action-settings'], + }, + }); + }} + /> + )} { - return s['x-component'] === 'Space' || s['x-component'] === 'ActionBar'; + return s['x-component'] === 'Space' || s['x-component'].endsWith('ActionBar'); }} /> diff --git a/packages/client/src/schema-component/antd/form-v2/Form.Designer.tsx b/packages/client/src/schema-component/antd/form-v2/Form.Designer.tsx index 890199e46..aaf69e203 100644 --- a/packages/client/src/schema-component/antd/form-v2/Form.Designer.tsx +++ b/packages/client/src/schema-component/antd/form-v2/Form.Designer.tsx @@ -1,4 +1,4 @@ -import { ISchema, useField, useFieldSchema } from '@formily/react'; +import { useField, useFieldSchema } from '@formily/react'; import React from 'react'; import { useTranslation } from 'react-i18next'; import { useFormBlockContext } from '../../../block-provider'; @@ -19,62 +19,6 @@ export const FormDesigner = () => { const { visible } = useActionContext(); return ( -