Skip to content

Commit 9081467

Browse files
authored
Merge pull request #10 from devforth/feature/AdminForth/1073/update-foreigninlineli-to-crea
feat: create copy of resource, instead of reffering to the parent res…
2 parents 3af9698 + d8fdbf2 commit 9081467

File tree

2 files changed

+15
-61
lines changed

2 files changed

+15
-61
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/${props.meta.pluginInstanceId}/start_bulk_action`,
285+
path: `/plugin/start_bulk_action`,
286286
method: 'POST',
287287
body: {
288288
resourceId: listResource.value.resourceId,

index.ts

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

1212
export default class ForeignInlineListPlugin extends AdminForthPlugin {
1313
foreignResource: AdminForthResource;
14+
copyOfForeignResource: AdminForthResource;
1415
options: PluginOptions;
1516
adminforth: IAdminForth;
1617

@@ -29,76 +30,19 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
2930
method: 'POST',
3031
path: `/plugin/${this.pluginInstanceId}/get_resource`,
3132
handler: async ({ body, adminUser }) => {
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);
33+
const { allowedActions } = await interpretResource(adminUser, this.copyOfForeignResource, {}, ActionCheckSource.DisplayButtons, this.adminforth);
4434

4535
return {
4636
resource: {
47-
...resourceCopy,
37+
...this.copyOfForeignResource,
4838
options: {
49-
...resourceCopy.options,
39+
...this.copyOfForeignResource.options,
5040
allowedActions,
5141
},
5242
}
5343
};
5444
}
5545
});
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-
})
10246
server.endpoint({
10347
method: 'POST',
10448
path: `/plugin/${this.pluginInstanceId}/get_default_filters`,
@@ -125,6 +69,16 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
12569

12670
// get resource with foreignResourceId
12771
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+
12882
if (!this.foreignResource) {
12983
const similar = suggestIfTypo(adminforth.config.resources.map((res) => res.resourceId), this.options.foreignResourceId);
13084
throw new Error(`ForeignInlineListPlugin: Resource with ID "${this.options.foreignResourceId}" not found. ${similar ? `Did you mean "${similar}"?` : ''}`);

0 commit comments

Comments
 (0)