From f8f708921ccf7bda68f3188af5caf761f455aff2 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 22 Apr 2022 11:47:21 +0800 Subject: [PATCH] fix: unable to save associated data in sub-table --- .../client/src/block-provider/hooks/index.ts | 93 +++++++++++++------ 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index ac7764813a..16d588e851 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -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'; + 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,12 +91,12 @@ export const useCreateActionProps = () => { } else { values[key] = items; } - } else { - values[key] = form.values[key]; } } else { values[key] = form.values[key]; } + } else { + values[key] = form.values[key]; } } await resource.create({ @@ -116,22 +145,28 @@ 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); - if (collectionField.interface === 'subTable') { - values[key] = form.values[key]; - continue; - } - if (!field.added.has(key)) { - continue; - } - const items = form.values[key]; - if (collectionField.interface === 'linkTo') { - const targetKey = collectionField.targetKey || 'id'; + for (const key in form.values) { + if (fieldNames.includes(key)) { + const collectionField = getField(key); + if (collectionField.interface === 'subTable') { + values[key] = form.values[key]; + continue; + } + if (!field.added.has(key)) { + continue; + } + 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,12 +174,12 @@ export const useUpdateActionProps = () => { } else { values[key] = items; } - } else { - values[key] = form.values[key]; } } else { values[key] = form.values[key]; } + } else { + values[key] = form.values[key]; } } await resource.update({