2023-12-07 21:46:58 +08:00
import { useForm } from '@formily/react' ;
2024-03-03 23:06:24 +08:00
import {
SchemaInitializerItemType ,
useCollectionDataSource ,
useCollectionManager_deprecated ,
useCompile ,
} from '@nocobase/client' ;
2023-12-07 21:46:58 +08:00
import { Trigger , CollectionBlockInitializer , getCollectionFieldOptions } from '@nocobase/plugin-workflow/client' ;
import { NAMESPACE , useLang } from '../locale' ;
export default class extends Trigger {
2024-03-06 14:42:20 +08:00
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 } " })}} ` ;
2023-12-07 21:46:58 +08:00
fieldset = {
collection : {
type : 'string' ,
required : true ,
'x-decorator' : 'FormItem' ,
'x-component' : 'CollectionSelect' ,
'x-component-props' : {
className : 'auto-width' ,
} ,
2024-03-06 14:42:20 +08:00
title : ` {{t("Collection", { ns: " ${ NAMESPACE } " })}} ` ,
description : ` {{t("Which collection record belongs to.", { ns: " ${ NAMESPACE } " })}} ` ,
2023-12-07 21:46:58 +08:00
'x-reactions' : [
{
target : 'appends' ,
effects : [ 'onFieldValueChange' ] ,
fulfill : {
state : {
value : [ ] ,
} ,
} ,
} ,
] ,
} ,
appends : {
type : 'array' ,
title : ` {{t("Associations to use", { ns: " ${ NAMESPACE } " })}} ` ,
2024-03-06 14:42:20 +08:00
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" })}} ` ,
2023-12-07 21:46:58 +08:00
'x-decorator' : 'FormItem' ,
'x-component' : 'AppendsTreeSelect' ,
'x-component-props' : {
title : 'Preload associations' ,
multiple : true ,
2024-03-04 16:45:10 +08:00
useCollection() {
2023-12-07 21:46:58 +08:00
const { values } = useForm ( ) ;
return values ? . collection ;
} ,
} ,
'x-reactions' : [
{
dependencies : [ 'collection' ] ,
fulfill : {
state : {
visible : '{{!!$deps[0]}}' ,
} ,
} ,
} ,
] ,
} ,
} ;
scope = {
useCollectionDataSource ,
} ;
2024-02-02 14:49:27 +08:00
isActionTriggerable = ( config , context ) = > {
2024-03-06 14:42:20 +08:00
return [ 'create' , 'update' , 'customize:update' , 'customize:triggerWorkflows' ] . includes ( context . action ) ;
2024-02-02 14:49:27 +08:00
} ;
2023-12-07 21:46:58 +08:00
useVariables ( config , options ) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const compile = useCompile ( ) ;
// eslint-disable-next-line react-hooks/rules-of-hooks
2024-03-03 23:06:24 +08:00
const { getCollectionFields } = useCollectionManager_deprecated ( ) ;
2023-12-07 21:46:58 +08:00
// eslint-disable-next-line react-hooks/rules-of-hooks
const langTriggerData = useLang ( 'Trigger data' ) ;
// eslint-disable-next-line react-hooks/rules-of-hooks
2024-03-06 14:42:20 +08:00
const langUserSubmittedForm = useLang ( 'User submitted action' ) ;
2023-12-13 20:08:02 +08:00
// eslint-disable-next-line react-hooks/rules-of-hooks
2024-03-06 14:42:20 +08:00
const langRoleSubmittedForm = useLang ( 'Role of user submitted action' ) ;
2023-12-07 21:46:58 +08:00
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 ,
} ,
} ,
2023-12-13 20:08:02 +08:00
{
name : 'roleName' ,
uiSchema : {
title : langRoleSubmittedForm ,
} ,
} ,
2023-12-07 21:46:58 +08:00
] ;
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}}' ,
} ;
}
}