From 32bba9b1ab36d8e414c0b2af6fefdb5b166fb624 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 23 Dec 2024 19:34:58 +0100 Subject: [PATCH] More typings --- .../src/components/grid/stores/datasource.ts | 2 +- .../frontend-core/src/utils/relatedColumns.ts | 38 +++++++++++++------ packages/types/src/ui/stores/grid/table.ts | 12 +++++- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/datasource.ts b/packages/frontend-core/src/components/grid/stores/datasource.ts index c97ee305ca..0bda8b0d64 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.ts +++ b/packages/frontend-core/src/components/grid/stores/datasource.ts @@ -65,7 +65,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { } = context const schema = derived(definition, $definition => { - let schema: Record = getDatasourceSchema({ + let schema: Record = getDatasourceSchema({ API, datasource: get(datasource), definition: $definition, diff --git a/packages/frontend-core/src/utils/relatedColumns.ts b/packages/frontend-core/src/utils/relatedColumns.ts index 60be13bec9..6d29ef9ce4 100644 --- a/packages/frontend-core/src/utils/relatedColumns.ts +++ b/packages/frontend-core/src/utils/relatedColumns.ts @@ -1,5 +1,11 @@ -import { FieldType, RelationshipType, UIFieldSchema } from "@budibase/types" import { Helpers } from "@budibase/bbui" +import { + FieldType, + isRelationshipField, + RelationshipType, + Row, + UIFieldSchema, +} from "@budibase/types" const columnTypeManyTypeOverrides = { [FieldType.DATETIME]: FieldType.STRING, @@ -8,8 +14,8 @@ const columnTypeManyTypeOverrides = { } const columnTypeManyParser = { - [FieldType.DATETIME]: (value, field) => { - function parseDate(value) { + [FieldType.DATETIME]: (value: any[], field: any) => { + function parseDate(value: any) { const { timeOnly, dateOnly, ignoreTimezones } = field || {} const enableTime = !dateOnly const parsedValue = Helpers.parseDate(value, { @@ -26,14 +32,14 @@ const columnTypeManyParser = { return value.map(v => parseDate(v)) }, - [FieldType.BOOLEAN]: value => value.map(v => !!v), - [FieldType.BB_REFERENCE_SINGLE]: value => [ + [FieldType.BOOLEAN]: (value: any[]) => value.map(v => !!v), + [FieldType.BB_REFERENCE_SINGLE]: (value: any[]) => [ ...new Map(value.map(i => [i._id, i])).values(), ], - [FieldType.BB_REFERENCE]: value => [ + [FieldType.BB_REFERENCE]: (value: any[]) => [ ...new Map(value.map(i => [i._id, i])).values(), ], - [FieldType.ARRAY]: value => Array.from(new Set(value)), + [FieldType.ARRAY]: (value: any[]) => Array.from(new Set(value)), } export function enrichSchemaWithRelColumns( @@ -46,7 +52,11 @@ export function enrichSchemaWithRelColumns( const field = schema[fieldName] result[fieldName] = field - if (field.visible !== false && field.columns) { + if ( + field.visible !== false && + isRelationshipField(field) && + field.columns + ) { const fromSingle = field?.relationshipType === RelationshipType.ONE_TO_MANY @@ -72,8 +82,13 @@ export function enrichSchemaWithRelColumns( return result } -export function getRelatedTableValues(row, field, fromField) { +export function getRelatedTableValues( + row: Row, + field: UIFieldSchema, + fromField: UIFieldSchema +) { const fromSingle = + isRelationshipField(fromField) && fromField?.relationshipType === RelationshipType.ONE_TO_MANY let result = "" @@ -85,7 +100,8 @@ export function getRelatedTableValues(row, field, fromField) { const value = row[field.related.field] ?.flatMap(r => r[field.related.subField]) ?.filter(i => i !== undefined && i !== null) - result = parser(value || [], field) + const parsed = parser(value || [], field) + result = parsed if ( [ FieldType.STRING, @@ -97,7 +113,7 @@ export function getRelatedTableValues(row, field, fromField) { FieldType.BARCODEQR, ].includes(field.type) ) { - result = result?.join(", ") + result = parsed?.join(", ") } } diff --git a/packages/types/src/ui/stores/grid/table.ts b/packages/types/src/ui/stores/grid/table.ts index 09bbb7672d..f1040a1369 100644 --- a/packages/types/src/ui/stores/grid/table.ts +++ b/packages/types/src/ui/stores/grid/table.ts @@ -1,3 +1,11 @@ -import { BasicViewFieldMetadata, FieldSchema } from "@budibase/types" +import { + BasicViewFieldMetadata, + FieldSchema, + RelationSchemaField, +} from "@budibase/types" -export type UIFieldSchema = FieldSchema & BasicViewFieldMetadata +export type UIFieldSchema = FieldSchema & + BasicViewFieldMetadata & { + related?: { field: string; subField: string } + columns?: Record + }