From eeb07112c7a57cd93ddbd19ad116b41cd69f6829 Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Mon, 17 Apr 2023 17:20:35 +0800 Subject: [PATCH] fix: button loading does not disappear when the operation submit failed (#1698) * fix: button loading does not disappear when the view operation submission failed * fix: add field button loading * refactor: code improve --- .../Configuration/AddCollectionAction.tsx | 51 ++++++++++--------- .../Configuration/AddFieldAction.tsx | 16 +++--- .../Configuration/SyncFieldsAction.tsx | 22 ++++---- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/packages/core/client/src/collection-manager/Configuration/AddCollectionAction.tsx b/packages/core/client/src/collection-manager/Configuration/AddCollectionAction.tsx index 4ed0b29e4f..7c1ed35270 100644 --- a/packages/core/client/src/collection-manager/Configuration/AddCollectionAction.tsx +++ b/packages/core/client/src/collection-manager/Configuration/AddCollectionAction.tsx @@ -201,30 +201,35 @@ const useCreateCollection = (schema?: any) => { async run() { field.data = field.data || {}; field.data.loading = true; - await form.submit(); - const values = cloneDeep(form.values); - if (schema?.events?.beforeSubmit) { - schema.events.beforeSubmit(values); + try { + await form.submit(); + const values = cloneDeep(form.values); + if (schema?.events?.beforeSubmit) { + schema.events.beforeSubmit(values); + } + const fields = values?.template !== 'view' ? useDefaultCollectionFields(values) : values.fields; + if (values.autoCreateReverseField) { + } else { + delete values.reverseField; + } + delete values.id; + delete values.autoCreateReverseField; + + await resource.create({ + values: { + logging: true, + ...values, + fields, + }, + }); + ctx.setVisible(false); + await form.reset(); + field.data.loading = false; + refresh(); + await refreshCM(); + } catch (error) { + field.data.loading = false; } - const fields = values?.template !== 'view' ? useDefaultCollectionFields(values) : values.fields; - if (values.autoCreateReverseField) { - } else { - delete values.reverseField; - } - delete values.id; - delete values.autoCreateReverseField; - await resource.create({ - values: { - logging: true, - ...values, - fields, - }, - }); - ctx.setVisible(false); - await form.reset(); - field.data.loading = false; - refresh(); - await refreshCM(); }, }; }; diff --git a/packages/core/client/src/collection-manager/Configuration/AddFieldAction.tsx b/packages/core/client/src/collection-manager/Configuration/AddFieldAction.tsx index 8ea05eac36..beb1dd0e70 100644 --- a/packages/core/client/src/collection-manager/Configuration/AddFieldAction.tsx +++ b/packages/core/client/src/collection-manager/Configuration/AddFieldAction.tsx @@ -148,12 +148,16 @@ const useCreateCollectionField = () => { delete values.reverseField; } delete values.autoCreateReverseField; - await resource.create({ values }); - ctx.setVisible(false); - await form.reset(); - field.data.loading = false; - refresh(); - await refreshCM(); + try { + await resource.create({ values }); + ctx.setVisible(false); + await form.reset(); + field.data.loading = false; + refresh(); + await refreshCM(); + } catch (error) { + field.data.loading = false; + } }, }; }; diff --git a/packages/core/client/src/collection-manager/Configuration/SyncFieldsAction.tsx b/packages/core/client/src/collection-manager/Configuration/SyncFieldsAction.tsx index 52c0ff3390..1f2bb44265 100644 --- a/packages/core/client/src/collection-manager/Configuration/SyncFieldsAction.tsx +++ b/packages/core/client/src/collection-manager/Configuration/SyncFieldsAction.tsx @@ -136,15 +136,19 @@ const useSyncFromDatabase = () => { await form.submit(); field.data = field.data || {}; field.data.loading = true; - await api.resource(`collections`).setFields({ - filterByTk, - values: form.values, - }); - ctx.setVisible(false); - await form.reset(); - field.data.loading = false; - refresh(); - await refreshCM(); + try { + await api.resource(`collections`).setFields({ + filterByTk, + values: form.values, + }); + ctx.setVisible(false); + await form.reset(); + field.data.loading = false; + refresh(); + await refreshCM(); + } catch (error) { + field.data.loading = false; + } }, }; };