diff --git a/packages/frontend-core/src/components/grid/stores/conditions.js b/packages/frontend-core/src/components/grid/stores/conditions.ts similarity index 77% rename from packages/frontend-core/src/components/grid/stores/conditions.js rename to packages/frontend-core/src/components/grid/stores/conditions.ts index 3800f21df1..cf47456b44 100644 --- a/packages/frontend-core/src/components/grid/stores/conditions.js +++ b/packages/frontend-core/src/components/grid/stores/conditions.ts @@ -1,15 +1,31 @@ -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 { + FieldType, + EmptyFilterOption, + UIRow, + UICondition, +} 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,12 +49,12 @@ export const deriveStores = context => { } } -export const initialise = context => { +export const initialise = (context: StoreContext) => { const { metadata, conditions, rows } = context // 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) @@ -54,7 +70,7 @@ export const initialise = context => { 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) @@ -69,15 +85,15 @@ export const initialise = context => { }) } -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 } @@ -90,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/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 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"