From d83afc52ec3457feb6e02d067ebea846f6449088 Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Sun, 24 Sep 2023 19:17:55 +0800 Subject: [PATCH] fix: saving method of association field creation button is not effect (#2706) * fix: the saving method of the association field creation button is not effective * refactor: code improve --- .../core/client/src/block-provider/hooks/index.ts | 10 +++++++++- .../antd/association-field/Editable.tsx | 11 ++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index 6c05e7605..7b5891a0d 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -215,6 +215,8 @@ export const useAssociationCreateActionProps = () => { const currentRecord = useRecord(); const currentUserContext = useCurrentUserContext(); const currentUser = currentUserContext?.data?.data; + const action = actionField.componentProps.saveMode || 'create'; + const filterKeys = actionField.componentProps.filterKeys?.checked || []; return { async onClick() { const fieldNames = fields.map((field) => field.name); @@ -223,6 +225,7 @@ export const useAssociationCreateActionProps = () => { onSuccess, overwriteValues, skipValidator, + triggerWorkflows, } = actionSchema?.['x-action-settings'] ?? {}; const addChild = fieldSchema?.['x-component-props']?.addChild; const assignedValues = parse(originalAssignedValues)({ currentTime: new Date(), currentRecord, currentUser }); @@ -238,12 +241,17 @@ export const useAssociationCreateActionProps = () => { actionField.data = field.data || {}; actionField.data.loading = true; try { - const data = await resource.create({ + const data = await resource[action]({ values: { ...values, ...overwriteValues, ...assignedValues, }, + filterKeys: filterKeys, + // TODO(refactor): should change to inject by plugin + triggerWorkflows: triggerWorkflows?.length + ? triggerWorkflows.map((row) => [row.workflowKey, row.context].filter(Boolean).join('!')).join(',') + : undefined, }); actionField.data.loading = false; actionField.data.data = data; diff --git a/packages/core/client/src/schema-component/antd/association-field/Editable.tsx b/packages/core/client/src/schema-component/antd/association-field/Editable.tsx index eace48a76..c03e62341 100644 --- a/packages/core/client/src/schema-component/antd/association-field/Editable.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/Editable.tsx @@ -12,6 +12,7 @@ import { InternalSubTable } from './InternalSubTable'; import { InternaPopoverNester } from './InternalPopoverNester'; import { CreateRecordAction } from './components/CreateRecordAction'; import { useAssociationFieldContext } from './hooks'; +import { useCollection } from '../../../collection-manager'; const EditableAssociationField = observer( (props: any) => { @@ -23,6 +24,8 @@ const EditableAssociationField = observer( const useCreateActionProps = () => { const { onClick } = useCAP(); const actionField: any = useField(); + const { getPrimaryKey } = useCollection(); + const primaryKey = getPrimaryKey(); return { async onClick() { await onClick(); @@ -30,9 +33,11 @@ const EditableAssociationField = observer( if (data) { if (['m2m', 'o2m'].includes(collectionField?.interface) && multiple !== false) { const values = form.getValuesIn(field.path) || []; - values.push(data); - form.setValuesIn(field.path, values); - field.onInput(values); + if (!values.find((v) => v[primaryKey] === data[primaryKey])) { + values.push(data); + form.setValuesIn(field.path, values); + field.onInput(values); + } } else { form.setValuesIn(field.path, data); field.onInput(data);