From ed30e51733107884d96527e349b186c46c89495c Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 30 Dec 2024 12:56:25 +0100 Subject: [PATCH 1/2] Initial conversion --- .../stores/{conditions.js => conditions.ts} | 19 +++++++++++++++---- .../src/components/grid/stores/index.ts | 3 ++- 2 files changed, 17 insertions(+), 5 deletions(-) rename packages/frontend-core/src/components/grid/stores/{conditions.js => conditions.ts} (89%) diff --git a/packages/frontend-core/src/components/grid/stores/conditions.js b/packages/frontend-core/src/components/grid/stores/conditions.ts similarity index 89% rename from packages/frontend-core/src/components/grid/stores/conditions.js rename to packages/frontend-core/src/components/grid/stores/conditions.ts index 3800f21df1..f56b8df47c 100644 --- a/packages/frontend-core/src/components/grid/stores/conditions.js +++ b/packages/frontend-core/src/components/grid/stores/conditions.ts @@ -1,15 +1,26 @@ -import { writable, get } from "svelte/store" +import { writable, get, Writable, Readable } from "svelte/store" import { derivedMemo, QueryUtils } from "../../../utils" import { FieldType, EmptyFilterOption } from "@budibase/types" +import { Store as StoreContext } from "." -export const createStores = () => { +interface ConditionStore { + metadata: Writable +} + +interface ConditionDerivedStore { + conditions: Readable +} + +export type Store = ConditionStore & ConditionDerivedStore + +export const createStores = (): ConditionStore => { const metadata = writable({}) return { metadata, } } -export const deriveStores = context => { +export const deriveStores = (context: StoreContext): ConditionDerivedStore => { const { columns } = context // Derive and memoize the cell conditions present in our columns so that we @@ -33,7 +44,7 @@ export const deriveStores = context => { } } -export const initialise = context => { +export const initialise = (context: StoreContext) => { const { metadata, conditions, rows } = context // Recompute all metadata if conditions change diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index 30a1a923f2..9581f9ff7c 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -105,7 +105,8 @@ export type Store = BaseStore & } & Rows.Store & Reorder.Store & Resize.Store & - Config.Store + Config.Store & + Conditions.Store export const attachStores = (context: Store): Store => { // Atomic store creation From d728b9f25a58a92bdb28c2eb266fa47ebcfcdfb5 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 30 Dec 2024 13:05:27 +0100 Subject: [PATCH 2/2] Type anys --- .../src/components/grid/stores/conditions.ts | 29 ++++++++++++------- .../types/src/ui/stores/grid/condition.ts | 11 +++++++ packages/types/src/ui/stores/grid/index.ts | 1 + 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 packages/types/src/ui/stores/grid/condition.ts diff --git a/packages/frontend-core/src/components/grid/stores/conditions.ts b/packages/frontend-core/src/components/grid/stores/conditions.ts index f56b8df47c..cf47456b44 100644 --- a/packages/frontend-core/src/components/grid/stores/conditions.ts +++ b/packages/frontend-core/src/components/grid/stores/conditions.ts @@ -1,14 +1,19 @@ import { writable, get, Writable, Readable } from "svelte/store" import { derivedMemo, QueryUtils } from "../../../utils" -import { FieldType, EmptyFilterOption } from "@budibase/types" +import { + FieldType, + EmptyFilterOption, + UIRow, + UICondition, +} from "@budibase/types" import { Store as StoreContext } from "." interface ConditionStore { - metadata: Writable + metadata: Writable> } interface ConditionDerivedStore { - conditions: Readable + conditions: Readable } export type Store = ConditionStore & ConditionDerivedStore @@ -49,7 +54,7 @@ export const initialise = (context: StoreContext) => { // Recompute all metadata if conditions change conditions.subscribe($conditions => { - let newMetadata = {} + let newMetadata: Record = {} if ($conditions?.length) { for (let row of get(rows)) { newMetadata[row._id] = evaluateConditions(row, $conditions) @@ -65,7 +70,7 @@ export const initialise = (context: StoreContext) => { return } const $metadata = get(metadata) - let metadataUpdates = {} + let metadataUpdates: Record = {} for (let row of $rows) { if (!row._rev || $metadata[row._id]?.version !== row._rev) { metadataUpdates[row._id] = evaluateConditions(row, $conditions) @@ -80,15 +85,15 @@ export const initialise = (context: StoreContext) => { }) } -const TypeCoercionMap = { +const TypeCoercionMap: Partial any>> = { [FieldType.NUMBER]: parseFloat, - [FieldType.DATETIME]: val => { + [FieldType.DATETIME]: (val: string) => { if (val) { return new Date(val).toISOString() } return null }, - [FieldType.BOOLEAN]: val => { + [FieldType.BOOLEAN]: (val: string) => { if (`${val}`.toLowerCase().trim() === "true") { return true } @@ -101,8 +106,12 @@ const TypeCoercionMap = { // Evaluates an array of cell conditions against a certain row and returns the // resultant metadata -const evaluateConditions = (row, conditions) => { - let metadata = { +const evaluateConditions = (row: UIRow, conditions: UICondition[]) => { + const metadata: { + version?: string + row: Record + cell: Record + } = { version: row._rev, row: {}, cell: {}, diff --git a/packages/types/src/ui/stores/grid/condition.ts b/packages/types/src/ui/stores/grid/condition.ts new file mode 100644 index 0000000000..dfd9ee867c --- /dev/null +++ b/packages/types/src/ui/stores/grid/condition.ts @@ -0,0 +1,11 @@ +import { FieldType, SearchFilter } from "@budibase/types" + +export interface UICondition { + column: string + type: FieldType + referenceValue: string + operator: SearchFilter["operator"] + metadataKey: string + metadataValue: string + target: string +} diff --git a/packages/types/src/ui/stores/grid/index.ts b/packages/types/src/ui/stores/grid/index.ts index 82126f13aa..f419134452 100644 --- a/packages/types/src/ui/stores/grid/index.ts +++ b/packages/types/src/ui/stores/grid/index.ts @@ -1,4 +1,5 @@ export * from "./columns" +export * from "./condition" export * from "./datasource" export * from "./table" export * from "./view"