Skip to content

Commit 7c0d0bf

Browse files
committed
feat: enhance resource handling by adding currentValue support and optimizing foreign data loading
1 parent 4721fca commit 7c0d0bf

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

adminforth/modules/restApi.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ const getResourceForeignDataRequestSchema: AnySchemaObject = {
362362
search: { type: 'string' },
363363
filters: commonFiltersSchema,
364364
sort: commonSortSchema,
365+
currentValue: {
366+
description: 'When set, guarantees this PK value appears in the returned items even if it falls outside the requested page.',
367+
},
365368
},
366369
additionalProperties: true,
367370
};
@@ -1635,7 +1638,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
16351638
request_schema: getResourceForeignDataRequestSchema,
16361639
response_schema: getResourceForeignDataResponseSchema,
16371640
handler: async ({ body, adminUser, headers, query, cookies, requestUrl }) => {
1638-
const { resourceId, column, search } = body;
1641+
const { resourceId, column, search, currentValue } = body;
16391642
if (!this.adminforth.statuses.dbDiscover) {
16401643
return { error: 'Database discovery not started' };
16411644
}
@@ -1746,6 +1749,12 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
17461749
normalizedFilters.subFilters.push(searchFilters[0]);
17471750
}
17481751
}
1752+
if (currentValue !== undefined && currentValue !== null && currentValue !== '') {
1753+
const pkField = targetResource.columns.find((col) => col.primaryKey);
1754+
if (pkField) {
1755+
normalizedFilters.subFilters.push({ field: pkField.name, operator: AdminForthFilterOperators.EQ, value: currentValue });
1756+
}
1757+
}
17491758
const dbDataItems = await this.adminforth.connectors[targetResource.dataSource].getData({
17501759
resource: targetResource,
17511760
limit,

adminforth/spa/src/components/ResourceForm.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ watch(() => props.resource.columns, async (newColumns) => {
281281
columnEmptyResultsCount[column.name] = 0;
282282
283283
await loadMoreOptions(column.name);
284+
285+
const currentFkValue = props.record?.[column.name];
286+
if (currentFkValue != null && currentFkValue !== '') {
287+
const inOptions = columnOptions.value[column.name]?.some((opt: any) => opt.value == currentFkValue);
288+
if (!inOptions) {
289+
const result = await callAdminForthApi({
290+
method: 'POST',
291+
path: '/get_resource_foreign_data',
292+
body: { resourceId: router.currentRoute.value.params.resourceId, column: column.name, limit: 1, offset: 0, currentValue: currentFkValue },
293+
});
294+
if (result?.items?.length) {
295+
columnOptions.value[column.name].unshift(...result.items);
296+
}
297+
}
298+
}
284299
}
285300
}
286301
}
@@ -325,7 +340,7 @@ async function loadMoreOptions(columnName: string, searchTerm = '') {
325340
columnOptions,
326341
columnLoadingState,
327342
columnOffsets,
328-
columnEmptyResultsCount
343+
columnEmptyResultsCount,
329344
});
330345
}
331346

adminforth/spa/src/utils/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ export async function loadMoreForeignOptions({
452452
if (!columnOptions.value[columnName]) {
453453
columnOptions.value[columnName] = [];
454454
}
455-
columnOptions.value[columnName].push(...list.items);
455+
const existingValues = new Set(columnOptions.value[columnName].map((o: any) => o.value));
456+
columnOptions.value[columnName].push(...list.items.filter((o: any) => !existingValues.has(o.value)));
456457

457458
columnOffsets[columnName] += 100;
458459

0 commit comments

Comments
 (0)