Skip to content

Commit

Permalink
feat: Fix subtable appID
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoBingyu committed Jul 10, 2023
1 parent d807191 commit b40564b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion clients/components/form-builder/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clients/components/form-builder/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, React.JSXElementConstructor<any>>;
compareOperators?: CompareOperator[];
placeholderComponent?: React.JSXElementConstructor<any>;
Expand Down
8 changes: 4 additions & 4 deletions clients/components/form-builder/utils/fields-operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export const findIndex = (id: string, arr: FormItem[]): number => {
};

// Deal with field by config
export function formatFieldWithConfig(currentField: Record<string, any>): FormItem | null {
export function formatFieldWithConfig(currentField: Record<string, any>, appID?: any): FormItem | null {
if (!currentField) return null;
const componentName = (currentField['x-component'] || currentField['componentName'])?.toLowerCase();
if (!componentName || !registry.elements[componentName]) {
logger.error('fatal! there is no x-component in schema:', currentField);
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'];

Expand Down Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions clients/home/pages/app-table-view-detail/create-data-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ function CreateDataForm({ appID, pageID, rowID, onCancel }: Props): JSX.Element
appID,
pageID,
rowID,
buildFormDataReqParams(schema, 'updated', newValue),
buildFormDataReqParams(schema, 'updated', newValue, appID),
).then(() => {
toast.success('修改成功');
});
} else {
return createFormDataRequest(
appID,
pageID,
buildFormDataReqParams(schema, 'create', currentValue),
buildFormDataReqParams(schema, 'create', currentValue, appID),
).then(() => {
toast.success('保存成功');
});
Expand Down
8 changes: 5 additions & 3 deletions clients/home/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ function buildRef(
schema: ISchema,
type: string,
values?: Record<string, any>,
appID?: any,
): [FormDataRequestUpdateParamsRef, string[]] {
const ref: FormDataRequestUpdateParamsRef = {};
const refFields = schemaToFields(schema, (schemaField) => {
Expand All @@ -182,15 +183,15 @@ 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;
// tips:如果子表单内有流水号等组件默认填一行空值
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),
};
Expand Down Expand Up @@ -237,8 +238,9 @@ export function buildFormDataReqParams(
schema: ISchema,
type: string,
values?: Record<string, any>,
appID?: any,
): FormDataBody {
const [ref, omitFields] = buildRef(schema, type, values);
const [ref, omitFields] = buildRef(schema, type, values, appID);

const formDataResBody: FormDataBody = {};

Expand Down
8 changes: 4 additions & 4 deletions clients/lib/http-client-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] || '');
});
Expand All @@ -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;
Expand Down Expand Up @@ -66,7 +66,7 @@ export function findOneFormDataRequest(
return httpClient<FormDataResponse>(
`/api/v1/form/${appID}/home/form/${tableID}/get`,
{
ref: buildQueryRef(schema),
ref: buildQueryRef(schema, appID),
query: {
term: { _id: rowID },
},
Expand Down

0 comments on commit b40564b

Please sign in to comment.