tachybase_todo/packages/plugins/@nocobase/plugin-workflow-action-trigger/src/client/ActionTrigger.tsx
Junyi c2b121cda6
feat(plugin-workflow-form-trigger): add trigger button to all single record action bar (#3563)
* feat(plugin-workflow-form-trigger): add trigger button to all single record actionbar

* fix(plugin-workflow-form-trigger): fix button style and triggering

* fix(plugin-workflow): fix unused hook ref in workflow

* fix(plugin-workflow-form-trigger): fix button style

* refactor(plugin-workflow-action-trigger): change plugin name

* fix(plugin-workflow-action-trigger): fix unmigrated stuff

* fix(plugin-workflow-action-trigger): fix test case

* fix(plugin-workflow-action-trigger): fix test case

* fix(presets): fix package name

* fix(plugin-workflow-action-trigger): fix e2e test and migration

* fix(plugin-workflow-action-trigger): fix migration

* fix(plugin-workflow-action-trigger): fix migration

* fix(plugin-workflow-action-trigger): fix migration

* feat(plugin-workflow-action-trigger): add destroy to trigger

* fix(plugin-workflow-action-trigger): fix appends select

* fix(plugin-workflow-action-trigger): remove support for destroy action

* fix(plugin-workflow-action-trigger): fix collection check

* fix(plugin-workflow-action-trigger): fix test case

* fix(plugin-workflow-action-trigger): fix test case

* fix(plugin-workflow-action-trigger): fix test case
2024-03-06 14:42:20 +08:00

133 lines
4.0 KiB
TypeScript

import { useForm } from '@formily/react';
import {
SchemaInitializerItemType,
useCollectionDataSource,
useCollectionManager_deprecated,
useCompile,
} from '@nocobase/client';
import { Trigger, CollectionBlockInitializer, getCollectionFieldOptions } from '@nocobase/plugin-workflow/client';
import { NAMESPACE, useLang } from '../locale';
export default class extends Trigger {
title = `{{t("Action event", { ns: "${NAMESPACE}" })}}`;
description = `{{t("Triggers after specific operations on data are submitted, such as create, update, delete, etc., or directly submitting a record to the workflow.", { ns: "${NAMESPACE}" })}}`;
fieldset = {
collection: {
type: 'string',
required: true,
'x-decorator': 'FormItem',
'x-component': 'CollectionSelect',
'x-component-props': {
className: 'auto-width',
},
title: `{{t("Collection", { ns: "${NAMESPACE}" })}}`,
description: `{{t("Which collection record belongs to.", { ns: "${NAMESPACE}" })}}`,
'x-reactions': [
{
target: 'appends',
effects: ['onFieldValueChange'],
fulfill: {
state: {
value: [],
},
},
},
],
},
appends: {
type: 'array',
title: `{{t("Associations to use", { ns: "${NAMESPACE}" })}}`,
description: `{{t("Please select the associated fields that need to be accessed in subsequent nodes. With more than two levels of to-many associations may cause performance issue, please use with caution.", { ns: "workflow" })}}`,
'x-decorator': 'FormItem',
'x-component': 'AppendsTreeSelect',
'x-component-props': {
title: 'Preload associations',
multiple: true,
useCollection() {
const { values } = useForm();
return values?.collection;
},
},
'x-reactions': [
{
dependencies: ['collection'],
fulfill: {
state: {
visible: '{{!!$deps[0]}}',
},
},
},
],
},
};
scope = {
useCollectionDataSource,
};
isActionTriggerable = (config, context) => {
return ['create', 'update', 'customize:update', 'customize:triggerWorkflows'].includes(context.action);
};
useVariables(config, options) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const compile = useCompile();
// eslint-disable-next-line react-hooks/rules-of-hooks
const { getCollectionFields } = useCollectionManager_deprecated();
// eslint-disable-next-line react-hooks/rules-of-hooks
const langTriggerData = useLang('Trigger data');
// eslint-disable-next-line react-hooks/rules-of-hooks
const langUserSubmittedForm = useLang('User submitted action');
// eslint-disable-next-line react-hooks/rules-of-hooks
const langRoleSubmittedForm = useLang('Role of user submitted action');
const rootFields = [
{
collectionName: config.collection,
name: 'data',
type: 'hasOne',
target: config.collection,
uiSchema: {
title: langTriggerData,
},
},
{
collectionName: 'users',
name: 'user',
type: 'hasOne',
target: 'users',
uiSchema: {
title: langUserSubmittedForm,
},
},
{
name: 'roleName',
uiSchema: {
title: langRoleSubmittedForm,
},
},
];
const result = getCollectionFieldOptions({
// depth,
appends: ['data', 'user', ...(config.appends?.map((item) => `data.${item}`) || [])],
...options,
fields: rootFields,
compile,
getCollectionFields,
});
return result;
}
useInitializers(config): SchemaInitializerItemType | null {
if (!config.collection) {
return null;
}
return {
name: 'triggerData',
type: 'item',
key: 'triggerData',
title: `{{t("Trigger data", { ns: "${NAMESPACE}" })}}`,
Component: CollectionBlockInitializer,
collection: config.collection,
dataSource: '{{$context.data}}',
};
}
}