fix: unable to save associated data in sub-table

This commit is contained in:
chenos 2022-04-22 11:47:21 +08:00
parent 01134f8253
commit f8f708921c

View File

@ -29,6 +29,29 @@ function isURL(string) {
return url.protocol === 'http:' || url.protocol === 'https:';
}
const filterValue = (value) => {
if (typeof value !== 'object') {
return value;
}
if (!value) {
return value;
}
if (Array.isArray(value)) {
return value.map((v) => filterValue(value));
}
const obj = {};
for (const key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
const val = value[key];
if (Array.isArray(val) || (val && typeof val === 'object')) {
continue;
}
obj[key] = val;
}
}
return obj;
};
export const useCreateActionProps = () => {
const form = useForm();
const { field, resource, __parent } = useBlockRequestContext();
@ -46,15 +69,21 @@ export const useCreateActionProps = () => {
await form.submit();
}
let values = {};
if (resource instanceof TableFieldResource) {
values = form.values;
} else {
for (const key in form.values) {
if (fieldNames.includes(key)) {
const items = form.values[key];
const collectionField = getField(key);
if (collectionField.interface === 'linkTo') {
const targetKey = collectionField.targetKey || 'id';
if (resource instanceof TableFieldResource) {
if (Array.isArray(items)) {
values[key] = filterValue(items);
} else if (items && typeof items === 'object') {
values[key] = filterValue(items);
} else {
values[key] = items;
}
} else {
if (Array.isArray(items)) {
values[key] = items.map((item) => item[targetKey]);
} else if (items && typeof items === 'object') {
@ -62,6 +91,7 @@ export const useCreateActionProps = () => {
} else {
values[key] = items;
}
}
} else {
values[key] = form.values[key];
}
@ -69,7 +99,6 @@ export const useCreateActionProps = () => {
values[key] = form.values[key];
}
}
}
await resource.create({
values: {
...values,
@ -116,9 +145,6 @@ export const useUpdateActionProps = () => {
}
const fieldNames = fields.map((field) => field.name);
let values = {};
if (resource instanceof TableFieldResource) {
values = form.values;
} else {
for (const key in form.values) {
if (fieldNames.includes(key)) {
const collectionField = getField(key);
@ -132,6 +158,15 @@ export const useUpdateActionProps = () => {
const items = form.values[key];
if (collectionField.interface === 'linkTo') {
const targetKey = collectionField.targetKey || 'id';
if (resource instanceof TableFieldResource) {
if (Array.isArray(items)) {
values[key] = filterValue(items);
} else if (items && typeof items === 'object') {
values[key] = filterValue(items);
} else {
values[key] = items;
}
} else {
if (Array.isArray(items)) {
values[key] = items.map((item) => item[targetKey]);
} else if (items && typeof items === 'object') {
@ -139,6 +174,7 @@ export const useUpdateActionProps = () => {
} else {
values[key] = items;
}
}
} else {
values[key] = form.values[key];
}
@ -146,7 +182,6 @@ export const useUpdateActionProps = () => {
values[key] = form.values[key];
}
}
}
await resource.update({
filterByTk,
values: {