refactor(plugin-workflow): add property to determine workflow type triggerable on ui (#2890)

This commit is contained in:
Junyi 2023-10-21 21:44:35 +08:00 committed by GitHub
parent cde25ed871
commit 5fad821eab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import { ArrayTable } from '@formily/antd-v5';
import { onFieldValueChange, onFieldInputValueChange } from '@formily/core';
import { onFieldValueChange, onFieldInputValueChange, onFieldInit } from '@formily/core';
import { connect, ISchema, mapProps, useField, useFieldSchema, useForm, useFormEffects } from '@formily/react';
import { isValid, uid } from '@formily/shared';
import { Alert, Tree as AntdTree, ModalProps } from 'antd';
@ -17,6 +17,7 @@ import { useSyncFromForm } from '../../../schema-settings/DataTemplates/utils';
import { DefaultValueProvider } from '../../../schema-settings/hooks/useIsAllowToSetDefaultValue';
import { useLinkageAction } from './hooks';
import { requestSettingsSchema } from './utils';
import { usePlugin } from '../../../application/hooks';
const Tree = connect(
AntdTree,
@ -713,7 +714,7 @@ function RemoveButton(
);
}
function FormWorkflowSelect(props) {
function WorkflowSelect({ types, ...props }) {
const index = ArrayTable.useIndex();
const { setValuesIn } = useForm();
const baseCollection = useCollection();
@ -745,7 +746,7 @@ function FormWorkflowSelect(props) {
action: 'list',
params: {
filter: {
type: 'form',
type: types,
enabled: true,
'config.collection': workflowCollection,
},
@ -760,6 +761,8 @@ function WorkflowConfig() {
const { t } = useTranslation();
const fieldSchema = useFieldSchema();
const { name: collection } = useCollection();
const workflowPlugin = usePlugin('workflow') as any;
const workflowTypes = workflowPlugin.getTriggersOptions().filter((item) => item.options.actionTriggerable);
const description = {
submit: t('Workflow will be triggered after submitting succeeded.', { ns: 'workflow' }),
'customize:save': t('Workflow will be triggered after saving succeeded.', { ns: 'workflow' }),
@ -777,7 +780,7 @@ function WorkflowConfig() {
components={{
Alert,
ArrayTable,
FormWorkflowSelect,
WorkflowSelect,
}}
schema={
{
@ -838,13 +841,14 @@ function WorkflowConfig() {
workflowKey: {
type: 'number',
'x-decorator': 'FormItem',
'x-component': 'FormWorkflowSelect',
'x-component': 'WorkflowSelect',
'x-component-props': {
placeholder: t('Select workflow', { ns: 'workflow' }),
fieldNames: {
label: 'title',
value: 'key',
},
types: workflowTypes.map((item) => item.value),
},
required: true,
},
@ -859,7 +863,6 @@ function WorkflowConfig() {
properties: {
remove: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayTable.Remove',
},
},

View File

@ -11,11 +11,17 @@ import { ExecutionPage } from './ExecutionPage';
import { WorkflowPage } from './WorkflowPage';
import { WorkflowProvider } from './WorkflowProvider';
import { DynamicExpression } from './components/DynamicExpression';
import { triggers, useTrigger, getTriggersOptions } from './triggers';
import { instructions } from './nodes';
import { WorkflowTodo } from './nodes/manual/WorkflowTodo';
import { WorkflowTodoBlockInitializer } from './nodes/manual/WorkflowTodoBlockInitializer';
import { useTriggerWorkflowsActionProps } from './triggers/form';
export class WorkflowPlugin extends Plugin {
triggers = triggers;
getTriggersOptions = getTriggersOptions;
instructions = instructions;
async load() {
this.addRoutes();
this.addScopes();

View File

@ -101,6 +101,7 @@ export default {
};
},
initializers: {},
actionTriggerable: true,
};
function getFormValues({

View File

@ -61,6 +61,7 @@ export interface Trigger {
components?: { [key: string]: any };
useInitializers?(config): SchemaInitializerItemOptions | null;
initializers?: any;
actionTriggerable?: boolean;
}
export const triggers = new Registry<Trigger>();
@ -311,9 +312,10 @@ export function useTrigger() {
}
export function getTriggersOptions() {
return Array.from(triggers.getEntities()).map(([value, { title }]) => ({
return Array.from(triggers.getEntities()).map(([value, { title, ...options }]) => ({
value,
label: title,
color: 'gold',
options,
}));
}

View File

@ -196,3 +196,7 @@ export async function sync(context: Context, next) {
await next();
}
export async function trigger(context: Context, next) {
return next();
}

View File

@ -1,3 +1,4 @@
export * from './utils';
export * from './constants';
export type * from './instructions';
export { Trigger } from './triggers';

View File

@ -12,12 +12,9 @@ export default class FormTrigger extends Trigger {
super(plugin);
plugin.app.resourcer.use(this.middleware);
plugin.app.actions({
['workflows:trigger']: this.triggerAction,
});
}
triggerAction = async (context, next) => {
async triggerAction(context, next) {
const { triggerWorkflows } = context.action.params;
if (!triggerWorkflows) {
@ -28,21 +25,26 @@ export default class FormTrigger extends Trigger {
await next();
this.trigger(context);
};
}
middleware = async (context, next) => {
await next();
const {
resourceName,
actionName,
params: { triggerWorkflows },
} = context.action;
if (resourceName === 'workflows' && actionName === 'trigger') {
return this.triggerAction(context, next);
}
await next();
if (!triggerWorkflows) {
return;
}
if ((resourceName === 'workflows' && actionName === 'trigger') || !['create', 'update'].includes(actionName)) {
if (!['create', 'update'].includes(actionName)) {
return;
}