From 058e7e320d7a293f4ce9b8ed09d1c1c593355171 Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Wed, 25 Sep 2024 22:27:24 +0800 Subject: [PATCH] feat(custom-request): support adding custom request in more places (#5320) * feat(custom-request): support adding custom request buttons in more places * fix: fix known issues --- .../grid-card/GridCardActionInitializers.tsx | 9 ++++++++- .../blocks/data-blocks/list/ListActionInitializers.tsx | 9 ++++++++- .../data-blocks/table/TableActionInitializers.tsx | 8 ++++++++ .../client/hooks/useCustomRequestVariableOptions.ts | 8 +++++--- .../src/client/schemaSettings.ts | 8 ++++---- .../initializers/CalendarActionInitializers.tsx | 8 ++++++++ .../src/client/GanttActionInitializers.tsx | 8 ++++++++ .../src/client/KanbanActionInitializers.tsx | 10 +++++++++- .../src/client/block/MapActionInitializers.tsx | 8 ++++++++ 9 files changed, 66 insertions(+), 10 deletions(-) diff --git a/packages/core/client/src/modules/blocks/data-blocks/grid-card/GridCardActionInitializers.tsx b/packages/core/client/src/modules/blocks/data-blocks/grid-card/GridCardActionInitializers.tsx index d47dc645fd..614bb6d0db 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/grid-card/GridCardActionInitializers.tsx +++ b/packages/core/client/src/modules/blocks/data-blocks/grid-card/GridCardActionInitializers.tsx @@ -8,7 +8,6 @@ */ import { CompatibleSchemaInitializer } from '../../../../application/schema-initializer/CompatibleSchemaInitializer'; -import { useCollection } from '../../../../data-source'; import { useActionAvailable } from '../../useActionAvailable'; const commonOptions = { title: "{{t('Configure actions')}}", @@ -73,6 +72,14 @@ const commonOptions = { }, }, }, + { + name: 'customRequest', + title: '{{t("Custom request")}}', + Component: 'CustomRequestInitializer', + schema: { + 'x-action': 'customize:table:request:global', + }, + }, ], }; diff --git a/packages/core/client/src/modules/blocks/data-blocks/list/ListActionInitializers.tsx b/packages/core/client/src/modules/blocks/data-blocks/list/ListActionInitializers.tsx index cbc1ac8418..97421222f7 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/list/ListActionInitializers.tsx +++ b/packages/core/client/src/modules/blocks/data-blocks/list/ListActionInitializers.tsx @@ -8,7 +8,6 @@ */ import { CompatibleSchemaInitializer } from '../../../../application/schema-initializer/CompatibleSchemaInitializer'; -import { useCollection } from '../../../../data-source'; import { useActionAvailable } from '../../useActionAvailable'; const commonOptions = { @@ -74,6 +73,14 @@ const commonOptions = { }, }, }, + { + name: 'customRequest', + title: '{{t("Custom request")}}', + Component: 'CustomRequestInitializer', + schema: { + 'x-action': 'customize:table:request:global', + }, + }, ], }; diff --git a/packages/core/client/src/modules/blocks/data-blocks/table/TableActionInitializers.tsx b/packages/core/client/src/modules/blocks/data-blocks/table/TableActionInitializers.tsx index b8e8027ab9..c31d340ede 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/table/TableActionInitializers.tsx +++ b/packages/core/client/src/modules/blocks/data-blocks/table/TableActionInitializers.tsx @@ -90,6 +90,14 @@ const commonOptions = { return collection.tree && treeTable; }, }, + { + name: 'customRequest', + title: '{{t("Custom request")}}', + Component: 'CustomRequestInitializer', + schema: { + 'x-action': 'customize:table:request:global', + }, + }, ], }; diff --git a/packages/plugins/@nocobase/plugin-action-custom-request/src/client/hooks/useCustomRequestVariableOptions.ts b/packages/plugins/@nocobase/plugin-action-custom-request/src/client/hooks/useCustomRequestVariableOptions.ts index 5b83940d8e..5c7b9d1d7d 100644 --- a/packages/plugins/@nocobase/plugin-action-custom-request/src/client/hooks/useCustomRequestVariableOptions.ts +++ b/packages/plugins/@nocobase/plugin-action-custom-request/src/client/hooks/useCustomRequestVariableOptions.ts @@ -11,6 +11,7 @@ import { DEFAULT_DATA_SOURCE_KEY, useCollection_deprecated, useCollectionFilterOptions, + useCollectionRecordData, useCompile, } from '@nocobase/client'; import { useMemo } from 'react'; @@ -22,13 +23,14 @@ export const useCustomRequestVariableOptions = () => { const fieldsOptions = useCollectionFilterOptions(collection); const userFieldOptions = useCollectionFilterOptions('users', DEFAULT_DATA_SOURCE_KEY); const compile = useCompile(); + const recordData = useCollectionRecordData(); const [fields, userFields] = useMemo(() => { return [compile(fieldsOptions), compile(userFieldOptions)]; }, [fieldsOptions, userFieldOptions]); return useMemo(() => { return [ - { + recordData && { name: 'currentRecord', title: t('Current record', { ns: 'client' }), children: [...fields], @@ -48,6 +50,6 @@ export const useCustomRequestVariableOptions = () => { title: 'API token', children: null, }, - ]; - }, [fields, userFields]); + ].filter(Boolean); + }, [recordData, t, fields, userFields]); }; diff --git a/packages/plugins/@nocobase/plugin-action-custom-request/src/client/schemaSettings.ts b/packages/plugins/@nocobase/plugin-action-custom-request/src/client/schemaSettings.ts index baf19de2c2..340c4d3128 100644 --- a/packages/plugins/@nocobase/plugin-action-custom-request/src/client/schemaSettings.ts +++ b/packages/plugins/@nocobase/plugin-action-custom-request/src/client/schemaSettings.ts @@ -7,20 +7,20 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ +import { useFieldSchema } from '@formily/react'; import { AfterSuccess, ButtonEditor, + RefreshDataBlockRequest, RemoveButton, SchemaSettings, SchemaSettingsLinkageRules, SecondConFirm, useCollection, - useSchemaToolbar, - RefreshDataBlockRequest, useCollectionRecord, + useSchemaToolbar, } from '@nocobase/client'; import { CustomRequestACL, CustomRequestSettingsItem } from './components/CustomRequestActionDesigner'; -import { useFieldSchema } from '@formily/react'; export const customizeCustomRequestActionSettings = new SchemaSettings({ name: 'actionSettings:customRequest', @@ -48,7 +48,7 @@ export const customizeCustomRequestActionSettings = new SchemaSettings({ }, useVisible() { const record = useCollectionRecord(); - return record && !record?.isNew; + return record && record.data && !record?.isNew; }, }, { diff --git a/packages/plugins/@nocobase/plugin-calendar/src/client/schema-initializer/initializers/CalendarActionInitializers.tsx b/packages/plugins/@nocobase/plugin-calendar/src/client/schema-initializer/initializers/CalendarActionInitializers.tsx index 78dca46f84..bc6769aabd 100644 --- a/packages/plugins/@nocobase/plugin-calendar/src/client/schema-initializer/initializers/CalendarActionInitializers.tsx +++ b/packages/plugins/@nocobase/plugin-calendar/src/client/schema-initializer/initializers/CalendarActionInitializers.tsx @@ -92,6 +92,14 @@ const commonOptions = { }, useVisible: () => useActionAvailable('create'), }, + { + name: 'customRequest', + title: '{{t("Custom request")}}', + Component: 'CustomRequestInitializer', + schema: { + 'x-action': 'customize:table:request:global', + }, + }, ], }; diff --git a/packages/plugins/@nocobase/plugin-gantt/src/client/GanttActionInitializers.tsx b/packages/plugins/@nocobase/plugin-gantt/src/client/GanttActionInitializers.tsx index 594c53a0fc..2a231a8174 100644 --- a/packages/plugins/@nocobase/plugin-gantt/src/client/GanttActionInitializers.tsx +++ b/packages/plugins/@nocobase/plugin-gantt/src/client/GanttActionInitializers.tsx @@ -75,6 +75,14 @@ const commonOptions = { return collection.tree && treeTable; }, }, + { + name: 'customRequest', + title: '{{t("Custom request")}}', + Component: 'CustomRequestInitializer', + schema: { + 'x-action': 'customize:table:request:global', + }, + }, ], }; diff --git a/packages/plugins/@nocobase/plugin-kanban/src/client/KanbanActionInitializers.tsx b/packages/plugins/@nocobase/plugin-kanban/src/client/KanbanActionInitializers.tsx index 1a86f8475e..5260dd34b9 100644 --- a/packages/plugins/@nocobase/plugin-kanban/src/client/KanbanActionInitializers.tsx +++ b/packages/plugins/@nocobase/plugin-kanban/src/client/KanbanActionInitializers.tsx @@ -7,7 +7,7 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ -import { CompatibleSchemaInitializer, useCollection, useActionAvailable } from '@nocobase/client'; +import { CompatibleSchemaInitializer, useActionAvailable } from '@nocobase/client'; const commonOptions = { title: "{{t('Configure actions')}}", @@ -37,6 +37,14 @@ const commonOptions = { }, useVisible: () => useActionAvailable('create'), }, + { + name: 'customRequest', + title: '{{t("Custom request")}}', + Component: 'CustomRequestInitializer', + schema: { + 'x-action': 'customize:table:request:global', + }, + }, ], }; diff --git a/packages/plugins/@nocobase/plugin-map/src/client/block/MapActionInitializers.tsx b/packages/plugins/@nocobase/plugin-map/src/client/block/MapActionInitializers.tsx index 7956afbd5e..b8ecdcf315 100644 --- a/packages/plugins/@nocobase/plugin-map/src/client/block/MapActionInitializers.tsx +++ b/packages/plugins/@nocobase/plugin-map/src/client/block/MapActionInitializers.tsx @@ -45,6 +45,14 @@ const commonOptions = { 'x-align': 'right', }, }, + { + name: 'customRequest', + title: '{{t("Custom request")}}', + Component: 'CustomRequestInitializer', + schema: { + 'x-action': 'customize:table:request:global', + }, + }, ], };