refactor: external data source collection manager (#4193)

* refactor: data source collection manager

* refactor: data source collection manager
This commit is contained in:
katherinehhh 2024-04-27 17:07:20 +08:00 committed by GitHub
parent 402e14500a
commit c5d8a7e251
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 13 deletions

View File

@ -16,7 +16,7 @@ export class PasswordFieldInterface extends CollectionFieldInterface {
'x-component': 'Password',
},
};
availableTypes = ['password'];
availableTypes = ['password', 'string'];
hasDefaultValue = true;
properties = {
...defaultProps,

View File

@ -84,6 +84,32 @@ export const CollectionFields = () => {
};
const dm = useDataSourceManager();
let isProcessing = false;
const queue = [];
const processQueue = async () => {
if (isProcessing) return;
if (queue.length === 0) return;
isProcessing = true;
const { value, filterByTk, flag } = queue.shift();
try {
await handleFieldChange(value, filterByTk, flag);
} catch (error) {
console.error('Error processing handleFieldChange:', error);
} finally {
isProcessing = false;
processQueue();
}
};
const enqueueChange = (value, filterByTk, flag) => {
queue.push({ value, filterByTk, flag });
processQueue();
};
const handleFieldChange = async (value, filterByTk, flag = true) => {
await api.request({
url: `dataSourcesCollections/${dataSourceKey}.${name}/fields:update?filterByTk=${filterByTk}`,
@ -173,7 +199,7 @@ export const CollectionFields = () => {
scope={{
useDataSource,
useTitleFieldProps,
handleFieldChange,
enqueueChange,
useDestroyActionAndRefreshCM,
useBulkDestroyActionAndRefreshCM,
loadCollections,

View File

@ -1,7 +1,12 @@
import { observer, useForm, useField } from '@formily/react';
import { Select, Tag } from 'antd';
import React, { useEffect, useState } from 'react';
import { useCompile, useCollectionManager_deprecated, useRecord, useFieldInterfaceOptions } from '@nocobase/client';
import {
useCompile,
useCollectionManager_deprecated,
useFieldInterfaceOptions,
useCollectionRecord,
} from '@nocobase/client';
const getInterfaceOptions = (data, type) => {
const interfaceOptions = [];
@ -26,17 +31,14 @@ const isValueInOptions = (value, options) => {
export const CollectionFieldInterfaceSelect = observer(
(props: any) => {
const { value, handleFieldChange } = props;
const record = useRecord();
const { data: record } = useCollectionRecord() as any;
const { getInterface } = useCollectionManager_deprecated();
const compile = useCompile();
const initOptions = useFieldInterfaceOptions();
const data = getInterfaceOptions(initOptions, record.type);
const form = useForm();
const field = useField();
const [selectValue, setSelectValue] = useState(value);
const [options, setOptions] = useState(data);
const targetRecord = Object.values(form.values)?.[0]?.[field.index];
const targetType = targetRecord?.type || record.type;
const targetType = record.type;
useEffect(() => {
//只有一个选项的时候选中该选项
if (options.length === 1 && options[0]?.children?.length === 1) {
@ -71,8 +73,6 @@ export const CollectionFieldInterfaceSelect = observer(
useEffect(() => {
if (record?.possibleTypes) {
const targetRecord = Object.values(form.values)?.[0]?.[field.index];
const targetType = targetRecord?.type || record.type;
const newOptions = getInterfaceOptions(initOptions, targetType);
setOptions(newOptions);
}

View File

@ -142,7 +142,7 @@ export const fieldsTableSchema: ISchema = {
type: 'string',
'x-component': 'FieldTitleInput',
'x-component-props': {
handleFieldChange: '{{handleFieldChange}}',
handleFieldChange: '{{enqueueChange}}',
},
},
},
@ -167,7 +167,7 @@ export const fieldsTableSchema: ISchema = {
type: {
'x-component': 'FieldType',
'x-component-props': {
handleFieldChange: '{{handleFieldChange}}',
handleFieldChange: '{{enqueueChange}}',
},
},
},
@ -181,7 +181,7 @@ export const fieldsTableSchema: ISchema = {
interface: {
'x-component': 'CollectionFieldInterfaceSelect',
'x-component-props': {
handleFieldChange: '{{handleFieldChange}}',
handleFieldChange: '{{enqueueChange}}',
},
},
},