From 42ad77da68badab86e8596b4a096ec12464f709f Mon Sep 17 00:00:00 2001 From: black Date: Tue, 6 Jun 2023 20:13:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20the=20edit=20drawer's=20audit=20logs=20b?= =?UTF-8?q?lock=20can=20only=20get=20records=20by=20this=20=E2=80=A6=20(#1?= =?UTF-8?q?917)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: rename resource function and add comment * chore: remove AuditLogsDesigner when create audit logs schema --------- Co-authored-by: chenos --- .../src/block-provider/TableBlockProvider.tsx | 35 +++++++- .../schema-component/antd/filter/useValues.ts | 2 +- .../antd/table-v2/TableBlockDesigner.tsx | 14 +++- .../client/src/schema-initializer/utils.ts | 3 + .../src/client/AuditLogsBlockInitializer.tsx | 23 +++++- .../src/client/deplicated/AuditLogs.tsx | 81 +++++++------------ .../plugins/audit-logs/src/client/utils.ts | 17 ++++ 7 files changed, 118 insertions(+), 57 deletions(-) diff --git a/packages/core/client/src/block-provider/TableBlockProvider.tsx b/packages/core/client/src/block-provider/TableBlockProvider.tsx index 8a93794c2..9bd7b612f 100644 --- a/packages/core/client/src/block-provider/TableBlockProvider.tsx +++ b/packages/core/client/src/block-provider/TableBlockProvider.tsx @@ -2,12 +2,13 @@ import { ArrayField, createForm } from '@formily/core'; import { FormContext, Schema, useField, useFieldSchema } from '@formily/react'; import uniq from 'lodash/uniq'; import React, { createContext, useContext, useEffect, useMemo, useState } from 'react'; -import { useCollectionManager } from '../collection-manager'; +import { useCollection, useCollectionManager } from '../collection-manager'; import { useFilterBlock } from '../filter-provider/FilterProvider'; import { FixedBlockWrapper, SchemaComponentOptions, removeNullCondition } from '../schema-component'; import { BlockProvider, RenderChildrenWithAssociationFilter, useBlockRequestContext } from './BlockProvider'; import { mergeFilter } from './SharedFilterProvider'; import { findFilterTargets } from './hooks'; +import { useRecord } from '../record-provider'; export const TableBlockContext = createContext({}); @@ -99,11 +100,18 @@ const useAssociationNames = (collection) => { export const TableBlockProvider = (props) => { const resourceName = props.resource; const params = { ...props.params }; + const record = useRecord(); + const appends = useAssociationNames(props.collection); const fieldSchema = useFieldSchema(); const { getCollection, getCollectionField } = useCollectionManager(); + const parent = useCollection(); + const filter = generateFilterParams(record, parent.name, parent.filterTargetKey, {}); const collection = getCollection(props.collection); const { treeTable } = fieldSchema?.['x-decorator-props'] || {}; + if (filter) { + params.filter = filter; + } if (props.dragSort) { params['sort'] = ['sort']; } @@ -253,3 +261,28 @@ export const useTableBlockProps = () => { }, }; }; + +export const generateFilterParams = (record, parentName, filterTargetKey, defaultFilter) => { + let filter = defaultFilter; + + if (parentName) { + const filterByTk = `${record?.[filterTargetKey || 'id']}`; + if (filter) { + filter = { + $and: [ + filter, + { + collectionName: parentName, + recordId: filterByTk, + }, + ], + }; + } else { + filter = { + collectionName: parentName, + recordId: filterByTk, + }; + } + } + return filter; +}; diff --git a/packages/core/client/src/schema-component/antd/filter/useValues.ts b/packages/core/client/src/schema-component/antd/filter/useValues.ts index 643975c5d..10b4d40db 100644 --- a/packages/core/client/src/schema-component/antd/filter/useValues.ts +++ b/packages/core/client/src/schema-component/antd/filter/useValues.ts @@ -31,7 +31,7 @@ export const useValues = () => { }; const value2data = () => { field.data = field.data || {}; - const values = flat(field.value); + const values = flat(field.value || {}); const path = Object.keys(values).shift() || ''; if (!path || !options) { return; diff --git a/packages/core/client/src/schema-component/antd/table-v2/TableBlockDesigner.tsx b/packages/core/client/src/schema-component/antd/table-v2/TableBlockDesigner.tsx index b5cba5ce1..656c092d1 100644 --- a/packages/core/client/src/schema-component/antd/table-v2/TableBlockDesigner.tsx +++ b/packages/core/client/src/schema-component/antd/table-v2/TableBlockDesigner.tsx @@ -13,6 +13,8 @@ import { useCompile, useDesignable } from '../../hooks'; import { removeNullCondition } from '../filter'; import { FixedBlockDesignerItem } from '../page'; import { FilterDynamicComponent } from './FilterDynamicComponent'; +import cloneDeep from 'lodash/cloneDeep'; +import isEmpty from 'lodash/isEmpty'; export const TableBlockDesigner = () => { const { name, title, sortable } = useCollection(); @@ -25,7 +27,9 @@ export const TableBlockDesigner = () => { const { t } = useTranslation(); const { dn } = useDesignable(); const compile = useCompile(); - const defaultFilter = fieldSchema?.['x-decorator-props']?.params?.filter || {}; + const defaultFilter = removeNullCondition(fieldSchema?.['x-decorator-props']?.params?.filter || {}); + // 当前filter 不需要在 "设置数据范围" 表单里初始化,只需要在查询的时候合并到查询条件 filter中 + const crypticFilter = fieldSchema?.['x-decorator-props']?.params?.crypticFilter; const defaultSort = fieldSchema?.['x-decorator-props']?.params?.sort || []; const defaultResource = fieldSchema?.['x-decorator-props']?.resource; const supportTemplate = !fieldSchema?.['x-decorator-props']?.disableTemplate; @@ -67,12 +71,16 @@ export const TableBlockDesigner = () => { ({ filter }) => { filter = removeNullCondition(filter); const params = field.decoratorProps.params || {}; + let tempFilter = !isEmpty(crypticFilter) ? crypticFilter : filter; + if (!isEmpty(filter)) { + tempFilter = !isEmpty(crypticFilter) ? mergeFilter([filter, tempFilter]) : cloneDeep(filter); + } params.filter = filter; field.decoratorProps.params = params; fieldSchema['x-decorator-props']['params'] = params; const filters = service.params?.[1]?.filters || {}; service.run( - { ...service.params?.[0], filter: mergeFilter([...Object.values(filters), filter]), page: 1 }, + { ...service.params?.[0], filter: mergeFilter([...Object.values(filters), tempFilter]), page: 1 }, { filters }, ); dn.emit('patch', { @@ -82,7 +90,7 @@ export const TableBlockDesigner = () => { }, }); }, - [field], + [field, crypticFilter], ); return ( diff --git a/packages/core/client/src/schema-initializer/utils.ts b/packages/core/client/src/schema-initializer/utils.ts index fa6759d81..e442cea74 100644 --- a/packages/core/client/src/schema-initializer/utils.ts +++ b/packages/core/client/src/schema-initializer/utils.ts @@ -1323,6 +1323,8 @@ export const createTableBlockSchema = (options) => { TableBlockDesigner, blockType, pageSize = 20, + // 当前filter 不需要在 "设置数据范围" 表单里初始化,只需要在查询的时候合并到查询条件 filter中 + crypticFilter = {}, ...others } = options; const schema: ISchema = { @@ -1335,6 +1337,7 @@ export const createTableBlockSchema = (options) => { action: 'list', params: { pageSize, + crypticFilter, }, rowKey, showIndex: true, diff --git a/packages/plugins/audit-logs/src/client/AuditLogsBlockInitializer.tsx b/packages/plugins/audit-logs/src/client/AuditLogsBlockInitializer.tsx index a8ccbd350..4c09cf0bc 100644 --- a/packages/plugins/audit-logs/src/client/AuditLogsBlockInitializer.tsx +++ b/packages/plugins/audit-logs/src/client/AuditLogsBlockInitializer.tsx @@ -1,13 +1,30 @@ import { TableOutlined } from '@ant-design/icons'; -import { ISchema } from '@formily/react'; -import { createTableBlockSchema, SchemaInitializer } from '@nocobase/client'; +import { ISchema, useFieldSchema } from '@formily/react'; +import { + createTableBlockSchema, + generateFilterParams, + SchemaInitializer, + useCollection, + useRecord, +} from '@nocobase/client'; import React from 'react'; import { useTranslation } from 'react-i18next'; +import { getResourceOfBlockDesigner } from './utils'; export const AuditLogsBlockInitializer = (props) => { + const { filterTargetKey } = useCollection(); + const { insert } = props; const { t } = useTranslation(); + const record = useRecord(); + const fieldSchema = useFieldSchema(); + const crypticFilterFromAuditLogs = generateFilterParams( + record, + getResourceOfBlockDesigner(fieldSchema), + filterTargetKey, + {}, + ); const schema = createTableBlockSchema({ collection: 'auditLogs', rowKey: 'id', @@ -16,6 +33,8 @@ export const AuditLogsBlockInitializer = (props) => { tableActionColumnInitializers: 'AuditLogsTableActionColumnInitializers', tableBlockProvider: 'AuditLogsBlockProvider', disableTemplate: true, + // 当前filter 不需要在 "设置数据范围" 表单里初始化,只需要在查询的时候合并到查询条件 filter中 + crypticFilter: crypticFilterFromAuditLogs, }); return ( diff --git a/packages/plugins/audit-logs/src/client/deplicated/AuditLogs.tsx b/packages/plugins/audit-logs/src/client/deplicated/AuditLogs.tsx index 8d8781d60..67d043b86 100644 --- a/packages/plugins/audit-logs/src/client/deplicated/AuditLogs.tsx +++ b/packages/plugins/audit-logs/src/client/deplicated/AuditLogs.tsx @@ -4,6 +4,7 @@ import { CollectionManagerContext, CollectionManagerProvider, FormProvider, + generateFilterParams, SchemaComponent, TableBlockProvider, useCollection, @@ -458,55 +459,35 @@ export const AuditLogs: any = () => { ); }; -AuditLogs.Decorator = observer( - (props: any) => { - const parent = useCollection(); - const record = useRecord(); - const { interfaces } = useContext(CollectionManagerContext); - let filter = props?.params?.filter; - if (parent.name) { - const filterByTk = record?.[parent.filterTargetKey || 'id']; - if (filter) { - filter = { - $and: [ - filter, - { - collectionName: parent.name, - recordId: `${filterByTk}`, - }, - ], - }; - } else { - filter = { - collectionName: parent.name, - recordId: `${filterByTk}`, - }; - } - } - const defaults = { - collection: 'auditLogs', - resource: 'auditLogs', - action: 'list', - params: { - pageSize: 20, - appends: ['collection', 'user'], - ...props.params, - filter, - sort: '-createdAt', - }, - rowKey: 'id', - showIndex: true, - dragSort: false, - }; - return ( - - - {props.children} - - - ); - }, - { displayName: 'AuditLogs.Decorator' }, -); +AuditLogs.Decorator = observer((props: any) => { + const parent = useCollection(); + const record = useRecord(); + const { interfaces } = useContext(CollectionManagerContext); + const filter = generateFilterParams(record, parent.name, parent.filterTargetKey, props?.params?.filter) || {}; + const defaults = { + collection: 'auditLogs', + resource: 'auditLogs', + action: 'list', + params: { + pageSize: 20, + appends: ['collection', 'user'], + ...props.params, + filter, + sort: '-createdAt', + }, + rowKey: 'id', + showIndex: true, + dragSort: false, + }; + return ( + + + {props.children} + + + ); +}, { + displayName: 'AuditLogs.Decorator' +}); AuditLogs.Designer = AuditLogsDesigner; diff --git a/packages/plugins/audit-logs/src/client/utils.ts b/packages/plugins/audit-logs/src/client/utils.ts index 1ecc63443..3a7cc2177 100644 --- a/packages/plugins/audit-logs/src/client/utils.ts +++ b/packages/plugins/audit-logs/src/client/utils.ts @@ -1,4 +1,6 @@ import { uid } from '@formily/shared'; +import { Schema } from '@formily/react'; +import cloneDeep from 'lodash/cloneDeep'; export const createSchema = () => { const filterSchema = { @@ -301,3 +303,18 @@ export const createSchema = () => { }; return schema; }; + +/** + * 获取 TableBlockDesigner 或 DetailsDesigner 的 resource + */ +export function getResourceOfBlockDesigner(schema: Schema) { + let tempSchema = cloneDeep(schema); + while ( + tempSchema && + ((tempSchema?.['x-designer'] !== 'TableBlockDesigner' && tempSchema?.['x-designer'] !== 'DetailsDesigner') || + tempSchema?.['x-decorator-props']?.['resource'] === 'auditLogs') + ) { + tempSchema = tempSchema.parent; + } + return tempSchema?.['x-decorator-props']?.['resource']; +}