From b40564b619bb61e7074161f2743b56c86872c641 Mon Sep 17 00:00:00 2001 From: ZhaoBingyu <13843076612@163.com> Date: Mon, 10 Jul 2023 15:06:59 +0800 Subject: [PATCH] feat: Fix subtable appID --- .../form-builder/registry/sub-table/convertor.ts | 4 ++-- clients/components/form-builder/store.ts | 2 +- clients/components/form-builder/type.d.ts | 2 +- clients/components/form-builder/utils/fields-operator.ts | 8 ++++---- .../home/pages/app-table-view-detail/create-data-form.tsx | 4 ++-- clients/home/utils.ts | 8 +++++--- clients/lib/http-client-form.ts | 8 ++++---- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/clients/components/form-builder/registry/sub-table/convertor.ts b/clients/components/form-builder/registry/sub-table/convertor.ts index a634a02f6..52aaaeda7 100644 --- a/clients/components/form-builder/registry/sub-table/convertor.ts +++ b/clients/components/form-builder/registry/sub-table/convertor.ts @@ -74,7 +74,7 @@ export function toSchema(value: SubTableConfig): ISchema { }; } -export function toConfig(schema: ISchema): SubTableConfig { +export function toConfig(schema: ISchema, appID?: any): SubTableConfig { const isFromLinkedTable = schema?.['x-component-props']?.subordination === 'foreign_table'; const tableID = schema['x-component-props']?.tableID; return { @@ -87,7 +87,7 @@ export function toConfig(schema: ISchema): SubTableConfig { subTableSchema: schema.items as ISchema, required: !!schema.required, linkedTable: { - appID: schema['x-component-props']?.appID, + appID: appID || schema['x-component-props']?.appID, tableID: isFromLinkedTable ? tableID : '', tableName: schema['x-component-props']?.tableName, }, diff --git a/clients/components/form-builder/store.ts b/clients/components/form-builder/store.ts index d1983f2b4..0863f33fa 100644 --- a/clients/components/form-builder/store.ts +++ b/clients/components/form-builder/store.ts @@ -64,7 +64,7 @@ export default class FormBuilderStore { @observable getFieldValueFunc?: any; constructor({ schema, appID, pageID }: Props) { - this.flattenFields = flattenSchemaToFields(schema); + this.flattenFields = flattenSchemaToFields(schema, appID); this.internalFields = INTERNAL_FIELDS; this.appID = appID; this.pageID = pageID; diff --git a/clients/components/form-builder/type.d.ts b/clients/components/form-builder/type.d.ts index fb2a80110..cd1b1649a 100644 --- a/clients/components/form-builder/type.d.ts +++ b/clients/components/form-builder/type.d.ts @@ -92,7 +92,7 @@ declare namespace FormBuilder { defaultConfig: T; // transform configuration to the node of schema used by SchemaForm toSchema: (value: T) => ISchema; - toConfig: (schema: ISchema) => T; + toConfig: (schema: ISchema, appID?: any) => T; configDependencies?: Record>; compareOperators?: CompareOperator[]; placeholderComponent?: React.JSXElementConstructor; diff --git a/clients/components/form-builder/utils/fields-operator.ts b/clients/components/form-builder/utils/fields-operator.ts index aded5a273..57d8ac821 100644 --- a/clients/components/form-builder/utils/fields-operator.ts +++ b/clients/components/form-builder/utils/fields-operator.ts @@ -10,7 +10,7 @@ export const findIndex = (id: string, arr: FormItem[]): number => { }; // Deal with field by config -export function formatFieldWithConfig(currentField: Record): FormItem | null { +export function formatFieldWithConfig(currentField: Record, appID?: any): FormItem | null { if (!currentField) return null; const componentName = (currentField['x-component'] || currentField['componentName'])?.toLowerCase(); if (!componentName || !registry.elements[componentName]) { @@ -18,7 +18,7 @@ export function formatFieldWithConfig(currentField: Record): FormIt return null; } - const configValue = registry.elements[componentName].toConfig(currentField); + const configValue = registry.elements[componentName].toConfig(currentField, appID); const xInternal = currentField?.['x-internal'] || {}; const xIndex = currentField?.['x-index']; @@ -126,14 +126,14 @@ export function getFieldId(field: ISchema): string { return get(field, 'x-internal.fieldId') || get(field, 'fieldName'); } -export function flattenSchemaToFields(schema: ISchema): FormItem[] { +export function flattenSchemaToFields(schema: ISchema, appID?: any): FormItem[] { const fieldsArr: FormItem[] = []; const recu = ({ properties }: ISchema, pid?: string): void => { if (isEmpty(properties)) return; Object.keys(properties || {}).forEach((key: string) => { const _field = properties?.[key] || {}; if (get(_field, 'x-internal.isSystem')) return; - const field = formatFieldWithConfig(_field); + const field = formatFieldWithConfig(_field, appID); const currentField = omit(cloneDeep(field), 'properties'); // fallback: will remove this code! diff --git a/clients/home/pages/app-table-view-detail/create-data-form.tsx b/clients/home/pages/app-table-view-detail/create-data-form.tsx index fd520a7fe..3c239318f 100644 --- a/clients/home/pages/app-table-view-detail/create-data-form.tsx +++ b/clients/home/pages/app-table-view-detail/create-data-form.tsx @@ -59,7 +59,7 @@ function CreateDataForm({ appID, pageID, rowID, onCancel }: Props): JSX.Element appID, pageID, rowID, - buildFormDataReqParams(schema, 'updated', newValue), + buildFormDataReqParams(schema, 'updated', newValue, appID), ).then(() => { toast.success('修改成功'); }); @@ -67,7 +67,7 @@ function CreateDataForm({ appID, pageID, rowID, onCancel }: Props): JSX.Element return createFormDataRequest( appID, pageID, - buildFormDataReqParams(schema, 'create', currentValue), + buildFormDataReqParams(schema, 'create', currentValue, appID), ).then(() => { toast.success('保存成功'); }); diff --git a/clients/home/utils.ts b/clients/home/utils.ts index ae98c5291..1c681c763 100644 --- a/clients/home/utils.ts +++ b/clients/home/utils.ts @@ -172,6 +172,7 @@ function buildRef( schema: ISchema, type: string, values?: Record, + appID?: any, ): [FormDataRequestUpdateParamsRef, string[]] { const ref: FormDataRequestUpdateParamsRef = {}; const refFields = schemaToFields(schema, (schemaField) => { @@ -182,7 +183,7 @@ function buildRef( refFields.forEach(async (field) => { switch (field['x-component']) { case 'SubTable': { - const { subordination, appID, tableID } = field?.['x-component-props'] || {}; + const { subordination, appID: subtableAppID, tableID } = field?.['x-component-props'] || {}; const [subRef] = buildRef(subordination === 'foreign_table' ? window[`schema-${field.id}`] : field.items as ISchema, 'create'); const _ref = subRef; @@ -190,7 +191,7 @@ function buildRef( if (values?.[field.id]?.length || !isEmpty(_ref)) { ref[field.id] = { type: subordination || 'sub_table', - appID, + appID: appID || subtableAppID, tableID, ...buildSubTableParams(type, values?.[field.id] || [{}], _ref), }; @@ -237,8 +238,9 @@ export function buildFormDataReqParams( schema: ISchema, type: string, values?: Record, + appID?: any, ): FormDataBody { - const [ref, omitFields] = buildRef(schema, type, values); + const [ref, omitFields] = buildRef(schema, type, values, appID); const formDataResBody: FormDataBody = {}; diff --git a/clients/lib/http-client-form.ts b/clients/lib/http-client-form.ts index 342392e68..ff01e88b3 100644 --- a/clients/lib/http-client-form.ts +++ b/clients/lib/http-client-form.ts @@ -3,7 +3,7 @@ import schemaToFields from '@lib/schema-convert'; import type { ESParameter } from '@c/data-filter/utils'; import httpClient from './http-client'; -export function buildQueryRef(schema: ISchema): FormDataRequestUpdateParamsRef { +export function buildQueryRef(schema: ISchema, appID?: any): FormDataRequestUpdateParamsRef { const refFields = schemaToFields(schema, (schemaField) => { return ['SubTable', 'AggregationRecords', 'AssociatedRecords'].includes(schemaField['x-component'] || ''); }); @@ -13,10 +13,10 @@ export function buildQueryRef(schema: ISchema): FormDataRequestUpdateParamsRef { refFields.forEach((field) => { switch (field.componentName) { case 'subtable': { - const { subordination, appID, tableID } = field?.['x-component-props'] || {}; + const { subordination, appID: subtableAppID, tableID } = field?.['x-component-props'] || {}; ref[field.id] = { type: subordination || 'sub_table', - appID, + appID: appID || subtableAppID, tableID, }; break; @@ -66,7 +66,7 @@ export function findOneFormDataRequest( return httpClient( `/api/v1/form/${appID}/home/form/${tableID}/get`, { - ref: buildQueryRef(schema), + ref: buildQueryRef(schema, appID), query: { term: { _id: rowID }, },