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:'; 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 = () => { export const useCreateActionProps = () => {
const form = useForm(); const form = useForm();
const { field, resource, __parent } = useBlockRequestContext(); const { field, resource, __parent } = useBlockRequestContext();
@ -46,15 +69,21 @@ export const useCreateActionProps = () => {
await form.submit(); await form.submit();
} }
let values = {}; let values = {};
if (resource instanceof TableFieldResource) {
values = form.values;
} else {
for (const key in form.values) { for (const key in form.values) {
if (fieldNames.includes(key)) { if (fieldNames.includes(key)) {
const items = form.values[key]; const items = form.values[key];
const collectionField = getField(key); const collectionField = getField(key);
if (collectionField.interface === 'linkTo') { if (collectionField.interface === 'linkTo') {
const targetKey = collectionField.targetKey || 'id'; 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)) { if (Array.isArray(items)) {
values[key] = items.map((item) => item[targetKey]); values[key] = items.map((item) => item[targetKey]);
} else if (items && typeof items === 'object') { } else if (items && typeof items === 'object') {
@ -62,6 +91,7 @@ export const useCreateActionProps = () => {
} else { } else {
values[key] = items; values[key] = items;
} }
}
} else { } else {
values[key] = form.values[key]; values[key] = form.values[key];
} }
@ -69,7 +99,6 @@ export const useCreateActionProps = () => {
values[key] = form.values[key]; values[key] = form.values[key];
} }
} }
}
await resource.create({ await resource.create({
values: { values: {
...values, ...values,
@ -116,9 +145,6 @@ export const useUpdateActionProps = () => {
} }
const fieldNames = fields.map((field) => field.name); const fieldNames = fields.map((field) => field.name);
let values = {}; let values = {};
if (resource instanceof TableFieldResource) {
values = form.values;
} else {
for (const key in form.values) { for (const key in form.values) {
if (fieldNames.includes(key)) { if (fieldNames.includes(key)) {
const collectionField = getField(key); const collectionField = getField(key);
@ -132,6 +158,15 @@ export const useUpdateActionProps = () => {
const items = form.values[key]; const items = form.values[key];
if (collectionField.interface === 'linkTo') { if (collectionField.interface === 'linkTo') {
const targetKey = collectionField.targetKey || 'id'; 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)) { if (Array.isArray(items)) {
values[key] = items.map((item) => item[targetKey]); values[key] = items.map((item) => item[targetKey]);
} else if (items && typeof items === 'object') { } else if (items && typeof items === 'object') {
@ -139,6 +174,7 @@ export const useUpdateActionProps = () => {
} else { } else {
values[key] = items; values[key] = items;
} }
}
} else { } else {
values[key] = form.values[key]; values[key] = form.values[key];
} }
@ -146,7 +182,6 @@ export const useUpdateActionProps = () => {
values[key] = form.values[key]; values[key] = form.values[key];
} }
} }
}
await resource.update({ await resource.update({
filterByTk, filterByTk,
values: { values: {