Skip to content

Commit d29e881

Browse files
committed
Revert "Merge pull request #10 from devforth/feature/AdminForth/1073/update-foreigninlineli-to-crea"
This reverts commit 9081467, reversing changes made to 3af9698.
1 parent 9081467 commit d29e881

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

custom/InlineList.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ async function startBulkAction(actionId) {
282282
bulkActionLoadingStates.value[actionId] = true;
283283
284284
const data = await callAdminForthApi({
285-
path: `/plugin/start_bulk_action`,
285+
path: `/plugin/${props.meta.pluginInstanceId}/start_bulk_action`,
286286
method: 'POST',
287287
body: {
288288
resourceId: listResource.value.resourceId,

index.ts

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { interpretResource, ActionCheckSource } from "adminforth";
1111

1212
export 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

Comments
 (0)