mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 08:55:33 +00:00
refactor: remove SharedFilterProvider (#3424)
This commit is contained in:
parent
644039ba53
commit
bdaa2197a7
@ -19,7 +19,6 @@ import { ACLCollectionProvider } from '../acl/ACLProvider';
|
||||
import { CollectionProvider, useCollection, useCollectionManager } from '../collection-manager';
|
||||
import { FilterBlockRecord } from '../filter-provider/FilterProvider';
|
||||
import { useRecordIndex } from '../record-provider';
|
||||
import { SharedFilterProvider } from './SharedFilterProvider';
|
||||
import { useTemplateBlockContext } from './TemplateBlockProvider';
|
||||
import { useAssociationNames } from './hooks';
|
||||
|
||||
@ -321,11 +320,9 @@ export const BlockProvider = (props: {
|
||||
<BlockAssociationContext.Provider value={association}>
|
||||
<BlockResourceContext.Provider value={resource}>
|
||||
<BlockRequestProvider {...props} updateAssociationValues={updateAssociationValues} params={params}>
|
||||
<SharedFilterProvider {...props} params={params}>
|
||||
<FilterBlockRecord {...props} params={params}>
|
||||
{props.children}
|
||||
</FilterBlockRecord>
|
||||
</SharedFilterProvider>
|
||||
<FilterBlockRecord {...props} params={params}>
|
||||
{props.children}
|
||||
</FilterBlockRecord>
|
||||
</BlockRequestProvider>
|
||||
</BlockResourceContext.Provider>
|
||||
</BlockAssociationContext.Provider>
|
||||
|
@ -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>
|
||||
);
|
||||
};
|
@ -3,10 +3,10 @@ import { FormContext, useField, useFieldSchema } from '@formily/react';
|
||||
import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
|
||||
import { useCollectionManager } from '../collection-manager';
|
||||
import { useFilterBlock } from '../filter-provider/FilterProvider';
|
||||
import { mergeFilter } from '../filter-provider/utils';
|
||||
import { useRecord } from '../record-provider';
|
||||
import { FixedBlockWrapper, SchemaComponentOptions, removeNullCondition } from '../schema-component';
|
||||
import { BlockProvider, RenderChildrenWithAssociationFilter, useBlockRequestContext } from './BlockProvider';
|
||||
import { mergeFilter } from './SharedFilterProvider';
|
||||
import { findFilterTargets, useParsedFilter } from './hooks';
|
||||
|
||||
export const TableBlockContext = createContext<any>({});
|
||||
|
@ -5,10 +5,10 @@ import uniq from 'lodash/uniq';
|
||||
import React, { createContext, useContext, useEffect, useState } from 'react';
|
||||
import { useCollectionManager } from '../collection-manager';
|
||||
import { isInFilterFormBlock } from '../filter-provider';
|
||||
import { mergeFilter } from '../filter-provider/utils';
|
||||
import { RecordProvider, useRecord } from '../record-provider';
|
||||
import { SchemaComponentOptions } from '../schema-component';
|
||||
import { BlockProvider, RenderChildrenWithAssociationFilter, useBlockRequestContext } from './BlockProvider';
|
||||
import { mergeFilter } from './SharedFilterProvider';
|
||||
import { useParsedFilter } from './hooks';
|
||||
|
||||
type Params = {
|
||||
|
@ -12,7 +12,7 @@ import { AssociationFilter, useFormActiveFields, useFormBlockContext, useTableBl
|
||||
import { useAPIClient, useRequest } from '../../api-client';
|
||||
import { useCollection, useCollectionManager } from '../../collection-manager';
|
||||
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 { removeNullCondition, useActionContext, useCompile } from '../../schema-component';
|
||||
import { useCurrentUserContext } from '../../user';
|
||||
@ -21,7 +21,6 @@ import { isVariable } from '../../variables/utils/isVariable';
|
||||
import { transformVariableValue } from '../../variables/utils/transformVariableValue';
|
||||
import { useBlockRequestContext, useFilterByTk, useParamsFromRecord } from '../BlockProvider';
|
||||
import { useDetailsBlockContext } from '../DetailsBlockProvider';
|
||||
import { mergeFilter } from '../SharedFilterProvider';
|
||||
import { TableFieldResource } from '../TableFieldProvider';
|
||||
|
||||
export * from './useFormActiveFields';
|
||||
|
@ -2,10 +2,8 @@ export * from './BlockProvider';
|
||||
export * from './BlockSchemaComponentProvider';
|
||||
export * from './FilterFormBlockProvider';
|
||||
export * from './FormBlockProvider';
|
||||
export * from './SharedFilterProvider';
|
||||
export * from './FormFieldProvider';
|
||||
export * from './TableBlockProvider';
|
||||
export * from './TableFieldProvider';
|
||||
export * from './TableSelectorProvider';
|
||||
export * from './FormFieldProvider';
|
||||
export * from './SharedFilterProvider';
|
||||
export * from './hooks';
|
||||
|
@ -2,10 +2,18 @@ import { useField, useFieldSchema } from '@formily/react';
|
||||
import { uniqBy } from 'lodash';
|
||||
import React, { createContext, useEffect, useRef } from 'react';
|
||||
import { useBlockRequestContext } from '../block-provider/BlockProvider';
|
||||
import { SharedFilter, mergeFilter } from '../block-provider/SharedFilterProvider';
|
||||
import { CollectionFieldOptions, useCollection } from '../collection-manager';
|
||||
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 {
|
||||
/** 外键字段所在的数据表的名称 */
|
||||
@ -37,7 +45,7 @@ export interface DataBlock {
|
||||
/** 数据区块表中所有的外键字段 */
|
||||
foreignKeyFields?: ForeignKeyField[];
|
||||
/** 数据区块已经存在的过滤条件(通过 `设置数据范围` 或者其它能设置筛选条件的功能) */
|
||||
defaultFilter?: SharedFilter;
|
||||
defaultFilter?: FilterParam;
|
||||
/** 数据区块用于请求数据的接口 */
|
||||
service?: any;
|
||||
/** 数据区块所的 DOM 容器 */
|
||||
@ -71,7 +79,7 @@ export const FilterBlockRecord = ({
|
||||
params,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
params?: { filter: SharedFilter };
|
||||
params?: { filter: FilterParam };
|
||||
}) => {
|
||||
const collection = useCollection();
|
||||
const { recordDataBlocks, removeDataBlock } = useFilterBlock();
|
||||
|
@ -2,7 +2,6 @@ import { Schema, useFieldSchema } from '@formily/react';
|
||||
import { flatten, getValuesByPath } from '@nocobase/utils/client';
|
||||
import _ from 'lodash';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { mergeFilter } from '../block-provider';
|
||||
import { FilterTarget, findFilterTargets } from '../block-provider/hooks';
|
||||
import {
|
||||
Collection,
|
||||
@ -22,6 +21,21 @@ export enum FilterBlockType {
|
||||
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) => {
|
||||
return block.associatedFields?.filter((field) =>
|
||||
inheritCollectionsChain.some((collectionName) => collectionName === field.target),
|
||||
|
@ -7,7 +7,7 @@ import dayjs from 'dayjs';
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAPIClient, useCollectionManager } from '../../../';
|
||||
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
|
||||
import { mergeFilter } from '../../../filter-provider/utils';
|
||||
import { SchemaComponent, useCompile } from '../../../schema-component';
|
||||
import useServiceOptions, { useAssociationFieldContext } from './hooks';
|
||||
|
||||
|
@ -5,9 +5,9 @@ import { flatten, getValuesByPath } from '@nocobase/utils/client';
|
||||
import _, { isString } from 'lodash';
|
||||
import cloneDeep from 'lodash/cloneDeep';
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
||||
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
|
||||
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
||||
import { isInFilterFormBlock } from '../../../filter-provider';
|
||||
import { mergeFilter } from '../../../filter-provider/utils';
|
||||
import { useRecord } from '../../../record-provider';
|
||||
import { useParseDataScopeFilter } from '../../../schema-settings';
|
||||
import { DEBOUNCE_WAIT } from '../../../variables';
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { Schema, useFieldSchema } from '@formily/react';
|
||||
import React, { useContext } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { SchemaComponentContext, createDesignable } from '../..';
|
||||
import { useAPIClient } from '../../../api-client';
|
||||
import { createDesignable, SchemaComponentContext, useDesignable } from '../..';
|
||||
import { ActionInitializer } from '../../../schema-initializer/items/ActionInitializer';
|
||||
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) => {
|
||||
const { refresh } = useContext(SchemaComponentContext);
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { useFieldSchema } from '@formily/react';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
|
||||
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
||||
import { isInFilterFormBlock } from '../../../filter-provider';
|
||||
import { mergeFilter } from '../../../filter-provider/utils';
|
||||
import { useRecord } from '../../../record-provider';
|
||||
|
||||
export default function useServiceOptions(props) {
|
||||
|
@ -3,8 +3,8 @@ import { useField, useFieldSchema } from '@formily/react';
|
||||
import flat from 'flat';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useBlockRequestContext } from '../../../block-provider';
|
||||
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
|
||||
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
||||
import { mergeFilter } from '../../../filter-provider/utils';
|
||||
|
||||
export const useGetFilterOptions = () => {
|
||||
const { getCollectionFields } = useCollectionManager();
|
||||
|
@ -5,8 +5,8 @@ import dayjs from 'dayjs';
|
||||
import { uniqBy } from 'lodash';
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { ResourceActionOptions, useRequest } from '../../../api-client';
|
||||
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
|
||||
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
||||
import { mergeFilter } from '../../../filter-provider/utils';
|
||||
import { useCompile } from '../../hooks';
|
||||
import { Select, defaultFieldNames } from '../select';
|
||||
import { ReadPretty } from './ReadPretty';
|
||||
|
@ -4,10 +4,9 @@ import React, { useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAPIClient } from '../../../api-client';
|
||||
import { useFormBlockContext, useTableBlockContext } from '../../../block-provider';
|
||||
import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
|
||||
import { useCollection, useCollectionManager } from '../../../collection-manager';
|
||||
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 {
|
||||
GeneralSchemaDesigner,
|
||||
|
@ -5,8 +5,8 @@ import { Tree as AntdTree } from 'antd';
|
||||
import _ from 'lodash';
|
||||
import React, { useMemo } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { mergeFilter } from '../../block-provider';
|
||||
import { useCollectionManager } from '../../collection-manager';
|
||||
import { mergeFilter } from '../../filter-provider/utils';
|
||||
import { SchemaComponent, SchemaComponentContext, removeNullCondition } from '../../schema-component';
|
||||
import { ITemplate } from '../../schema-component/antd/form-v2/Templates';
|
||||
import { VariableInput } from '../VariableInput';
|
||||
|
@ -6,8 +6,9 @@ import _ from 'lodash';
|
||||
import React from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { GeneralSchemaDesigner, SchemaSettingsDataScope, SchemaSettingsItem } from '../..';
|
||||
import { mergeFilter, useFormBlockContext } from '../../../block-provider';
|
||||
import { useFormBlockContext } from '../../../block-provider';
|
||||
import { useCollectionManager } from '../../../collection-manager';
|
||||
import { mergeFilter } from '../../../filter-provider/utils';
|
||||
import { removeNullCondition, useCompile, useDesignable } from '../../../schema-component';
|
||||
import { ITemplate } from '../../../schema-component/antd/form-v2/Templates';
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user