Skip to content

Commit 968602b

Browse files
committed
Merge branch 'next' of github.com:devforth/adminforth into next
2 parents bd1b130 + 5cd4f0d commit 968602b

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

adminforth/modules/configValidator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ export default class ConfigValidator implements IConfigValidator {
530530
}
531531

532532
if (col.foreignResource) {
533-
534533
if (!col.foreignResource.resourceId) {
535534
// resourceId is absent or empty
536535
if (!col.foreignResource.polymorphicResources && !col.foreignResource.polymorphicOn) {
@@ -550,11 +549,11 @@ export default class ConfigValidator implements IConfigValidator {
550549
}
551550
// we do || here because 'resourceId' might yet not be assigned from 'table'
552551
col.foreignResource.polymorphicResources.forEach((polymorphicResource, polymorphicResourceIndex) => {
553-
if (!polymorphicResource.resourceId) {
552+
if (polymorphicResource.resourceId === undefined) {
554553
errors.push(`Resource "${res.resourceId}" column "${col.name}" has polymorphic foreign resource without resourceId`);
555554
} else if (!polymorphicResource.whenValue) {
556555
errors.push(`Resource "${res.resourceId}" column "${col.name}" has polymorphic foreign resource without whenValue`);
557-
} else {
556+
} else if (polymorphicResource.resourceId !== null) {
558557
const resource = this.inputConfig.resources.find((r) => r.resourceId === polymorphicResource.resourceId || r.table === polymorphicResource.resourceId);
559558
if (!resource) {
560559
const similar = suggestIfTypo(this.inputConfig.resources.map((r) => r.resourceId || r.table), polymorphicResource.resourceId);

adminforth/modules/restApi.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
703703
const targetResourcePkFields = {};
704704
const pksUniques = {};
705705
col.foreignResource.polymorphicResources.forEach((pr) => {
706-
targetResources[pr.whenValue] = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
706+
if (pr.resourceId === null) {
707+
return;
708+
}
709+
const targetResource = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
710+
if (!targetResource) {
711+
return;
712+
}
713+
targetResources[pr.whenValue] = targetResource;
707714
targetConnectors[pr.whenValue] = this.adminforth.connectors[targetResources[pr.whenValue].dataSource];
708715
targetResourcePkFields[pr.whenValue] = targetResources[pr.whenValue].columns.find((col) => col.primaryKey).name;
709716
const pksUnique = [...new Set(data.data.filter((item) => item[col.foreignResource.polymorphicOn] === pr.whenValue).map((item) => item[col.name]))];
@@ -828,7 +835,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
828835
return { error: `Column '${column}' in resource '${resourceId}' is not a foreign key` };
829836
}
830837

831-
const targetResourceIds = columnConfig.foreignResource.resourceId ? [columnConfig.foreignResource.resourceId] : columnConfig.foreignResource.polymorphicResources.map((pr) => pr.resourceId);
838+
const targetResourceIds = columnConfig.foreignResource.resourceId ? [columnConfig.foreignResource.resourceId] : columnConfig.foreignResource.polymorphicResources.filter(pr => pr.resourceId !== null).map((pr) => pr.resourceId);
832839
const targetResources = targetResourceIds.map((trId) => this.adminforth.config.resources.find((res) => res.resourceId == trId));
833840

834841
const responses = (await Promise.all(
@@ -983,7 +990,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
983990
const targetConnectors = {};
984991
const targetResourcePkFields = {};
985992
column.foreignResource.polymorphicResources.forEach((pr) => {
986-
targetResources[pr.whenValue] = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
993+
if (pr.resourceId === null) {
994+
return;
995+
}
996+
const targetResource = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
997+
if (!targetResource) {
998+
return;
999+
}
1000+
targetResources[pr.whenValue] = targetResource;
9871001
targetConnectors[pr.whenValue] = this.adminforth.connectors[targetResources[pr.whenValue].dataSource];
9881002
targetResourcePkFields[pr.whenValue] = targetResources[pr.whenValue].columns.find((col) => col.primaryKey).name;
9891003
});
@@ -1064,14 +1078,21 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
10641078

10651079
// for polymorphic foreign resources, we need to find out the value for polymorphicOn column
10661080
for (const column of resource.columns) {
1067-
if (column.foreignResource?.polymorphicOn) {
1081+
if (column.foreignResource?.polymorphicOn && record[column.name] !== undefined) {
10681082
let newPolymorphicOnValue = null;
10691083
if (record[column.name]) {
10701084
const targetResources = {};
10711085
const targetConnectors = {};
10721086
const targetResourcePkFields = {};
10731087
column.foreignResource.polymorphicResources.forEach((pr) => {
1074-
targetResources[pr.whenValue] = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
1088+
if (pr.resourceId === null) {
1089+
return;
1090+
}
1091+
const targetResource = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
1092+
if (!targetResource) {
1093+
return;
1094+
}
1095+
targetResources[pr.whenValue] = targetResource;
10751096
targetConnectors[pr.whenValue] = this.adminforth.connectors[targetResources[pr.whenValue].dataSource];
10761097
targetResourcePkFields[pr.whenValue] = targetResources[pr.whenValue].columns.find((col) => col.primaryKey).name;
10771098
});

adminforth/spa/src/components/ResourceListTable.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
<!-- table header -->
1919
<tr class="t-header sticky z-10 top-0 text-xs bg-lightListTableHeading dark:bg-darkListTableHeading dark:text-gray-400">
2020
<td scope="col" class="p-4">
21-
<div v-if="rows && rows.length" class="flex items-center">
21+
<div class="flex items-center">
2222
<input id="checkbox-all-search" type="checkbox" :checked="allFromThisPageChecked" @change="selectAll()"
23+
:disabled="!rows || !rows.length"
2324
class="w-4 h-4 cursor-pointer text-blue-600 bg-gray-100 border-gray-300 rounded
2425
focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 dark:focus:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
2526
<label for="checkbox-all-search" class="sr-only">{{ $t('checkbox') }}</label>
2627
</div>
2728
</td>
2829

29-
<td v-for="c in columnsListed" scope="col" class="px-2 md:px-3 lg:px-6 py-3">
30+
<td v-for="c in columnsListed" ref="headerRefs" scope="col" class="px-2 md:px-3 lg:px-6 py-3">
3031

3132
<div @click="(evt) => c.sortable && onSortButtonClick(evt, c.name)"
3233
class="flex items-center " :class="{'cursor-pointer':c.sortable}">
@@ -65,6 +66,7 @@
6566
:columns="resource?.columns.filter(c => c.showIn.list).length + 2"
6667
:rows="rowHeights.length || 3"
6768
:row-heights="rowHeights"
69+
:column-widths="columnWidths"
6870
/>
6971
<tr v-else-if="rows.length === 0" class="bg-lightListTable dark:bg-darkListTable dark:border-darkListTableBorder">
7072
<td :colspan="resource?.columns.length + 2">
@@ -376,10 +378,13 @@ watch(() => props.page, (newPage) => {
376378
});
377379
378380
const rowRefs = useTemplateRef('rowRefs');
381+
const headerRefs = useTemplateRef('headerRefs');
379382
const rowHeights = ref([]);
383+
const columnWidths = ref([]);
380384
watch(() => props.rows, (newRows) => {
381385
// rows are set to null when new records are loading
382386
rowHeights.value = newRows || !rowRefs.value ? [] : rowRefs.value.map((el) => el.offsetHeight);
387+
columnWidths.value = newRows || !headerRefs.value ? [] : [48, ...headerRefs.value.map((el) => el.offsetWidth)];
383388
});
384389
385390
function addToCheckedValues(id) {

adminforth/spa/src/components/SkeleteLoader.vue

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<template>
22
<tr
33
v-for="(r, ri) in new Array(props.rows)"
4-
class="bg-lightListTable border-b dark:bg-darkListTable dark:border-darkListBorder"
5-
:style="[props.rowHeights[ri] !== undefined ? `height: ${props.rowHeights[ri]}px` : '' ]"
4+
class="bg-lightListTable dark:bg-darkListTable dark:border-darkListBorder"
5+
:class="{'border-b': ri !== props.rows - 1}"
6+
:style="[`height: ${props.rowHeights[ri] !== undefined ? props.rowHeights[ri] : 52.5}px`]"
67
>
7-
<td v-for="c in new Array(props.columns)" class="items-center px-6 py-8 cursor-default" >
8+
<td
9+
v-for="(c, ci) in new Array(props.columns)" class="items-center px-6 py-4 cursor-default"
10+
:style="[props.columnWidths[ci] !== undefined
11+
? `min-width: ${props.columnWidths[ci]}px; width: ${props.columnWidths[ci]}px;`
12+
: '']"
13+
>
814
<div role="status" class="max-w-sm animate-pulse">
915
<div class="h-2 bg-gray-200 rounded-full dark:bg-gray-700 max-w-[360px]"></div>
1016
</div>
@@ -18,8 +24,10 @@ const props = withDefaults(defineProps<{
1824
columns: number;
1925
rows: number;
2026
rowHeights?: number[];
27+
columnWidths?: number[];
2128
}>(), {
2229
rowHeights: [],
30+
columnWidths: [],
2331
});
2432
2533
</script>

adminforth/spa/src/views/EditView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async function saveRecord() {
145145
146146
const column = coreStore.resource.columns.find((c) => c.name === key);
147147
if (column?.foreignResource) {
148-
columnIsUpdated = record.value[key] !== coreStore.record[key].pk;
148+
columnIsUpdated = record.value[key] !== coreStore.record[key]?.pk;
149149
}
150150
151151
if (columnIsUpdated) {

dev-demo/resources/api_keys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export default {
5454
resourceId: 'providers',
5555
whenValue: 'provider',
5656
},
57+
{
58+
resourceId: null,
59+
whenValue: 'Syst1em',
60+
},
5761
],
5862
polymorphicOn: 'owner',
5963
},

0 commit comments

Comments
 (0)