From c84d2449f2f306e7859bf868f0eda5178518f367 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 3 Oct 2023 14:26:12 +0200 Subject: [PATCH] Smarter getValidOperatorsForType --- .../controls/FilterEditor/FilterDrawer.svelte | 26 ++++++++--------- packages/shared-core/src/filters.ts | 28 +++++++++++-------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/packages/builder/src/components/design/settings/controls/FilterEditor/FilterDrawer.svelte b/packages/builder/src/components/design/settings/controls/FilterEditor/FilterDrawer.svelte index dca6cd9294..5d56b131b0 100644 --- a/packages/builder/src/components/design/settings/controls/FilterEditor/FilterDrawer.svelte +++ b/packages/builder/src/components/design/settings/controls/FilterEditor/FilterDrawer.svelte @@ -184,26 +184,22 @@ } const getValidOperatorsForType = filter => { + const fieldSchema = getSchema(filter) + const type = + fieldSchema.type !== FieldType.BB_REFERENCE + ? field.type + : { + type: fieldSchema.type, + multiple: + fieldSchema.relationshipType === RelationshipType.MANY_TO_MANY, + } + let operators = LuceneUtils.getValidOperatorsForType( - filter.type, + type, filter.field, datasource ) - const fieldSchema = getSchema(filter) - if ( - fieldSchema.type === FieldType.BB_REFERENCE && - fieldSchema.relationshipType !== RelationshipType.MANY_TO_MANY - ) { - operators = operators.filter( - o => - ![ - OperatorOptions.Contains.value, - OperatorOptions.NotContains.value, - ].includes(o.value) - ) - } - return operators } diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 57add48ebd..7ed2a9f49a 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -12,12 +12,22 @@ import { deepGet } from "./helpers" const HBS_REGEX = /{{([^{].*?)}}/g +type RequestedFieldType = + | Exclude + | { type: FieldType.BB_REFERENCE; multiple: boolean } + +export function isFieldType( + r: RequestedFieldType +): r is Exclude { + return typeof r === "string" && Object.values(FieldType).includes(r) +} + /** * Returns the valid operator options for a certain data type * @param type the data type */ export const getValidOperatorsForType = ( - type: FieldType, + type: RequestedFieldType, field: string, datasource: Datasource & { tableId: any } // TODO: is this table id ever populated? ) => { @@ -60,16 +70,12 @@ export const getValidOperatorsForType = ( ops = numOps } else if (type === FieldType.FORMULA) { ops = stringOps.concat([Op.MoreThan, Op.LessThan]) - } else if (type === FieldType.BB_REFERENCE) { - ops = [ - Op.Equals, - Op.NotEquals, - Op.Empty, - Op.NotEmpty, - Op.Contains, - Op.NotContains, - Op.ContainsAny, - ] + } else if (!isFieldType(type) && type.type === FieldType.BB_REFERENCE) { + if (type.multiple) { + ops = [Op.Contains, Op.NotContains, Op.ContainsAny, Op.Empty, Op.NotEmpty] + } else { + ops = [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty, Op.ContainsAny] + } } // Only allow equal/not equal for _id in SQL tables