fix: the edit drawer's audit logs block can only get records by this … (#1917)
* chore: rename resource function and add comment * chore: remove AuditLogsDesigner when create audit logs schema --------- Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
		
							parent
							
								
									e6a2a292b3
								
							
						
					
					
						commit
						42ad77da68
					
				@ -2,12 +2,13 @@ import { ArrayField, createForm } from '@formily/core';
 | 
				
			|||||||
import { FormContext, Schema, useField, useFieldSchema } from '@formily/react';
 | 
					import { FormContext, Schema, useField, useFieldSchema } from '@formily/react';
 | 
				
			||||||
import uniq from 'lodash/uniq';
 | 
					import uniq from 'lodash/uniq';
 | 
				
			||||||
import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
 | 
					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 { useFilterBlock } from '../filter-provider/FilterProvider';
 | 
				
			||||||
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 { mergeFilter } from './SharedFilterProvider';
 | 
				
			||||||
import { findFilterTargets } from './hooks';
 | 
					import { findFilterTargets } from './hooks';
 | 
				
			||||||
 | 
					import { useRecord } from '../record-provider';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const TableBlockContext = createContext<any>({});
 | 
					export const TableBlockContext = createContext<any>({});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -99,11 +100,18 @@ const useAssociationNames = (collection) => {
 | 
				
			|||||||
export const TableBlockProvider = (props) => {
 | 
					export const TableBlockProvider = (props) => {
 | 
				
			||||||
  const resourceName = props.resource;
 | 
					  const resourceName = props.resource;
 | 
				
			||||||
  const params = { ...props.params };
 | 
					  const params = { ...props.params };
 | 
				
			||||||
 | 
					  const record = useRecord();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const appends = useAssociationNames(props.collection);
 | 
					  const appends = useAssociationNames(props.collection);
 | 
				
			||||||
  const fieldSchema = useFieldSchema();
 | 
					  const fieldSchema = useFieldSchema();
 | 
				
			||||||
  const { getCollection, getCollectionField } = useCollectionManager();
 | 
					  const { getCollection, getCollectionField } = useCollectionManager();
 | 
				
			||||||
 | 
					  const parent = useCollection();
 | 
				
			||||||
 | 
					  const filter = generateFilterParams(record, parent.name, parent.filterTargetKey, {});
 | 
				
			||||||
  const collection = getCollection(props.collection);
 | 
					  const collection = getCollection(props.collection);
 | 
				
			||||||
  const { treeTable } = fieldSchema?.['x-decorator-props'] || {};
 | 
					  const { treeTable } = fieldSchema?.['x-decorator-props'] || {};
 | 
				
			||||||
 | 
					  if (filter) {
 | 
				
			||||||
 | 
					    params.filter = filter;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  if (props.dragSort) {
 | 
					  if (props.dragSort) {
 | 
				
			||||||
    params['sort'] = ['sort'];
 | 
					    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;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -31,7 +31,7 @@ export const useValues = () => {
 | 
				
			|||||||
  };
 | 
					  };
 | 
				
			||||||
  const value2data = () => {
 | 
					  const value2data = () => {
 | 
				
			||||||
    field.data = field.data || {};
 | 
					    field.data = field.data || {};
 | 
				
			||||||
    const values = flat(field.value);
 | 
					    const values = flat(field.value || {});
 | 
				
			||||||
    const path = Object.keys(values).shift() || '';
 | 
					    const path = Object.keys(values).shift() || '';
 | 
				
			||||||
    if (!path || !options) {
 | 
					    if (!path || !options) {
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
 | 
				
			|||||||
@ -13,6 +13,8 @@ import { useCompile, useDesignable } from '../../hooks';
 | 
				
			|||||||
import { removeNullCondition } from '../filter';
 | 
					import { removeNullCondition } from '../filter';
 | 
				
			||||||
import { FixedBlockDesignerItem } from '../page';
 | 
					import { FixedBlockDesignerItem } from '../page';
 | 
				
			||||||
import { FilterDynamicComponent } from './FilterDynamicComponent';
 | 
					import { FilterDynamicComponent } from './FilterDynamicComponent';
 | 
				
			||||||
 | 
					import cloneDeep from 'lodash/cloneDeep';
 | 
				
			||||||
 | 
					import isEmpty from 'lodash/isEmpty';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const TableBlockDesigner = () => {
 | 
					export const TableBlockDesigner = () => {
 | 
				
			||||||
  const { name, title, sortable } = useCollection();
 | 
					  const { name, title, sortable } = useCollection();
 | 
				
			||||||
@ -25,7 +27,9 @@ export const TableBlockDesigner = () => {
 | 
				
			|||||||
  const { t } = useTranslation();
 | 
					  const { t } = useTranslation();
 | 
				
			||||||
  const { dn } = useDesignable();
 | 
					  const { dn } = useDesignable();
 | 
				
			||||||
  const compile = useCompile();
 | 
					  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 defaultSort = fieldSchema?.['x-decorator-props']?.params?.sort || [];
 | 
				
			||||||
  const defaultResource = fieldSchema?.['x-decorator-props']?.resource;
 | 
					  const defaultResource = fieldSchema?.['x-decorator-props']?.resource;
 | 
				
			||||||
  const supportTemplate = !fieldSchema?.['x-decorator-props']?.disableTemplate;
 | 
					  const supportTemplate = !fieldSchema?.['x-decorator-props']?.disableTemplate;
 | 
				
			||||||
@ -67,12 +71,16 @@ export const TableBlockDesigner = () => {
 | 
				
			|||||||
    ({ filter }) => {
 | 
					    ({ filter }) => {
 | 
				
			||||||
      filter = removeNullCondition(filter);
 | 
					      filter = removeNullCondition(filter);
 | 
				
			||||||
      const params = field.decoratorProps.params || {};
 | 
					      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;
 | 
					      params.filter = filter;
 | 
				
			||||||
      field.decoratorProps.params = params;
 | 
					      field.decoratorProps.params = params;
 | 
				
			||||||
      fieldSchema['x-decorator-props']['params'] = params;
 | 
					      fieldSchema['x-decorator-props']['params'] = params;
 | 
				
			||||||
      const filters = service.params?.[1]?.filters || {};
 | 
					      const filters = service.params?.[1]?.filters || {};
 | 
				
			||||||
      service.run(
 | 
					      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 },
 | 
					        { filters },
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
      dn.emit('patch', {
 | 
					      dn.emit('patch', {
 | 
				
			||||||
@ -82,7 +90,7 @@ export const TableBlockDesigner = () => {
 | 
				
			|||||||
        },
 | 
					        },
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    [field],
 | 
					    [field, crypticFilter],
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
 | 
				
			|||||||
@ -1323,6 +1323,8 @@ export const createTableBlockSchema = (options) => {
 | 
				
			|||||||
    TableBlockDesigner,
 | 
					    TableBlockDesigner,
 | 
				
			||||||
    blockType,
 | 
					    blockType,
 | 
				
			||||||
    pageSize = 20,
 | 
					    pageSize = 20,
 | 
				
			||||||
 | 
					    // 当前filter 不需要在 "设置数据范围" 表单里初始化,只需要在查询的时候合并到查询条件 filter中
 | 
				
			||||||
 | 
					    crypticFilter = {},
 | 
				
			||||||
    ...others
 | 
					    ...others
 | 
				
			||||||
  } = options;
 | 
					  } = options;
 | 
				
			||||||
  const schema: ISchema = {
 | 
					  const schema: ISchema = {
 | 
				
			||||||
@ -1335,6 +1337,7 @@ export const createTableBlockSchema = (options) => {
 | 
				
			|||||||
      action: 'list',
 | 
					      action: 'list',
 | 
				
			||||||
      params: {
 | 
					      params: {
 | 
				
			||||||
        pageSize,
 | 
					        pageSize,
 | 
				
			||||||
 | 
					        crypticFilter,
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      rowKey,
 | 
					      rowKey,
 | 
				
			||||||
      showIndex: true,
 | 
					      showIndex: true,
 | 
				
			||||||
 | 
				
			|||||||
@ -1,13 +1,30 @@
 | 
				
			|||||||
import { TableOutlined } from '@ant-design/icons';
 | 
					import { TableOutlined } from '@ant-design/icons';
 | 
				
			||||||
import { ISchema } from '@formily/react';
 | 
					import { ISchema, useFieldSchema } from '@formily/react';
 | 
				
			||||||
import { createTableBlockSchema, SchemaInitializer } from '@nocobase/client';
 | 
					import {
 | 
				
			||||||
 | 
					  createTableBlockSchema,
 | 
				
			||||||
 | 
					  generateFilterParams,
 | 
				
			||||||
 | 
					  SchemaInitializer,
 | 
				
			||||||
 | 
					  useCollection,
 | 
				
			||||||
 | 
					  useRecord,
 | 
				
			||||||
 | 
					} from '@nocobase/client';
 | 
				
			||||||
import React from 'react';
 | 
					import React from 'react';
 | 
				
			||||||
import { useTranslation } from 'react-i18next';
 | 
					import { useTranslation } from 'react-i18next';
 | 
				
			||||||
 | 
					import { getResourceOfBlockDesigner } from './utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const AuditLogsBlockInitializer = (props) => {
 | 
					export const AuditLogsBlockInitializer = (props) => {
 | 
				
			||||||
 | 
					  const { filterTargetKey } = useCollection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const { insert } = props;
 | 
					  const { insert } = props;
 | 
				
			||||||
  const { t } = useTranslation();
 | 
					  const { t } = useTranslation();
 | 
				
			||||||
 | 
					  const record = useRecord();
 | 
				
			||||||
 | 
					  const fieldSchema = useFieldSchema();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const crypticFilterFromAuditLogs = generateFilterParams(
 | 
				
			||||||
 | 
					    record,
 | 
				
			||||||
 | 
					    getResourceOfBlockDesigner(fieldSchema),
 | 
				
			||||||
 | 
					    filterTargetKey,
 | 
				
			||||||
 | 
					    {},
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
  const schema = createTableBlockSchema({
 | 
					  const schema = createTableBlockSchema({
 | 
				
			||||||
    collection: 'auditLogs',
 | 
					    collection: 'auditLogs',
 | 
				
			||||||
    rowKey: 'id',
 | 
					    rowKey: 'id',
 | 
				
			||||||
@ -16,6 +33,8 @@ export const AuditLogsBlockInitializer = (props) => {
 | 
				
			|||||||
    tableActionColumnInitializers: 'AuditLogsTableActionColumnInitializers',
 | 
					    tableActionColumnInitializers: 'AuditLogsTableActionColumnInitializers',
 | 
				
			||||||
    tableBlockProvider: 'AuditLogsBlockProvider',
 | 
					    tableBlockProvider: 'AuditLogsBlockProvider',
 | 
				
			||||||
    disableTemplate: true,
 | 
					    disableTemplate: true,
 | 
				
			||||||
 | 
					    // 当前filter 不需要在 "设置数据范围" 表单里初始化,只需要在查询的时候合并到查询条件 filter中
 | 
				
			||||||
 | 
					    crypticFilter: crypticFilterFromAuditLogs,
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,7 @@ import {
 | 
				
			|||||||
  CollectionManagerContext,
 | 
					  CollectionManagerContext,
 | 
				
			||||||
  CollectionManagerProvider,
 | 
					  CollectionManagerProvider,
 | 
				
			||||||
  FormProvider,
 | 
					  FormProvider,
 | 
				
			||||||
 | 
					  generateFilterParams,
 | 
				
			||||||
  SchemaComponent,
 | 
					  SchemaComponent,
 | 
				
			||||||
  TableBlockProvider,
 | 
					  TableBlockProvider,
 | 
				
			||||||
  useCollection,
 | 
					  useCollection,
 | 
				
			||||||
@ -458,55 +459,35 @@ export const AuditLogs: any = () => {
 | 
				
			|||||||
  );
 | 
					  );
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
AuditLogs.Decorator = observer(
 | 
					AuditLogs.Decorator = observer((props: any) => {
 | 
				
			||||||
  (props: any) => {
 | 
					  const parent = useCollection();
 | 
				
			||||||
    const parent = useCollection();
 | 
					  const record = useRecord();
 | 
				
			||||||
    const record = useRecord();
 | 
					  const { interfaces } = useContext(CollectionManagerContext);
 | 
				
			||||||
    const { interfaces } = useContext(CollectionManagerContext);
 | 
					  const filter = generateFilterParams(record, parent.name, parent.filterTargetKey, props?.params?.filter) || {};
 | 
				
			||||||
    let filter = props?.params?.filter;
 | 
					  const defaults = {
 | 
				
			||||||
    if (parent.name) {
 | 
					    collection: 'auditLogs',
 | 
				
			||||||
      const filterByTk = record?.[parent.filterTargetKey || 'id'];
 | 
					    resource: 'auditLogs',
 | 
				
			||||||
      if (filter) {
 | 
					    action: 'list',
 | 
				
			||||||
        filter = {
 | 
					    params: {
 | 
				
			||||||
          $and: [
 | 
					      pageSize: 20,
 | 
				
			||||||
            filter,
 | 
					      appends: ['collection', 'user'],
 | 
				
			||||||
            {
 | 
					      ...props.params,
 | 
				
			||||||
              collectionName: parent.name,
 | 
					      filter,
 | 
				
			||||||
              recordId: `${filterByTk}`,
 | 
					      sort: '-createdAt',
 | 
				
			||||||
            },
 | 
					    },
 | 
				
			||||||
          ],
 | 
					    rowKey: 'id',
 | 
				
			||||||
        };
 | 
					    showIndex: true,
 | 
				
			||||||
      } else {
 | 
					    dragSort: false,
 | 
				
			||||||
        filter = {
 | 
					  };
 | 
				
			||||||
          collectionName: parent.name,
 | 
					  return (
 | 
				
			||||||
          recordId: `${filterByTk}`,
 | 
					    <IsAssociationBlock.Provider value={!!parent.name}>
 | 
				
			||||||
        };
 | 
					      <CollectionManagerProvider collections={[collection]} interfaces={interfaces}>
 | 
				
			||||||
      }
 | 
					        <TableBlockProvider {...defaults}>{props.children}</TableBlockProvider>
 | 
				
			||||||
    }
 | 
					      </CollectionManagerProvider>
 | 
				
			||||||
    const defaults = {
 | 
					    </IsAssociationBlock.Provider>
 | 
				
			||||||
      collection: 'auditLogs',
 | 
					  );
 | 
				
			||||||
      resource: 'auditLogs',
 | 
					}, {
 | 
				
			||||||
      action: 'list',
 | 
					  displayName: 'AuditLogs.Decorator'
 | 
				
			||||||
      params: {
 | 
					});
 | 
				
			||||||
        pageSize: 20,
 | 
					 | 
				
			||||||
        appends: ['collection', 'user'],
 | 
					 | 
				
			||||||
        ...props.params,
 | 
					 | 
				
			||||||
        filter,
 | 
					 | 
				
			||||||
        sort: '-createdAt',
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      rowKey: 'id',
 | 
					 | 
				
			||||||
      showIndex: true,
 | 
					 | 
				
			||||||
      dragSort: false,
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    return (
 | 
					 | 
				
			||||||
      <IsAssociationBlock.Provider value={!!parent.name}>
 | 
					 | 
				
			||||||
        <CollectionManagerProvider collections={[collection]} interfaces={interfaces}>
 | 
					 | 
				
			||||||
          <TableBlockProvider {...defaults}>{props.children}</TableBlockProvider>
 | 
					 | 
				
			||||||
        </CollectionManagerProvider>
 | 
					 | 
				
			||||||
      </IsAssociationBlock.Provider>
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  { displayName: 'AuditLogs.Decorator' },
 | 
					 | 
				
			||||||
);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
AuditLogs.Designer = AuditLogsDesigner;
 | 
					AuditLogs.Designer = AuditLogsDesigner;
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,6 @@
 | 
				
			|||||||
import { uid } from '@formily/shared';
 | 
					import { uid } from '@formily/shared';
 | 
				
			||||||
 | 
					import { Schema } from '@formily/react';
 | 
				
			||||||
 | 
					import cloneDeep from 'lodash/cloneDeep';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const createSchema = () => {
 | 
					export const createSchema = () => {
 | 
				
			||||||
  const filterSchema = {
 | 
					  const filterSchema = {
 | 
				
			||||||
@ -301,3 +303,18 @@ export const createSchema = () => {
 | 
				
			|||||||
  };
 | 
					  };
 | 
				
			||||||
  return schema;
 | 
					  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'];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user