From a8ce758cab88ef6262bc0d7a8ab7cca14a32c419 Mon Sep 17 00:00:00 2001 From: SemmyWong <67748948+semmywong@users.noreply.github.com> Date: Wed, 22 Jun 2022 13:41:45 +0800 Subject: [PATCH] fix: action loading, refresh context, form submit and validate (#523) --- .../client/src/block-provider/hooks/index.ts | 68 +++++++++++-------- .../buttons/FormActionInitializers.tsx | 3 + .../ReadPrettyFormActionInitializers.tsx | 1 + 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index c7f121d4a..2da4085a0 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -223,17 +223,20 @@ export const useCustomizeRequestActionProps = () => { const compile = useCompile(); const form = useForm(); const { fields, getField } = useCollection(); - const { field, resource } = useBlockRequestContext(); + const { field, resource, __parent, service } = useBlockRequestContext(); const currentRecord = useRecord(); const currentUserContext = useCurrentUserContext(); const currentUser = currentUserContext?.data?.data; + const actionField = useField(); + return { async onClick() { const { skipValidator, onSuccess, requestSettings } = actionSchema?.['x-action-settings'] ?? {}; + const xAction = actionSchema?.['x-action']; if (!requestSettings['url']) { return; } - if (skipValidator === false) { + if (skipValidator === false && xAction === 'customize:form:request') { await form.submit(); } @@ -241,7 +244,7 @@ export const useCustomizeRequestActionProps = () => { const params = requestSettings['params'] ? JSON.parse(requestSettings['params']) : {}; const data = requestSettings['data'] ? JSON.parse(requestSettings['data']) : {}; const methods = ['POST', 'PUT', 'PATCH']; - if (actionSchema['x-action'] === 'customize:form:request' && methods.includes(requestSettings['method'])) { + if (xAction === 'customize:form:request' && methods.includes(requestSettings['method'])) { const fieldNames = fields.map((field) => field.name); const values = getFormValues(filterByTk, field, form, fieldNames, getField, resource); Object.assign(data, values); @@ -253,29 +256,40 @@ export const useCustomizeRequestActionProps = () => { params: SchemaCompiler.compile(params, { currentRecord, currentUser }), data: SchemaCompiler.compile(data, { currentRecord, currentUser }), }; - - await apiClient.request({ - ...requestBody, - }); - - if (!onSuccess?.successMessage) { - return; - } - if (onSuccess?.manualClose) { - Modal.success({ - title: compile(onSuccess?.successMessage), - onOk: async () => { - if (onSuccess?.redirecting && onSuccess?.redirectTo) { - if (isURL(onSuccess.redirectTo)) { - window.location.href = onSuccess.redirectTo; - } else { - history.push(onSuccess.redirectTo); - } - } - }, + actionField.data = field.data || {}; + actionField.data.loading = true; + try { + await apiClient.request({ + ...requestBody, }); - } else { - message.success(compile(onSuccess?.successMessage)); + actionField.data.loading = false; + + service?.refresh?.(); + if (!(resource instanceof TableFieldResource)) { + __parent?.service?.refresh?.(); + } + + if (!onSuccess?.successMessage) { + return; + } + if (onSuccess?.manualClose) { + Modal.success({ + title: compile(onSuccess?.successMessage), + onOk: async () => { + if (onSuccess?.redirecting && onSuccess?.redirectTo) { + if (isURL(onSuccess.redirectTo)) { + window.location.href = onSuccess.redirectTo; + } else { + history.push(onSuccess.redirectTo); + } + } + }, + }); + } else { + message.success(compile(onSuccess?.successMessage)); + } + } finally { + actionField.data.loading = false; } }, }; @@ -311,8 +325,8 @@ export const useUpdateActionProps = () => { ...assignedValues, }, }); - actionField.data.loading = false; - if (!(resource instanceof TableFieldResource)) { + actionField.data.loading = false; + if (!(resource instanceof TableFieldResource)) { __parent?.__parent?.service?.refresh?.(); } __parent?.service?.refresh?.(); diff --git a/packages/core/client/src/schema-initializer/buttons/FormActionInitializers.tsx b/packages/core/client/src/schema-initializer/buttons/FormActionInitializers.tsx index 3a95c39ea..0f3405ed8 100644 --- a/packages/core/client/src/schema-initializer/buttons/FormActionInitializers.tsx +++ b/packages/core/client/src/schema-initializer/buttons/FormActionInitializers.tsx @@ -112,6 +112,7 @@ export const FormActionInitializers = { 'x-designer': 'Action.Designer', 'x-action-settings': { requestSettings: {}, + skipValidator: false, onSuccess: { manualClose: false, redirecting: false, @@ -241,6 +242,7 @@ export const CreateFormActionInitializers = { 'x-designer': 'Action.Designer', 'x-action-settings': { requestSettings: {}, + skipValidator: false, onSuccess: { manualClose: false, redirecting: false, @@ -370,6 +372,7 @@ export const UpdateFormActionInitializers = { 'x-designer': 'Action.Designer', 'x-action-settings': { requestSettings: {}, + skipValidator: false, onSuccess: { manualClose: false, redirecting: false, diff --git a/packages/core/client/src/schema-initializer/buttons/ReadPrettyFormActionInitializers.tsx b/packages/core/client/src/schema-initializer/buttons/ReadPrettyFormActionInitializers.tsx index 65b97b02a..f2dde2de9 100644 --- a/packages/core/client/src/schema-initializer/buttons/ReadPrettyFormActionInitializers.tsx +++ b/packages/core/client/src/schema-initializer/buttons/ReadPrettyFormActionInitializers.tsx @@ -124,6 +124,7 @@ export const ReadPrettyFormActionInitializers = { 'x-designer': 'Action.Designer', 'x-action-settings': { requestSettings: {}, + skipValidator: false, onSuccess: { manualClose: false, redirecting: false,