Merge pull request #15265 from Budibase/typing/stores-grid-conditions
Typing grid conditions store
This commit is contained in:
commit
24b6a25e2e
|
@ -1,15 +1,31 @@
|
||||||
import { writable, get } from "svelte/store"
|
import { writable, get, Writable, Readable } from "svelte/store"
|
||||||
import { derivedMemo, QueryUtils } from "../../../utils"
|
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({})
|
const metadata = writable({})
|
||||||
return {
|
return {
|
||||||
metadata,
|
metadata,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deriveStores = context => {
|
export const deriveStores = (context: StoreContext): ConditionDerivedStore => {
|
||||||
const { columns } = context
|
const { columns } = context
|
||||||
|
|
||||||
// Derive and memoize the cell conditions present in our columns so that we
|
// 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
|
const { metadata, conditions, rows } = context
|
||||||
|
|
||||||
// Recompute all metadata if conditions change
|
// Recompute all metadata if conditions change
|
||||||
conditions.subscribe($conditions => {
|
conditions.subscribe($conditions => {
|
||||||
let newMetadata = {}
|
let newMetadata: Record<string, any> = {}
|
||||||
if ($conditions?.length) {
|
if ($conditions?.length) {
|
||||||
for (let row of get(rows)) {
|
for (let row of get(rows)) {
|
||||||
newMetadata[row._id] = evaluateConditions(row, $conditions)
|
newMetadata[row._id] = evaluateConditions(row, $conditions)
|
||||||
|
@ -54,7 +70,7 @@ export const initialise = context => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const $metadata = get(metadata)
|
const $metadata = get(metadata)
|
||||||
let metadataUpdates = {}
|
let metadataUpdates: Record<string, any> = {}
|
||||||
for (let row of $rows) {
|
for (let row of $rows) {
|
||||||
if (!row._rev || $metadata[row._id]?.version !== row._rev) {
|
if (!row._rev || $metadata[row._id]?.version !== row._rev) {
|
||||||
metadataUpdates[row._id] = evaluateConditions(row, $conditions)
|
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.NUMBER]: parseFloat,
|
||||||
[FieldType.DATETIME]: val => {
|
[FieldType.DATETIME]: (val: string) => {
|
||||||
if (val) {
|
if (val) {
|
||||||
return new Date(val).toISOString()
|
return new Date(val).toISOString()
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
},
|
},
|
||||||
[FieldType.BOOLEAN]: val => {
|
[FieldType.BOOLEAN]: (val: string) => {
|
||||||
if (`${val}`.toLowerCase().trim() === "true") {
|
if (`${val}`.toLowerCase().trim() === "true") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -90,8 +106,12 @@ const TypeCoercionMap = {
|
||||||
|
|
||||||
// Evaluates an array of cell conditions against a certain row and returns the
|
// Evaluates an array of cell conditions against a certain row and returns the
|
||||||
// resultant metadata
|
// resultant metadata
|
||||||
const evaluateConditions = (row, conditions) => {
|
const evaluateConditions = (row: UIRow, conditions: UICondition[]) => {
|
||||||
let metadata = {
|
const metadata: {
|
||||||
|
version?: string
|
||||||
|
row: Record<string, string>
|
||||||
|
cell: Record<string, any>
|
||||||
|
} = {
|
||||||
version: row._rev,
|
version: row._rev,
|
||||||
row: {},
|
row: {},
|
||||||
cell: {},
|
cell: {},
|
|
@ -105,7 +105,8 @@ export type Store = BaseStore &
|
||||||
} & Rows.Store &
|
} & Rows.Store &
|
||||||
Reorder.Store &
|
Reorder.Store &
|
||||||
Resize.Store &
|
Resize.Store &
|
||||||
Config.Store
|
Config.Store &
|
||||||
|
Conditions.Store
|
||||||
|
|
||||||
export const attachStores = (context: Store): Store => {
|
export const attachStores = (context: Store): Store => {
|
||||||
// Atomic store creation
|
// Atomic store creation
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
export * from "./columns"
|
export * from "./columns"
|
||||||
|
export * from "./condition"
|
||||||
export * from "./datasource"
|
export * from "./datasource"
|
||||||
export * from "./table"
|
export * from "./table"
|
||||||
export * from "./view"
|
export * from "./view"
|
||||||
|
|
Loading…
Reference in New Issue