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"