Skip to content

Commit fc49c6a

Browse files
committed
feat: add support for foreign resource array columns
This reverts commit e21f242.
1 parent e21f242 commit fc49c6a

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

adminforth/modules/configValidator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ export default class ConfigValidator implements IConfigValidator {
515515
if (col.masked) {
516516
errors.push(`Resource "${res.resourceId}" column "${col.name}" isArray cannot be used for a masked column`);
517517
}
518-
if (col.foreignResource) {
519-
errors.push(`Resource "${res.resourceId}" column "${col.name}" isArray cannot be used for a foreignResource column`);
518+
if (col.foreignResource && col.foreignResource.polymorphicResources) {
519+
errors.push(`Resource "${res.resourceId}" column "${col.name}" isArray cannot be used for a polymorphic foreignResource column`);
520520
}
521521

522522
if (!col.type || col.type !== AdminForthDataTypes.JSON) {

adminforth/modules/restApi.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,22 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
675675
const targetResource = this.adminforth.config.resources.find((res) => res.resourceId == col.foreignResource.resourceId);
676676
const targetConnector = this.adminforth.connectors[targetResource.dataSource];
677677
const targetResourcePkField = targetResource.columns.find((col) => col.primaryKey).name;
678-
const pksUnique = [...new Set(data.data.map((item) => item[col.name]))];
678+
const pksUnique = [...new Set(data.data.reduce((pks, item) => {
679+
if (col.isArray?.enabled) {
680+
if (item[col.name]?.length) {
681+
pks = pks.concat(item[col.name]);
682+
}
683+
} else {
684+
pks.push(item[col.name]);
685+
}
686+
return pks;
687+
}, []))];
679688
if (pksUnique.length === 0) {
680689
return;
681690
}
682691
const targetData = await targetConnector.getData({
683692
resource: targetResource,
684-
limit: limit,
693+
limit: pksUnique.length,
685694
offset: 0,
686695
filters: [
687696
{
@@ -755,7 +764,13 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
755764
}
756765

757766
data.data.forEach((item) => {
758-
item[col.name] = targetDataMap[item[col.name]];
767+
if (col.isArray?.enabled) {
768+
if (item[col.name]?.length) {
769+
item[col.name] = item[col.name].map((i) => targetDataMap[i]);
770+
}
771+
} else {
772+
item[col.name] = targetDataMap[item[col.name]];
773+
}
759774

760775
if (!item[col.name]) {
761776
if (col.foreignResource && col.foreignResource.polymorphicResources) {

adminforth/spa/src/components/ValueRenderer.vue

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
<template>
22
<div>
3-
<span @click="(e)=>{e.stopPropagation()}" v-if="column.foreignResource">
4-
<RouterLink v-if="record[column.name]" class="font-medium text-lightPrimary dark:text-darkPrimary hover:brightness-110 whitespace-nowrap"
3+
<span
4+
v-if="column.foreignResource"
5+
:class="{'flex flex-wrap': column.isArray?.enabled}"
6+
@click="(e)=>{e.stopPropagation()}"
7+
>
8+
<span
9+
v-if="record[column.name] && column.isArray?.enabled"
10+
v-for="foreignResource in record[column.name]"
11+
class="rounded-md m-0.5 bg-lightAnnouncementBG dark:bg-darkAnnouncementBG text-lightAnnouncementText dark:text-darkAnnouncementText py-0.5 px-2.5 text-sm"
12+
>
13+
<RouterLink
14+
class="font-medium text-lightSidebarText dark:text-darkSidebarText hover:brightness-110 whitespace-nowrap"
15+
:to="{ name: 'resource-show', params: { primaryKey: foreignResource.pk, resourceId: column.foreignResource.resourceId || column.foreignResource.polymorphicResources.find((pr) => pr.whenValue === record[column.foreignResource.polymorphicOn]).resourceId } }"
16+
>
17+
{{ foreignResource.label }}
18+
</RouterLink>
19+
</span>
20+
<RouterLink v-else-if="record[column.name]" class="font-medium text-lightPrimary dark:text-darkPrimary hover:brightness-110 whitespace-nowrap"
521
:to="{ name: 'resource-show', params: { primaryKey: record[column.name].pk, resourceId: column.foreignResource.resourceId || column.foreignResource.polymorphicResources.find((pr) => pr.whenValue === record[column.foreignResource.polymorphicOn]).resourceId } }">
622
{{ record[column.name].label }}
723
</RouterLink>

dev-demo/resources/apartments.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,16 @@ export default {
223223
{
224224
name: "room_sizes",
225225
type: AdminForthDataTypes.JSON,
226+
// isArray: {
227+
// enabled: true,
228+
// itemType: AdminForthDataTypes.FLOAT,
229+
// },
226230
isArray: {
227231
enabled: true,
228-
itemType: AdminForthDataTypes.FLOAT,
232+
itemType: AdminForthDataTypes.STRING,
233+
},
234+
foreignResource: {
235+
resourceId: "users",
229236
},
230237
},
231238
{

0 commit comments

Comments
 (0)