@@ -11,7 +11,6 @@ import { interpretResource, ActionCheckSource } from "adminforth";
1111
1212export default class ForeignInlineListPlugin extends AdminForthPlugin {
1313 foreignResource : AdminForthResource ;
14- copyOfForeignResource : AdminForthResource ;
1514 options : PluginOptions ;
1615 adminforth : IAdminForth ;
1716
@@ -30,19 +29,76 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
3029 method : 'POST' ,
3130 path : `/plugin/${ this . pluginInstanceId } /get_resource` ,
3231 handler : async ( { body, adminUser } ) => {
33- const { allowedActions } = await interpretResource ( adminUser , this . copyOfForeignResource , { } , ActionCheckSource . DisplayButtons , this . adminforth ) ;
32+ const resource = this . adminforth . config . resources . find ( ( res ) => this . options . foreignResourceId === res . resourceId ) ;
33+ if ( ! resource ) {
34+ return { error : `Resource ${ this . options . foreignResourceId } not found` } ;
35+ }
36+ // exclude "plugins" key
37+ const resourceCopy = clone ( { ...resource , plugins : undefined } ) ;
38+
39+ if ( this . options . modifyTableResourceConfig ) {
40+ this . options . modifyTableResourceConfig ( resourceCopy ) ;
41+ }
42+
43+ const { allowedActions } = await interpretResource ( adminUser , resourceCopy , { } , ActionCheckSource . DisplayButtons , this . adminforth ) ;
3444
3545 return {
3646 resource : {
37- ...this . copyOfForeignResource ,
47+ ...resourceCopy ,
3848 options : {
39- ...this . copyOfForeignResource . options ,
49+ ...resourceCopy . options ,
4050 allowedActions,
4151 } ,
4252 }
4353 } ;
4454 }
4555 } ) ;
56+ server . endpoint ( {
57+ method : 'POST' ,
58+ path : `/plugin/${ this . pluginInstanceId } /start_bulk_action` ,
59+ handler : async ( { body, adminUser, tr } ) => {
60+ const { resourceId, actionId, recordIds } = body ;
61+ const resource = this . adminforth . config . resources . find ( ( res ) => res . resourceId == resourceId ) ;
62+ if ( ! resource ) {
63+ return { error : await tr ( `Resource {resourceId} not found` , 'errors' , { resourceId } ) } ;
64+ }
65+
66+ const resourceCopy = JSON . parse ( JSON . stringify ( { ...resource , plugins : undefined } ) ) ;
67+
68+
69+ if ( this . options . modifyTableResourceConfig ) {
70+ this . options . modifyTableResourceConfig ( resourceCopy ) ;
71+ }
72+
73+ const { allowedActions } = await interpretResource (
74+ adminUser ,
75+ resourceCopy ,
76+ { requestBody : body } ,
77+ ActionCheckSource . BulkActionRequest ,
78+ this . adminforth
79+ ) ;
80+
81+ const action = resourceCopy . options . bulkActions . find ( ( act ) => act . id == actionId ) ;
82+ if ( ! action ) {
83+ return { error : await tr ( `Action {actionId} not found` , 'errors' , { actionId } ) } ;
84+ }
85+
86+ if ( action . allowed ) {
87+ const execAllowed = await action . allowed ( { adminUser, resourceCopy, selectedIds : recordIds , allowedActions } ) ;
88+ if ( ! execAllowed ) {
89+ return { error : await tr ( `Action "{actionId}" not allowed` , 'errors' , { actionId : action . label } ) } ;
90+ }
91+ }
92+ const response = await action . action ( { selectedIds : recordIds , adminUser, resourceCopy, tr} ) ;
93+
94+ return {
95+ actionId,
96+ recordIds,
97+ resourceId,
98+ ...response
99+ }
100+ }
101+ } )
46102 server . endpoint ( {
47103 method : 'POST' ,
48104 path : `/plugin/${ this . pluginInstanceId } /get_default_filters` ,
@@ -69,16 +125,6 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
69125
70126 // get resource with foreignResourceId
71127 this . foreignResource = adminforth . config . resources . find ( ( resource ) => resource . resourceId === this . options . foreignResourceId ) ;
72- this . copyOfForeignResource = clone ( { ...this . foreignResource , plugins : undefined } ) ;
73- const idOfNewCopy = `${ this . foreignResource . resourceId } _inline_list_copy_${ this . pluginInstanceId } ` ;
74- this . copyOfForeignResource . resourceId = idOfNewCopy ;
75- adminforth . config . resources . push ( this . copyOfForeignResource ) ;
76-
77- if ( this . options . modifyTableResourceConfig ) {
78- this . options . modifyTableResourceConfig ( this . copyOfForeignResource ) ;
79- }
80-
81-
82128 if ( ! this . foreignResource ) {
83129 const similar = suggestIfTypo ( adminforth . config . resources . map ( ( res ) => res . resourceId ) , this . options . foreignResourceId ) ;
84130 throw new Error ( `ForeignInlineListPlugin: Resource with ID "${ this . options . foreignResourceId } " not found. ${ similar ? `Did you mean "${ similar } "?` : '' } ` ) ;
0 commit comments