From bd5d4489fd257507726f8337e2930616e7b25fc6 Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Wed, 24 Jul 2024 22:03:23 +0800 Subject: [PATCH] refactor: migrate DataBlockCollector to DataBlockProvider (#4938) * refactor: migrate DataBlockCollector to DataBlockProvider * chore: fix build * chore: make unit test pass --- .../src/block-provider/BlockProvider.tsx | 5 +---- .../data-block/DataBlockProvider.tsx | 9 +++++--- .../src/filter-provider/FilterProvider.tsx | 21 ++++++++++--------- .../core/client/src/filter-provider/utils.ts | 12 +++++------ .../SchemaSettingsConnectDataBlocks.tsx | 4 ++-- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/packages/core/client/src/block-provider/BlockProvider.tsx b/packages/core/client/src/block-provider/BlockProvider.tsx index ecb1c38e47..6c53fca0e7 100644 --- a/packages/core/client/src/block-provider/BlockProvider.tsx +++ b/packages/core/client/src/block-provider/BlockProvider.tsx @@ -32,7 +32,6 @@ import { useCollectionManager_deprecated, useCollection_deprecated, } from '../collection-manager'; -import { DataBlockCollector } from '../filter-provider/FilterProvider'; import { useSourceId } from '../modules/blocks/useSourceId'; import { RecordProvider, useRecordIndex } from '../record-provider'; import { useAssociationNames } from './hooks'; @@ -271,9 +270,7 @@ export const BlockProvider = (props: { - - {props.children} - + {props.children} diff --git a/packages/core/client/src/data-source/data-block/DataBlockProvider.tsx b/packages/core/client/src/data-source/data-block/DataBlockProvider.tsx index dd8e6bcab6..71964c2058 100644 --- a/packages/core/client/src/data-source/data-block/DataBlockProvider.tsx +++ b/packages/core/client/src/data-source/data-block/DataBlockProvider.tsx @@ -7,11 +7,11 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ -import { useFieldSchema } from '@formily/react'; import React, { FC, ReactNode, createContext, useContext, useMemo } from 'react'; import { ACLCollectionProvider } from '../../acl/ACLProvider'; import { UseRequestOptions, UseRequestService } from '../../api-client'; +import { DataBlockCollector, FilterParam } from '../../filter-provider/FilterProvider'; import { withDynamicSchemaProps } from '../../hoc/withDynamicSchemaProps'; import { Designable, useDesignable } from '../../schema-component'; import { @@ -34,6 +34,7 @@ export interface AllDataBlockProps { action?: 'list' | 'get'; params?: { filterByTk?: string | number; + filter?: FilterParam; [index: string]: any; }; parentRecord?: CollectionRecord; @@ -150,7 +151,7 @@ export const AssociationOrCollectionProvider = (props: { ); }; -export const DataBlockProvider: FC = withDynamicSchemaProps( +export const DataBlockProvider: FC> = withDynamicSchemaProps( (props) => { const { collection, association, dataSource, children, hidden, ...resets } = props as Partial; const { dn } = useDesignable(); @@ -168,7 +169,9 @@ export const DataBlockProvider: FC - {children} + + {children} + diff --git a/packages/core/client/src/filter-provider/FilterProvider.tsx b/packages/core/client/src/filter-provider/FilterProvider.tsx index 443dade674..73e665204c 100644 --- a/packages/core/client/src/filter-provider/FilterProvider.tsx +++ b/packages/core/client/src/filter-provider/FilterProvider.tsx @@ -10,8 +10,10 @@ import { useField, useFieldSchema } from '@formily/react'; import { uniqBy } from 'lodash'; import React, { createContext, useCallback, useEffect, useRef } from 'react'; -import { useBlockRequestContext } from '../block-provider/BlockProvider'; -import { CollectionFieldOptions_deprecated, useCollection_deprecated } from '../collection-manager'; +import { CollectionFieldOptions_deprecated } from '../collection-manager'; +import { Collection } from '../data-source/collection/Collection'; +import { useCollection } from '../data-source/collection/CollectionProvider'; +import { useDataBlockRequest } from '../data-source/data-block/DataBlockRequestProvider'; import { useDataLoadingMode } from '../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem'; import { removeNullCondition } from '../schema-component'; import { mergeFilter, useAssociatedFields } from './utils'; @@ -37,8 +39,6 @@ export interface ForeignKeyField { [key: string]: any; } -type Collection = ReturnType; - export interface DataBlock { /** 唯一标识符,schema 中的 name 值 */ uid: string; @@ -97,11 +97,11 @@ export const DataBlockCollector = ({ params, }: { children: React.ReactNode; - params?: { filter: FilterParam }; + params?: { filter?: FilterParam }; }) => { - const collection = useCollection_deprecated(); + const collection = useCollection(); const { recordDataBlocks } = useFilterBlock(); - const { service } = useBlockRequestContext(); + const service = useDataBlockRequest(); const field = useField(); const fieldSchema = useFieldSchema(); const associatedFields = useAssociatedFields(); @@ -109,6 +109,7 @@ export const DataBlockCollector = ({ const dataLoadingMode = useDataLoadingMode(); const shouldApplyFilter = + field && field.decoratorType !== 'FilterFormBlockProvider' && field.decoratorType !== 'FormBlockProvider' && field.decoratorProps.blockType !== 'filter'; @@ -117,10 +118,10 @@ export const DataBlockCollector = ({ recordDataBlocks({ uid: fieldSchema['x-uid'], title: field.componentProps.title, - doFilter: service.runAsync, + doFilter: service.runAsync as any, collection, associatedFields, - foreignKeyFields: collection.foreignKeyFields as ForeignKeyField[], + foreignKeyFields: collection.getFields('isForeignKey') as ForeignKeyField[], defaultFilter: params?.filter || {}, service, dom: container.current, @@ -151,7 +152,7 @@ export const DataBlockCollector = ({ associatedFields, collection, dataLoadingMode, - field.componentProps.title, + field?.componentProps?.title, fieldSchema, params?.filter, recordDataBlocks, diff --git a/packages/core/client/src/filter-provider/utils.ts b/packages/core/client/src/filter-provider/utils.ts index 60e9f66622..104631ff6b 100644 --- a/packages/core/client/src/filter-provider/utils.ts +++ b/packages/core/client/src/filter-provider/utils.ts @@ -14,11 +14,12 @@ import { useCallback, useEffect, useState } from 'react'; import { FilterTarget, findFilterTargets } from '../block-provider/hooks'; import { CollectionFieldOptions_deprecated, - Collection_deprecated, FieldOptions, useCollectionManager_deprecated, useCollection_deprecated, } from '../collection-manager'; +import { Collection } from '../data-source/collection/Collection'; +import { useCollection } from '../data-source/collection/CollectionProvider'; import { removeNullCondition } from '../schema-component'; import { DataBlock, useFilterBlock } from './FilterProvider'; @@ -50,10 +51,7 @@ export const getSupportFieldsByAssociation = (inheritCollectionsChain: string[], ); }; -export const getSupportFieldsByForeignKey = ( - filterBlockCollection: ReturnType, - block: DataBlock, -) => { +export const getSupportFieldsByForeignKey = (filterBlockCollection: Collection, block: DataBlock) => { return block.foreignKeyFields?.filter((foreignKeyField) => { return filterBlockCollection.fields.some( (field) => field.type !== 'belongsTo' && field.foreignKey === foreignKeyField.name, @@ -69,7 +67,7 @@ export const getSupportFieldsByForeignKey = ( export const useSupportedBlocks = (filterBlockType: FilterBlockType) => { const { getDataBlocks } = useFilterBlock(); const fieldSchema = useFieldSchema(); - const collection = useCollection_deprecated(); + const collection = useCollection(); const { getAllCollectionsInheritChain } = useCollectionManager_deprecated(); // Form 和 Collapse 仅支持同表的数据区块 @@ -174,7 +172,7 @@ export const isAssocField = (field?: FieldOptions) => { ); }; -export const isSameCollection = (c1: Collection_deprecated, c2: Collection_deprecated) => { +export const isSameCollection = (c1: Collection, c2: Collection) => { return c1.name === c2.name && c1.dataSource === c2.dataSource; }; diff --git a/packages/core/client/src/schema-settings/SchemaSettingsConnectDataBlocks.tsx b/packages/core/client/src/schema-settings/SchemaSettingsConnectDataBlocks.tsx index 5600157c6d..87b91fd868 100644 --- a/packages/core/client/src/schema-settings/SchemaSettingsConnectDataBlocks.tsx +++ b/packages/core/client/src/schema-settings/SchemaSettingsConnectDataBlocks.tsx @@ -14,7 +14,7 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; import { findFilterTargets, updateFilterTargets } from '../block-provider/hooks'; import { useCollectionManager_deprecated } from '../collection-manager/hooks/useCollectionManager_deprecated'; -import { useCollection_deprecated } from '../collection-manager/hooks/useCollection_deprecated'; +import { useCollection } from '../data-source/collection/CollectionProvider'; import { useFilterBlock } from '../filter-provider/FilterProvider'; import { getSupportFieldsByAssociation, @@ -37,7 +37,7 @@ export function SchemaSettingsConnectDataBlocks(props) { const fieldSchema = useFieldSchema(); const { dn } = useDesignable(); const { t } = useTranslation(); - const collection = useCollection_deprecated(); + const collection = useCollection(); const { inProvider } = useFilterBlock(); const dataBlocks = useSupportedBlocks(type); // eslint-disable-next-line prefer-const