Skip to content

Commit 6c0ef93

Browse files
committed
fix: update resource fetching logic to use foreignResourceId and streamline plugin endpoint
1 parent d8fdbf2 commit 6c0ef93

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

custom/InlineList.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,11 @@ onMounted( async () => {
364364
}
365365
const foreighResourceId = props.meta.foreignResourceId;
366366
listResource.value = (await callAdminForthApi({
367-
path: `/plugin/${props.meta.pluginInstanceId}/get_resource`,
367+
path: `/get_resource`,
368368
method: 'POST',
369-
body: {},
369+
body: {
370+
resourceId: foreighResourceId,
371+
},
370372
})).resource;
371373
372374
if (listResource.value?.options?.allowedActions?.create && listResourceRefColumn.value && !listResourceRefColumn.value.showIn.create) {

index.ts

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
1515
options: PluginOptions;
1616
adminforth: IAdminForth;
1717

18+
activationOrder: number = -10000000;
19+
1820
constructor(options: PluginOptions) {
1921
super(options, import.meta.url);
2022
this.options = options;
@@ -66,26 +68,15 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
6668
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
6769
super.modifyResourceConfig(adminforth, resourceConfig);
6870
this.adminforth = adminforth;
69-
70-
// get resource with foreignResourceId
7171
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-
8172

8273
if (!this.foreignResource) {
8374
const similar = suggestIfTypo(adminforth.config.resources.map((res) => res.resourceId), this.options.foreignResourceId);
8475
throw new Error(`ForeignInlineListPlugin: Resource with ID "${this.options.foreignResourceId}" not found. ${similar ? `Did you mean "${similar}"?` : ''}`);
8576
}
77+
const idOfNewCopy = `${this.foreignResource.resourceId}_inline_list__from_${this.resourceConfig.resourceId}__`;
8678

8779

88-
const defaultSort = this.foreignResource.options?.defaultSort;
8980
const newColumn = {
9081
name: `foreignInlineList_${this.foreignResource.resourceId}`,
9182
label: 'Foreign Inline List',
@@ -105,15 +96,7 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
10596
...this.options,
10697
pluginInstanceId: this.pluginInstanceId,
10798
disableForeignListResourceRefColumn: this.options.disableForeignListResourceRefColumn,
108-
...(defaultSort
109-
? {
110-
defaultSort: {
111-
field: defaultSort.columnName,
112-
direction: defaultSort.direction,
113-
}
114-
}
115-
: {}
116-
)
99+
foreignResourceId: idOfNewCopy
117100
}
118101
}
119102
},
@@ -156,5 +139,48 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
156139
} else {
157140
resourceConfig.columns.push(newColumn);
158141
}
142+
143+
// get resource with foreignResourceId
144+
this.copyOfForeignResource = clone({ ...this.foreignResource, plugins: [] });
145+
146+
// if we install on plugin which is already a copy, adjust foreignResource references
147+
if (this.resourceConfig.resourceId.includes('_inline_list__from_')) {
148+
const originalResourceIdPart = this.resourceConfig.resourceId.split('_inline_list__from_')[0];
149+
// find column in copied resource which is foreignResource.resourceId equal to originalResourceIdPart
150+
// and change it to point to this.resourceConfig.resourceId
151+
const foreignRefColumn = this.copyOfForeignResource.columns.find(col => col.foreignResource?.resourceId === originalResourceIdPart);
152+
if (foreignRefColumn) {
153+
foreignRefColumn.foreignResource.resourceId = this.resourceConfig.resourceId;
154+
}
155+
}
156+
157+
// if foreignInlineList_ column already created, remove it
158+
this.copyOfForeignResource.columns = this.copyOfForeignResource.columns.filter(col => !col.name.startsWith('foreignInlineList_'));
159+
// we should not cate about modifications made by other plugins, while activationOrder of this plugin is very low (negative)
160+
161+
this.copyOfForeignResource.resourceId = idOfNewCopy;
162+
adminforth.config.resources.push(this.copyOfForeignResource);
163+
164+
if (this.options.modifyTableResourceConfig) {
165+
this.options.modifyTableResourceConfig(this.copyOfForeignResource);
166+
}
167+
168+
// now we need to create a copy of all plugins of foreignResource,
169+
for (const plugin of this.foreignResource.plugins || []) {
170+
const options = plugin.pluginOptions;
171+
// call constructor
172+
const pluginCopy = new (plugin.constructor as any)(options);
173+
this.copyOfForeignResource.plugins.push(pluginCopy);
174+
}
175+
176+
// activate plugins for the copyOfForeignResource
177+
for (const plugin of this.copyOfForeignResource.plugins.sort((a, b) => a.activationOrder - b.activationOrder)) {
178+
// if there already is a plugin with same instanceUniqueRepresentation, skip
179+
if (plugin.modifyResourceConfig) {
180+
await plugin.modifyResourceConfig(adminforth, this.copyOfForeignResource);
181+
}
182+
}
183+
184+
159185
}
160186
}

0 commit comments

Comments
 (0)