refactor: migrate DataBlockCollector to DataBlockProvider (#4938)
Some checks failed
auto-merge / push-commit (push) Waiting to run
Build Docker Image / build-and-push (push) Waiting to run
Build Pro Image / build-and-push (push) Waiting to run
deploy client docs / Build (push) Waiting to run
E2E / Build (push) Waiting to run
E2E / Core and plugins (push) Blocked by required conditions
E2E / plugin-workflow (push) Blocked by required conditions
E2E / plugin-workflow-approval (push) Blocked by required conditions
E2E / plugin-data-source-main (push) Blocked by required conditions
E2E / Comment on PR (push) Blocked by required conditions
NocoBase FrontEnd Test / frontend-test (18) (push) Waiting to run
NocoBase Backend Test / sqlite-test (20, false) (push) Has been cancelled
NocoBase Backend Test / sqlite-test (20, true) (push) Has been cancelled
NocoBase Backend Test / postgres-test (public, 20, nocobase, false) (push) Has been cancelled
NocoBase Backend Test / postgres-test (public, 20, nocobase, true) (push) Has been cancelled
NocoBase Backend Test / postgres-test (public, 20, public, false) (push) Has been cancelled
NocoBase Backend Test / postgres-test (public, 20, public, true) (push) Has been cancelled
NocoBase Backend Test / postgres-test (user_schema, 20, nocobase, false) (push) Has been cancelled
NocoBase Backend Test / postgres-test (user_schema, 20, nocobase, true) (push) Has been cancelled
NocoBase Backend Test / postgres-test (user_schema, 20, public, false) (push) Has been cancelled
NocoBase Backend Test / postgres-test (user_schema, 20, public, true) (push) Has been cancelled
NocoBase Backend Test / mysql-test (20, false) (push) Has been cancelled
NocoBase Backend Test / mysql-test (20, true) (push) Has been cancelled
NocoBase Backend Test / mariadb-test (20, false) (push) Has been cancelled
NocoBase Backend Test / mariadb-test (20, true) (push) Has been cancelled
Test on Windows / build (push) Has been cancelled

* refactor: migrate DataBlockCollector to DataBlockProvider

* chore: fix build

* chore: make unit test pass
This commit is contained in:
Zeke Zhang 2024-07-24 22:03:23 +08:00 committed by GitHub
parent 80570c663f
commit bd5d4489fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 26 deletions

View File

@ -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: {
<BlockContext.Provider value={blockValue}>
<DataBlockProvider {...(props as any)} params={params} parentRecord={parentRecord || parentRecordFromHook}>
<BlockRequestProvider_deprecated {...props} updateAssociationValues={updateAssociationValues} params={params}>
<DataBlockCollector {...props} params={params}>
{props.children}
</DataBlockCollector>
{props.children}
</BlockRequestProvider_deprecated>
</DataBlockProvider>
</BlockContext.Provider>

View File

@ -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<DataBlockProviderProps & { children?: ReactNode }> = withDynamicSchemaProps(
export const DataBlockProvider: FC<Partial<AllDataBlockProps>> = withDynamicSchemaProps(
(props) => {
const { collection, association, dataSource, children, hidden, ...resets } = props as Partial<AllDataBlockProps>;
const { dn } = useDesignable();
@ -168,7 +169,9 @@ export const DataBlockProvider: FC<DataBlockProviderProps & { children?: ReactNo
<AssociationOrCollectionProvider collection={collection} association={association}>
<ACLCollectionProvider>
<DataBlockResourceProvider>
<BlockRequestProvider>{children}</BlockRequestProvider>
<BlockRequestProvider>
<DataBlockCollector params={props.params}>{children}</DataBlockCollector>
</BlockRequestProvider>
</DataBlockResourceProvider>
</ACLCollectionProvider>
</AssociationOrCollectionProvider>

View File

@ -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<typeof useCollection_deprecated>;
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,

View File

@ -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<typeof useCollection_deprecated>,
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;
};

View File

@ -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