refactor: remove SharedFilterProvider (#3424)

This commit is contained in:
Zeke Zhang 2024-01-30 14:17:44 +08:00 committed by GitHub
parent 644039ba53
commit bdaa2197a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 46 additions and 111 deletions

View File

@ -19,7 +19,6 @@ import { ACLCollectionProvider } from '../acl/ACLProvider';
import { CollectionProvider, useCollection, useCollectionManager } from '../collection-manager'; import { CollectionProvider, useCollection, useCollectionManager } from '../collection-manager';
import { FilterBlockRecord } from '../filter-provider/FilterProvider'; import { FilterBlockRecord } from '../filter-provider/FilterProvider';
import { useRecordIndex } from '../record-provider'; import { useRecordIndex } from '../record-provider';
import { SharedFilterProvider } from './SharedFilterProvider';
import { useTemplateBlockContext } from './TemplateBlockProvider'; import { useTemplateBlockContext } from './TemplateBlockProvider';
import { useAssociationNames } from './hooks'; import { useAssociationNames } from './hooks';
@ -321,11 +320,9 @@ export const BlockProvider = (props: {
<BlockAssociationContext.Provider value={association}> <BlockAssociationContext.Provider value={association}>
<BlockResourceContext.Provider value={resource}> <BlockResourceContext.Provider value={resource}>
<BlockRequestProvider {...props} updateAssociationValues={updateAssociationValues} params={params}> <BlockRequestProvider {...props} updateAssociationValues={updateAssociationValues} params={params}>
<SharedFilterProvider {...props} params={params}>
<FilterBlockRecord {...props} params={params}> <FilterBlockRecord {...props} params={params}>
{props.children} {props.children}
</FilterBlockRecord> </FilterBlockRecord>
</SharedFilterProvider>
</BlockRequestProvider> </BlockRequestProvider>
</BlockResourceContext.Provider> </BlockResourceContext.Provider>
</BlockAssociationContext.Provider> </BlockAssociationContext.Provider>

View File

@ -1,81 +0,0 @@
import React, { createContext, FC, useState } from 'react';
export enum SHARED_FILTER_CONDITION {
AND = '$and',
OR = '$or',
}
export type SharedFilter = {
[K in SHARED_FILTER_CONDITION]?: any;
};
export type SharedFilterStore = Record<string, SharedFilter>;
export type SharedFilterContextValue = {
sharedFilterStore: SharedFilter;
setSharedFilterStore: (filterStore: SharedFilterStore) => void;
getFilterParams: (filterStore?: SharedFilterStore) => any;
};
export const mergeFilter = (filters: any[], op = '$and') => {
const items = filters.filter((f) => {
if (f && typeof f === 'object' && !Array.isArray(f)) {
return Object.values(f).filter((v) => v !== undefined).length;
}
});
if (items.length === 0) {
return {};
}
if (items.length === 1) {
return items[0];
}
return { [op]: items };
};
export const getFilterParams = (filterStore?: SharedFilterStore) => {
const newAssociationFilterList = Object.entries(filterStore).map(([key, filter]) => filter);
const newAssociationFilter = newAssociationFilterList.length
? {
$and: newAssociationFilterList,
}
: {};
return newAssociationFilter;
};
export const SharedFilterContext = createContext<SharedFilterContextValue>({
sharedFilterStore: {},
setSharedFilterStore: () => {},
getFilterParams,
});
export const concatFilter = (f1: SharedFilter, f2: SharedFilter): SharedFilter => {
const newAnd = [f1?.$and, f2?.$and].filter((i) => i).reduce((pre, cur) => pre.concat(cur), []);
const newOr = [f1?.$or, f2?.$or].filter((i) => i).reduce((pre, cur) => pre.concat(cur), []);
const newFilter: SharedFilter = {};
newAnd.length && (newFilter.$and = newAnd);
newOr.length && (newFilter.$or = newOr);
return newFilter;
};
export const SharedFilterProvider: FC<{ params?: any }> = (props) => {
const [sharedFilterStore, setSharedFilterStoreUnwrap] = useState<Record<string, SharedFilter>>({});
const setSharedFilterStore = (associationFilter: Record<string, SharedFilter>) => {
setSharedFilterStoreUnwrap(associationFilter);
};
const getFilterParamsWrap = (filterStore?: SharedFilterStore) => getFilterParams(filterStore ?? sharedFilterStore);
return (
<SharedFilterContext.Provider
value={{
sharedFilterStore,
setSharedFilterStore,
getFilterParams: getFilterParamsWrap,
}}
>
{props.children}
</SharedFilterContext.Provider>
);
};

View File

@ -3,10 +3,10 @@ import { FormContext, useField, useFieldSchema } from '@formily/react';
import React, { createContext, useContext, useEffect, useMemo, useState } from 'react'; import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
import { useCollectionManager } from '../collection-manager'; import { useCollectionManager } from '../collection-manager';
import { useFilterBlock } from '../filter-provider/FilterProvider'; import { useFilterBlock } from '../filter-provider/FilterProvider';
import { mergeFilter } from '../filter-provider/utils';
import { useRecord } from '../record-provider'; import { useRecord } from '../record-provider';
import { FixedBlockWrapper, SchemaComponentOptions, removeNullCondition } from '../schema-component'; import { FixedBlockWrapper, SchemaComponentOptions, removeNullCondition } from '../schema-component';
import { BlockProvider, RenderChildrenWithAssociationFilter, useBlockRequestContext } from './BlockProvider'; import { BlockProvider, RenderChildrenWithAssociationFilter, useBlockRequestContext } from './BlockProvider';
import { mergeFilter } from './SharedFilterProvider';
import { findFilterTargets, useParsedFilter } from './hooks'; import { findFilterTargets, useParsedFilter } from './hooks';
export const TableBlockContext = createContext<any>({}); export const TableBlockContext = createContext<any>({});

View File

@ -5,10 +5,10 @@ import uniq from 'lodash/uniq';
import React, { createContext, useContext, useEffect, useState } from 'react'; import React, { createContext, useContext, useEffect, useState } from 'react';
import { useCollectionManager } from '../collection-manager'; import { useCollectionManager } from '../collection-manager';
import { isInFilterFormBlock } from '../filter-provider'; import { isInFilterFormBlock } from '../filter-provider';
import { mergeFilter } from '../filter-provider/utils';
import { RecordProvider, useRecord } from '../record-provider'; import { RecordProvider, useRecord } from '../record-provider';
import { SchemaComponentOptions } from '../schema-component'; import { SchemaComponentOptions } from '../schema-component';
import { BlockProvider, RenderChildrenWithAssociationFilter, useBlockRequestContext } from './BlockProvider'; import { BlockProvider, RenderChildrenWithAssociationFilter, useBlockRequestContext } from './BlockProvider';
import { mergeFilter } from './SharedFilterProvider';
import { useParsedFilter } from './hooks'; import { useParsedFilter } from './hooks';
type Params = { type Params = {

View File

@ -12,7 +12,7 @@ import { AssociationFilter, useFormActiveFields, useFormBlockContext, useTableBl
import { useAPIClient, useRequest } from '../../api-client'; import { useAPIClient, useRequest } from '../../api-client';
import { useCollection, useCollectionManager } from '../../collection-manager'; import { useCollection, useCollectionManager } from '../../collection-manager';
import { useFilterBlock } from '../../filter-provider/FilterProvider'; import { useFilterBlock } from '../../filter-provider/FilterProvider';
import { transformToFilter } from '../../filter-provider/utils'; import { mergeFilter, transformToFilter } from '../../filter-provider/utils';
import { useRecord } from '../../record-provider'; import { useRecord } from '../../record-provider';
import { removeNullCondition, useActionContext, useCompile } from '../../schema-component'; import { removeNullCondition, useActionContext, useCompile } from '../../schema-component';
import { useCurrentUserContext } from '../../user'; import { useCurrentUserContext } from '../../user';
@ -21,7 +21,6 @@ import { isVariable } from '../../variables/utils/isVariable';
import { transformVariableValue } from '../../variables/utils/transformVariableValue'; import { transformVariableValue } from '../../variables/utils/transformVariableValue';
import { useBlockRequestContext, useFilterByTk, useParamsFromRecord } from '../BlockProvider'; import { useBlockRequestContext, useFilterByTk, useParamsFromRecord } from '../BlockProvider';
import { useDetailsBlockContext } from '../DetailsBlockProvider'; import { useDetailsBlockContext } from '../DetailsBlockProvider';
import { mergeFilter } from '../SharedFilterProvider';
import { TableFieldResource } from '../TableFieldProvider'; import { TableFieldResource } from '../TableFieldProvider';
export * from './useFormActiveFields'; export * from './useFormActiveFields';

View File

@ -2,10 +2,8 @@ export * from './BlockProvider';
export * from './BlockSchemaComponentProvider'; export * from './BlockSchemaComponentProvider';
export * from './FilterFormBlockProvider'; export * from './FilterFormBlockProvider';
export * from './FormBlockProvider'; export * from './FormBlockProvider';
export * from './SharedFilterProvider'; export * from './FormFieldProvider';
export * from './TableBlockProvider'; export * from './TableBlockProvider';
export * from './TableFieldProvider'; export * from './TableFieldProvider';
export * from './TableSelectorProvider'; export * from './TableSelectorProvider';
export * from './FormFieldProvider';
export * from './SharedFilterProvider';
export * from './hooks'; export * from './hooks';

View File

@ -2,10 +2,18 @@ import { useField, useFieldSchema } from '@formily/react';
import { uniqBy } from 'lodash'; import { uniqBy } from 'lodash';
import React, { createContext, useEffect, useRef } from 'react'; import React, { createContext, useEffect, useRef } from 'react';
import { useBlockRequestContext } from '../block-provider/BlockProvider'; import { useBlockRequestContext } from '../block-provider/BlockProvider';
import { SharedFilter, mergeFilter } from '../block-provider/SharedFilterProvider';
import { CollectionFieldOptions, useCollection } from '../collection-manager'; import { CollectionFieldOptions, useCollection } from '../collection-manager';
import { removeNullCondition } from '../schema-component'; import { removeNullCondition } from '../schema-component';
import { useAssociatedFields } from './utils'; import { mergeFilter, useAssociatedFields } from './utils';
enum FILTER_OPERATOR {
AND = '$and',
OR = '$or',
}
export type FilterParam = {
[K in FILTER_OPERATOR]?: any;
};
export interface ForeignKeyField { export interface ForeignKeyField {
/** 外键字段所在的数据表的名称 */ /** 外键字段所在的数据表的名称 */
@ -37,7 +45,7 @@ export interface DataBlock {
/** 数据区块表中所有的外键字段 */ /** 数据区块表中所有的外键字段 */
foreignKeyFields?: ForeignKeyField[]; foreignKeyFields?: ForeignKeyField[];
/** 数据区块已经存在的过滤条件(通过 `设置数据范围` 或者其它能设置筛选条件的功能) */ /** 数据区块已经存在的过滤条件(通过 `设置数据范围` 或者其它能设置筛选条件的功能) */
defaultFilter?: SharedFilter; defaultFilter?: FilterParam;
/** 数据区块用于请求数据的接口 */ /** 数据区块用于请求数据的接口 */
service?: any; service?: any;
/** 数据区块所的 DOM 容器 */ /** 数据区块所的 DOM 容器 */
@ -71,7 +79,7 @@ export const FilterBlockRecord = ({
params, params,
}: { }: {
children: React.ReactNode; children: React.ReactNode;
params?: { filter: SharedFilter }; params?: { filter: FilterParam };
}) => { }) => {
const collection = useCollection(); const collection = useCollection();
const { recordDataBlocks, removeDataBlock } = useFilterBlock(); const { recordDataBlocks, removeDataBlock } = useFilterBlock();

View File

@ -2,7 +2,6 @@ import { Schema, useFieldSchema } from '@formily/react';
import { flatten, getValuesByPath } from '@nocobase/utils/client'; import { flatten, getValuesByPath } from '@nocobase/utils/client';
import _ from 'lodash'; import _ from 'lodash';
import { useCallback, useEffect, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { mergeFilter } from '../block-provider';
import { FilterTarget, findFilterTargets } from '../block-provider/hooks'; import { FilterTarget, findFilterTargets } from '../block-provider/hooks';
import { import {
Collection, Collection,
@ -22,6 +21,21 @@ export enum FilterBlockType {
COLLAPSE, COLLAPSE,
} }
export const mergeFilter = (filters: any[], op = '$and') => {
const items = filters.filter((f) => {
if (f && typeof f === 'object' && !Array.isArray(f)) {
return Object.values(f).filter((v) => v !== undefined).length;
}
});
if (items.length === 0) {
return {};
}
if (items.length === 1) {
return items[0];
}
return { [op]: items };
};
export const getSupportFieldsByAssociation = (inheritCollectionsChain: string[], block: DataBlock) => { export const getSupportFieldsByAssociation = (inheritCollectionsChain: string[], block: DataBlock) => {
return block.associatedFields?.filter((field) => return block.associatedFields?.filter((field) =>
inheritCollectionsChain.some((collectionName) => collectionName === field.target), inheritCollectionsChain.some((collectionName) => collectionName === field.target),

View File

@ -7,7 +7,7 @@ import dayjs from 'dayjs';
import React, { useCallback, useEffect, useMemo, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useAPIClient, useCollectionManager } from '../../../'; import { useAPIClient, useCollectionManager } from '../../../';
import { mergeFilter } from '../../../block-provider/SharedFilterProvider'; import { mergeFilter } from '../../../filter-provider/utils';
import { SchemaComponent, useCompile } from '../../../schema-component'; import { SchemaComponent, useCompile } from '../../../schema-component';
import useServiceOptions, { useAssociationFieldContext } from './hooks'; import useServiceOptions, { useAssociationFieldContext } from './hooks';

View File

@ -5,9 +5,9 @@ import { flatten, getValuesByPath } from '@nocobase/utils/client';
import _, { isString } from 'lodash'; import _, { isString } from 'lodash';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
import { useCollection, useCollectionManager } from '../../../collection-manager'; import { useCollection, useCollectionManager } from '../../../collection-manager';
import { isInFilterFormBlock } from '../../../filter-provider'; import { isInFilterFormBlock } from '../../../filter-provider';
import { mergeFilter } from '../../../filter-provider/utils';
import { useRecord } from '../../../record-provider'; import { useRecord } from '../../../record-provider';
import { useParseDataScopeFilter } from '../../../schema-settings'; import { useParseDataScopeFilter } from '../../../schema-settings';
import { DEBOUNCE_WAIT } from '../../../variables'; import { DEBOUNCE_WAIT } from '../../../variables';

View File

@ -1,11 +1,11 @@
import { Schema, useFieldSchema } from '@formily/react'; import { Schema, useFieldSchema } from '@formily/react';
import React, { useContext } from 'react'; import React, { useContext } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { SchemaComponentContext, createDesignable } from '../..';
import { useAPIClient } from '../../../api-client'; import { useAPIClient } from '../../../api-client';
import { createDesignable, SchemaComponentContext, useDesignable } from '../..';
import { ActionInitializer } from '../../../schema-initializer/items/ActionInitializer';
import { useBlockRequestContext } from '../../../block-provider'; import { useBlockRequestContext } from '../../../block-provider';
import { mergeFilter } from '../../../block-provider/SharedFilterProvider'; import { mergeFilter } from '../../../filter-provider/utils';
import { ActionInitializer } from '../../../schema-initializer/items/ActionInitializer';
export const ActionBarAssociationFilterAction = (props) => { export const ActionBarAssociationFilterAction = (props) => {
const { refresh } = useContext(SchemaComponentContext); const { refresh } = useContext(SchemaComponentContext);

View File

@ -1,8 +1,8 @@
import { useFieldSchema } from '@formily/react'; import { useFieldSchema } from '@formily/react';
import { useCallback, useMemo } from 'react'; import { useCallback, useMemo } from 'react';
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
import { useCollection, useCollectionManager } from '../../../collection-manager'; import { useCollection, useCollectionManager } from '../../../collection-manager';
import { isInFilterFormBlock } from '../../../filter-provider'; import { isInFilterFormBlock } from '../../../filter-provider';
import { mergeFilter } from '../../../filter-provider/utils';
import { useRecord } from '../../../record-provider'; import { useRecord } from '../../../record-provider';
export default function useServiceOptions(props) { export default function useServiceOptions(props) {

View File

@ -3,8 +3,8 @@ import { useField, useFieldSchema } from '@formily/react';
import flat from 'flat'; import flat from 'flat';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useBlockRequestContext } from '../../../block-provider'; import { useBlockRequestContext } from '../../../block-provider';
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
import { useCollection, useCollectionManager } from '../../../collection-manager'; import { useCollection, useCollectionManager } from '../../../collection-manager';
import { mergeFilter } from '../../../filter-provider/utils';
export const useGetFilterOptions = () => { export const useGetFilterOptions = () => {
const { getCollectionFields } = useCollectionManager(); const { getCollectionFields } = useCollectionManager();

View File

@ -5,8 +5,8 @@ import dayjs from 'dayjs';
import { uniqBy } from 'lodash'; import { uniqBy } from 'lodash';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { ResourceActionOptions, useRequest } from '../../../api-client'; import { ResourceActionOptions, useRequest } from '../../../api-client';
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
import { useCollection, useCollectionManager } from '../../../collection-manager'; import { useCollection, useCollectionManager } from '../../../collection-manager';
import { mergeFilter } from '../../../filter-provider/utils';
import { useCompile } from '../../hooks'; import { useCompile } from '../../hooks';
import { Select, defaultFieldNames } from '../select'; import { Select, defaultFieldNames } from '../select';
import { ReadPretty } from './ReadPretty'; import { ReadPretty } from './ReadPretty';

View File

@ -4,10 +4,9 @@ import React, { useCallback } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useAPIClient } from '../../../api-client'; import { useAPIClient } from '../../../api-client';
import { useFormBlockContext, useTableBlockContext } from '../../../block-provider'; import { useFormBlockContext, useTableBlockContext } from '../../../block-provider';
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
import { useCollection, useCollectionManager } from '../../../collection-manager'; import { useCollection, useCollectionManager } from '../../../collection-manager';
import { useSortFields } from '../../../collection-manager/action-hooks'; import { useSortFields } from '../../../collection-manager/action-hooks';
import { FilterBlockType } from '../../../filter-provider/utils'; import { FilterBlockType, mergeFilter } from '../../../filter-provider/utils';
import { RecordProvider, useRecord } from '../../../record-provider'; import { RecordProvider, useRecord } from '../../../record-provider';
import { import {
GeneralSchemaDesigner, GeneralSchemaDesigner,

View File

@ -5,8 +5,8 @@ import { Tree as AntdTree } from 'antd';
import _ from 'lodash'; import _ from 'lodash';
import React, { useMemo } from 'react'; import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { mergeFilter } from '../../block-provider';
import { useCollectionManager } from '../../collection-manager'; import { useCollectionManager } from '../../collection-manager';
import { mergeFilter } from '../../filter-provider/utils';
import { SchemaComponent, SchemaComponentContext, removeNullCondition } from '../../schema-component'; import { SchemaComponent, SchemaComponentContext, removeNullCondition } from '../../schema-component';
import { ITemplate } from '../../schema-component/antd/form-v2/Templates'; import { ITemplate } from '../../schema-component/antd/form-v2/Templates';
import { VariableInput } from '../VariableInput'; import { VariableInput } from '../VariableInput';

View File

@ -6,8 +6,9 @@ import _ from 'lodash';
import React from 'react'; import React from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { GeneralSchemaDesigner, SchemaSettingsDataScope, SchemaSettingsItem } from '../..'; import { GeneralSchemaDesigner, SchemaSettingsDataScope, SchemaSettingsItem } from '../..';
import { mergeFilter, useFormBlockContext } from '../../../block-provider'; import { useFormBlockContext } from '../../../block-provider';
import { useCollectionManager } from '../../../collection-manager'; import { useCollectionManager } from '../../../collection-manager';
import { mergeFilter } from '../../../filter-provider/utils';
import { removeNullCondition, useCompile, useDesignable } from '../../../schema-component'; import { removeNullCondition, useCompile, useDesignable } from '../../../schema-component';
import { ITemplate } from '../../../schema-component/antd/form-v2/Templates'; import { ITemplate } from '../../../schema-component/antd/form-v2/Templates';