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) { for (const key in form.values) {
values = form.values; if (fieldNames.includes(key)) {
} else { const items = form.values[key];
for (const key in form.values) { const collectionField = getField(key);
if (fieldNames.includes(key)) { if (collectionField.interface === 'linkTo') {
const items = form.values[key]; const targetKey = collectionField.targetKey || 'id';
const collectionField = getField(key); if (resource instanceof TableFieldResource) {
if (collectionField.interface === 'linkTo') { if (Array.isArray(items)) {
const targetKey = collectionField.targetKey || 'id'; 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,12 +91,12 @@ export const useCreateActionProps = () => {
} else { } else {
values[key] = items; values[key] = items;
} }
} else {
values[key] = form.values[key];
} }
} else { } else {
values[key] = form.values[key]; values[key] = form.values[key];
} }
} else {
values[key] = form.values[key];
} }
} }
await resource.create({ await resource.create({
@ -116,22 +145,28 @@ 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) { for (const key in form.values) {
values = form.values; if (fieldNames.includes(key)) {
} else { const collectionField = getField(key);
for (const key in form.values) { if (collectionField.interface === 'subTable') {
if (fieldNames.includes(key)) { values[key] = form.values[key];
const collectionField = getField(key); continue;
if (collectionField.interface === 'subTable') { }
values[key] = form.values[key]; if (!field.added.has(key)) {
continue; continue;
} }
if (!field.added.has(key)) { const items = form.values[key];
continue; if (collectionField.interface === 'linkTo') {
} const targetKey = collectionField.targetKey || 'id';
const items = form.values[key]; if (resource instanceof TableFieldResource) {
if (collectionField.interface === 'linkTo') { if (Array.isArray(items)) {
const targetKey = collectionField.targetKey || 'id'; 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,12 +174,12 @@ export const useUpdateActionProps = () => {
} else { } else {
values[key] = items; values[key] = items;
} }
} else {
values[key] = form.values[key];
} }
} else { } else {
values[key] = form.values[key]; values[key] = form.values[key];
} }
} else {
values[key] = form.values[key];
} }
} }
await resource.update({ await resource.update({