From 7f35abe348e5f3a1806a213116ed0a7060b10585 Mon Sep 17 00:00:00 2001 From: mytharcher Date: Sun, 13 Apr 2025 11:54:57 +0800 Subject: [PATCH] refactor(plugin-workflow): change task center popup logic --- .../antd/action/ActionBar.tsx | 2 +- .../src/schema-component/antd/list/List.tsx | 2 +- .../src/client/WorkflowTodo.tsx | 3 + .../src/server/Plugin.ts | 26 ++- .../src/client/WorkflowTasks.tsx | 189 +++++++++++------- .../plugin-workflow/src/client/index.tsx | 2 +- .../plugin-workflow/src/server/Plugin.ts | 16 +- 7 files changed, 144 insertions(+), 96 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/action/ActionBar.tsx b/packages/core/client/src/schema-component/antd/action/ActionBar.tsx index 09adfde89d..a1dfc5ecea 100644 --- a/packages/core/client/src/schema-component/antd/action/ActionBar.tsx +++ b/packages/core/client/src/schema-component/antd/action/ActionBar.tsx @@ -72,7 +72,7 @@ const InternalActionBar: FC = (props: any) => {
diff --git a/packages/core/client/src/schema-component/antd/list/List.tsx b/packages/core/client/src/schema-component/antd/list/List.tsx index d6197f27bd..4f6db0faea 100644 --- a/packages/core/client/src/schema-component/antd/list/List.tsx +++ b/packages/core/client/src/schema-component/antd/list/List.tsx @@ -158,7 +158,7 @@ const InternalList = withSkeletonComponent( > {field.value?.length diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/WorkflowTodo.tsx b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/WorkflowTodo.tsx index 591a16e5bf..491a82d89c 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/WorkflowTodo.tsx +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/WorkflowTodo.tsx @@ -50,6 +50,7 @@ import WorkflowPlugin, { JOB_STATUS, WorkflowTitle, TASK_STATUS, + usePopupRecordContext, } from '@nocobase/plugin-workflow/client'; import { NAMESPACE, useLang } from '../locale'; @@ -619,11 +620,13 @@ function TaskItem() { const token = useAntdToken(); const record = useCollectionRecordData(); const navigate = useNavigate(); + const { setRecord } = usePopupRecordContext(); const onOpen = useCallback( (e: React.MouseEvent) => { const targetElement = e.target as Element; // 将事件目标转换为Element类型 const currentTargetElement = e.currentTarget as Element; if (currentTargetElement.contains(targetElement)) { + setRecord(record); navigate(`./${record.id}`); } e.stopPropagation(); diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/Plugin.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/Plugin.ts index 7338d0958e..a63b5aba87 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/Plugin.ts +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/Plugin.ts @@ -16,13 +16,14 @@ import * as jobActions from './actions'; import ManualInstruction from './ManualInstruction'; import { MANUAL_TASK_TYPE } from '../common/constants'; +import { Model } from '@nocobase/database'; -interface WorkflowManualTaskModel { - id: number; - userId: number; - workflowId: number; - executionId: number; - status: number; +class WorkflowManualTaskModel extends Model { + declare id: number; + declare userId: number; + declare workflowId: number; + declare executionId: number; + declare status: number; } export default class extends Plugin { @@ -55,7 +56,13 @@ export default class extends Plugin { const workflowPlugin = this.app.pm.get(WorkflowPlugin) as WorkflowPlugin; workflowPlugin.registerInstruction('manual', ManualInstruction); - this.db.on('workflowManualTasks.afterSave', async (task: WorkflowManualTaskModel, options) => { + this.db.on('workflowManualTasks.afterSave', async (task: WorkflowManualTaskModel, { transaction }) => { + // const allCount = await (task.constructor as typeof WorkflowManualTaskModel).count({ + // where: { + // userId: task.userId, + // }, + // transaction, + // }); await workflowPlugin.toggleTaskStatus( { type: MANUAL_TASK_TYPE, @@ -63,8 +70,9 @@ export default class extends Plugin { userId: task.userId, workflowId: task.workflowId, }, - Boolean(task.status), - options, + task.status === JOB_STATUS.PENDING, + // allCount, + { transaction }, ); }); } diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/WorkflowTasks.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/WorkflowTasks.tsx index 1fc30c72f7..17d435fa26 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/WorkflowTasks.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/WorkflowTasks.tsx @@ -11,7 +11,7 @@ import { PageHeader } from '@ant-design/pro-layout'; import { Badge, Button, Layout, Menu, Tabs, Tooltip } from 'antd'; import classnames from 'classnames'; import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; -import { Link, Outlet, useNavigate, useParams } from 'react-router-dom'; +import { Link, useNavigate, useParams } from 'react-router-dom'; import { ActionContextProvider, @@ -21,6 +21,7 @@ import { SchemaComponent, SchemaComponentContext, SchemaComponentOptions, + useAPIClient, useApp, useCompile, useDocumentTitle, @@ -163,7 +164,11 @@ function useCurrentTaskType() { function PopupContext(props: any) { const { popupId } = useParams(); + const { record } = usePopupRecordContext(); const navigate = useNavigate(); + if (!popupId) { + return null; + } return ( - {props.children} + {props.children} ); } +const PopupRecordContext = createContext({ record: null, setRecord: (record) => {} }); +export function usePopupRecordContext() { + return useContext(PopupRecordContext); +} + export function WorkflowTasks() { const compile = useCompile(); const { setTitle } = useDocumentTitle(); const navigate = useNavigate(); + const apiClient = useAPIClient(); const { taskType, status = TASK_STATUS.PENDING, popupId } = useParams(); const { token } = useToken(); + const [currentRecord, setCurrentRecord] = useState(null); const items = useTaskTypeItems(); @@ -202,6 +214,24 @@ export function WorkflowTasks() { } }, [items, navigate, status, taskType]); + useEffect(() => { + if (popupId && !currentRecord) { + apiClient + .resource(collection) + .get({ + filterByTk: popupId, + }) + .then((res) => { + if (res.data?.data) { + setCurrentRecord(res.data.data); + } + }) + .catch((err) => { + console.error(err); + }); + } + }, [popupId, collection, currentRecord, apiClient]); + const typeKey = taskType ?? items[0].key; return ( @@ -227,88 +257,95 @@ export function WorkflowTasks() { } `} > - - + + .itemCss:not(:last-child) { - border-bottom: none; - } - `, - locale: { - emptyText: `{{ t("No data yet", { ns: "${NAMESPACE}" }) }}`, - }, + properties: { + header: { + type: 'void', + 'x-component': 'PageHeader', + 'x-component-props': { + className: classnames('pageHeaderCss'), + style: { + background: token.colorBgContainer, + padding: '12px 24px 0 24px', }, - properties: { - item: { - type: 'object', - 'x-decorator': 'List.Item', - 'x-component': Item, - 'x-read-pretty': true, + title, + }, + properties: { + tabs: { + type: 'void', + 'x-component': 'StatusTabs', + }, + }, + }, + content: { + type: 'void', + 'x-component': 'Layout.Content', + 'x-component-props': { + className: contentClass, + style: { + padding: `${token.paddingPageVertical}px ${token.paddingPageHorizontal}px`, + }, + }, + properties: { + list: { + type: 'array', + 'x-component': 'List', + 'x-component-props': { + className: css` + > .itemCss:not(:last-child) { + border-bottom: none; + } + `, + locale: { + emptyText: `{{ t("No data yet", { ns: "${NAMESPACE}" }) }}`, + }, + }, + properties: { + item: { + type: 'object', + 'x-decorator': 'List.Item', + 'x-component': Item, + 'x-read-pretty': true, + }, }, }, }, }, + popup: { + type: 'void', + 'x-decorator': PopupContext, + 'x-component': Detail, + }, }, - popup: { - type: 'void', - 'x-decorator': PopupContext, - 'x-component': Detail, - }, - }, - }} - /> - + }} + /> + + ); diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/index.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/index.tsx index b0ac1e1918..893f493509 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/index.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/index.tsx @@ -188,4 +188,4 @@ export { default as useStyles } from './style'; export { Trigger, useTrigger } from './triggers'; export * from './utils'; export * from './variable'; -export { TASK_STATUS } from './WorkflowTasks'; +export { TASK_STATUS, usePopupRecordContext } from './WorkflowTasks'; diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/Plugin.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/Plugin.ts index 698e2e7389..658054b29f 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/Plugin.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/Plugin.ts @@ -793,10 +793,16 @@ export default class PluginWorkflowServer extends Plugin { /** * @experimental */ - public async toggleTaskStatus(task: WorkflowTaskModel, done: boolean, { transaction }: Transactionable) { + public async toggleTaskStatus(task: WorkflowTaskModel, on: boolean, { transaction }: Transactionable) { const { db } = this.app; const repository = db.getRepository('workflowTasks') as WorkflowTasksRepository; - if (done) { + if (on) { + await repository.updateOrCreate({ + filterKeys: ['key', 'type'], + values: task, + transaction, + }); + } else { await repository.destroy({ filter: { type: task.type, @@ -804,12 +810,6 @@ export default class PluginWorkflowServer extends Plugin { }, transaction, }); - } else { - await repository.updateOrCreate({ - filterKeys: ['key', 'type'], - values: task, - transaction, - }); } // NOTE: