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
This commit is contained in:
katherinehhh 2023-09-24 19:17:55 +08:00 committed by GitHub
parent e522e1f742
commit d83afc52ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -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;

View File

@ -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);