Merge pull request #15265 from Budibase/typing/stores-grid-conditions

Typing grid conditions store
This commit is contained in:
Adria Navarro 2024-12-31 12:36:58 +01:00 committed by GitHub
commit 24b6a25e2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 13 deletions

View File

@ -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<Record<string, any>>
}
interface ConditionDerivedStore {
conditions: Readable<UICondition[]>
}
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<string, any> = {}
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<string, any> = {}
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<Record<FieldType, (val: string) => 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<string, string>
cell: Record<string, any>
} = {
version: row._rev,
row: {},
cell: {},

View File

@ -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

View File

@ -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
}

View File

@ -1,4 +1,5 @@
export * from "./columns"
export * from "./condition"
export * from "./datasource"
export * from "./table"
export * from "./view"