Merge pull request #15264 from Budibase/typing/stores-grid-config
Typing grid config store
This commit is contained in:
commit
218aea393d
|
@ -26,11 +26,13 @@
|
||||||
const getSchema = (asset, datasource) => {
|
const getSchema = (asset, datasource) => {
|
||||||
const schema = getSchemaForDatasource(asset, datasource).schema
|
const schema = getSchemaForDatasource(asset, datasource).schema
|
||||||
|
|
||||||
|
if (!schema) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Don't show ID and rev in tables
|
// Don't show ID and rev in tables
|
||||||
if (schema) {
|
|
||||||
delete schema._id
|
delete schema._id
|
||||||
delete schema._rev
|
delete schema._rev
|
||||||
}
|
|
||||||
|
|
||||||
const result = enrichSchemaWithRelColumns(schema)
|
const result = enrichSchemaWithRelColumns(schema)
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -47,7 +47,6 @@
|
||||||
export let buttonsCollapsedText = null
|
export let buttonsCollapsedText = null
|
||||||
export let darkMode = false
|
export let darkMode = false
|
||||||
export let isCloud = null
|
export let isCloud = null
|
||||||
export let rowConditions = null
|
|
||||||
export let aiEnabled = false
|
export let aiEnabled = false
|
||||||
|
|
||||||
// Unique identifier for DOM nodes inside this instance
|
// Unique identifier for DOM nodes inside this instance
|
||||||
|
@ -106,7 +105,6 @@
|
||||||
darkMode,
|
darkMode,
|
||||||
isCloud,
|
isCloud,
|
||||||
aiEnabled,
|
aiEnabled,
|
||||||
rowConditions,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Derive min height and make available in context
|
// Derive min height and make available in context
|
||||||
|
|
|
@ -156,11 +156,11 @@ export const initialise = (context: StoreContext) => {
|
||||||
|
|
||||||
// Merge new schema fields with existing schema in order to preserve widths
|
// Merge new schema fields with existing schema in order to preserve widths
|
||||||
const processColumns = ($enrichedSchema: any) => {
|
const processColumns = ($enrichedSchema: any) => {
|
||||||
if (!$enrichedSchema) {
|
const $definition = get(definition)
|
||||||
|
if (!$enrichedSchema || !$definition) {
|
||||||
columns.set([])
|
columns.set([])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const $definition = get(definition)
|
|
||||||
const $columns = get(columns)
|
const $columns = get(columns)
|
||||||
const $displayColumn = get(displayColumn)
|
const $displayColumn = get(displayColumn)
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,22 @@
|
||||||
import { derivedMemo } from "../../../utils"
|
import { derivedMemo } from "../../../utils"
|
||||||
import { derived } from "svelte/store"
|
import { derived, Readable } from "svelte/store"
|
||||||
import { ViewV2Type } from "@budibase/types"
|
import { ViewV2Type } from "@budibase/types"
|
||||||
|
import { BaseStoreProps, Store as StoreContext } from "."
|
||||||
|
|
||||||
export const createStores = context => {
|
type ConfigStore = {
|
||||||
|
[key in keyof BaseStoreProps]: Readable<BaseStoreProps[key]>
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ConfigDerivedStore {
|
||||||
|
config: Readable<BaseStoreProps>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Store = ConfigStore & ConfigDerivedStore
|
||||||
|
|
||||||
|
export const createStores = (context: StoreContext): ConfigStore => {
|
||||||
const { props } = context
|
const { props } = context
|
||||||
const getProp = prop => derivedMemo(props, $props => $props[prop])
|
const getProp = <T extends keyof BaseStoreProps>(prop: T) =>
|
||||||
|
derivedMemo(props, $props => $props[prop])
|
||||||
|
|
||||||
// Derive and memoize some props so that we can react to them in isolation
|
// Derive and memoize some props so that we can react to them in isolation
|
||||||
const datasource = getProp("datasource")
|
const datasource = getProp("datasource")
|
||||||
|
@ -15,7 +27,6 @@ export const createStores = context => {
|
||||||
const schemaOverrides = getProp("schemaOverrides")
|
const schemaOverrides = getProp("schemaOverrides")
|
||||||
const notifySuccess = getProp("notifySuccess")
|
const notifySuccess = getProp("notifySuccess")
|
||||||
const notifyError = getProp("notifyError")
|
const notifyError = getProp("notifyError")
|
||||||
const rowConditions = getProp("rowConditions")
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
datasource,
|
datasource,
|
||||||
|
@ -26,11 +37,10 @@ export const createStores = context => {
|
||||||
schemaOverrides,
|
schemaOverrides,
|
||||||
notifySuccess,
|
notifySuccess,
|
||||||
notifyError,
|
notifyError,
|
||||||
rowConditions,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deriveStores = context => {
|
export const deriveStores = (context: StoreContext): ConfigDerivedStore => {
|
||||||
const { props, definition, hasNonAutoColumn } = context
|
const { props, definition, hasNonAutoColumn } = context
|
||||||
|
|
||||||
// Derive features
|
// Derive features
|
|
@ -12,11 +12,11 @@ import {
|
||||||
UpdateViewRequest,
|
UpdateViewRequest,
|
||||||
ViewV2Type,
|
ViewV2Type,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { Store as StoreContext } from "."
|
import { Store as StoreContext, BaseStoreProps } from "."
|
||||||
import { DatasourceActions } from "./datasources"
|
import { DatasourceActions } from "./datasources"
|
||||||
|
|
||||||
interface DatasourceStore {
|
interface DatasourceStore {
|
||||||
definition: Writable<UIDatasource>
|
definition: Writable<UIDatasource | null>
|
||||||
schemaMutations: Writable<Record<string, UIFieldMutation>>
|
schemaMutations: Writable<Record<string, UIFieldMutation>>
|
||||||
subSchemaMutations: Writable<Record<string, Record<string, UIFieldMutation>>>
|
subSchemaMutations: Writable<Record<string, Record<string, UIFieldMutation>>>
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ interface DerivedDatasourceStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ActionDatasourceStore {
|
interface ActionDatasourceStore {
|
||||||
datasource: DatasourceStore["definition"] & {
|
datasource: BaseStoreProps["datasource"] & {
|
||||||
actions: DatasourceActions & {
|
actions: DatasourceActions & {
|
||||||
refreshDefinition: () => Promise<void>
|
refreshDefinition: () => Promise<void>
|
||||||
changePrimaryDisplay: (column: string) => Promise<void>
|
changePrimaryDisplay: (column: string) => Promise<void>
|
||||||
|
@ -218,7 +218,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => {
|
||||||
|
|
||||||
// Updates the datasources primary display column
|
// Updates the datasources primary display column
|
||||||
const changePrimaryDisplay = async (column: string) => {
|
const changePrimaryDisplay = async (column: string) => {
|
||||||
let newDefinition = cloneDeep(get(definition))
|
let newDefinition = cloneDeep(get(definition)!)
|
||||||
|
|
||||||
// Update primary display
|
// Update primary display
|
||||||
newDefinition.primaryDisplay = column
|
newDefinition.primaryDisplay = column
|
||||||
|
|
|
@ -91,7 +91,7 @@ export const initialise = (context: StoreContext) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wipe state
|
// Wipe state
|
||||||
filter.set(get(initialFilter))
|
filter.set(get(initialFilter) ?? undefined)
|
||||||
inlineFilters.set([])
|
inlineFilters.set([])
|
||||||
sort.set({
|
sort.set({
|
||||||
column: get(initialSortColumn),
|
column: get(initialSortColumn),
|
||||||
|
|
|
@ -108,7 +108,7 @@ export const initialise = (context: StoreContext) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wipe state
|
// Wipe state
|
||||||
filter.set(get(initialFilter))
|
filter.set(get(initialFilter) ?? undefined)
|
||||||
inlineFilters.set([])
|
inlineFilters.set([])
|
||||||
sort.set({
|
sort.set({
|
||||||
column: get(initialSortColumn),
|
column: get(initialSortColumn),
|
||||||
|
|
|
@ -121,7 +121,7 @@ export const initialise = (context: StoreContext) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset state for new view
|
// Reset state for new view
|
||||||
filter.set(get(initialFilter))
|
filter.set(get(initialFilter) ?? undefined)
|
||||||
inlineFilters.set([])
|
inlineFilters.set([])
|
||||||
sort.set({
|
sort.set({
|
||||||
column: get(initialSortColumn),
|
column: get(initialSortColumn),
|
||||||
|
|
|
@ -26,7 +26,7 @@ export const createStores = (context: StoreContext): FilterStore => {
|
||||||
const { props } = context
|
const { props } = context
|
||||||
|
|
||||||
// Initialise to default props
|
// Initialise to default props
|
||||||
const filter = memo(get(props).initialFilter)
|
const filter = memo(get(props).initialFilter ?? undefined)
|
||||||
const inlineFilters = memo([])
|
const inlineFilters = memo([])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -120,5 +120,7 @@ export const initialise = (context: StoreContext) => {
|
||||||
const { filter, initialFilter } = context
|
const { filter, initialFilter } = context
|
||||||
|
|
||||||
// Reset filter when initial filter prop changes
|
// Reset filter when initial filter prop changes
|
||||||
initialFilter.subscribe(filter.set)
|
initialFilter.subscribe($initialFilter =>
|
||||||
|
filter.set($initialFilter ?? undefined)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import * as ViewV2 from "./datasources/viewV2"
|
||||||
import * as NonPlus from "./datasources/nonPlus"
|
import * as NonPlus from "./datasources/nonPlus"
|
||||||
import * as Cache from "./cache"
|
import * as Cache from "./cache"
|
||||||
import * as Conditions from "./conditions"
|
import * as Conditions from "./conditions"
|
||||||
|
import { SortOrder, UIDatasource, UISearchFilter } from "@budibase/types"
|
||||||
|
|
||||||
const DependencyOrderedStores = [
|
const DependencyOrderedStores = [
|
||||||
Sort,
|
Sort,
|
||||||
|
@ -51,8 +52,33 @@ const DependencyOrderedStores = [
|
||||||
Cache,
|
Cache,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
export interface BaseStoreProps {
|
||||||
|
datasource: UIDatasource
|
||||||
|
initialSortColumn: string | null
|
||||||
|
initialSortOrder: SortOrder | null
|
||||||
|
initialFilter: UISearchFilter | null
|
||||||
|
fixedRowHeight: number | null
|
||||||
|
schemaOverrides: Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
displayName?: string
|
||||||
|
disabled?: boolean
|
||||||
|
}
|
||||||
|
> | null
|
||||||
|
notifySuccess: (message: string) => void
|
||||||
|
notifyError: (message: string) => void
|
||||||
|
canAddRows?: boolean
|
||||||
|
canEditRows?: boolean
|
||||||
|
canDeleteRows?: boolean
|
||||||
|
canEditColumns?: boolean
|
||||||
|
canExpandRows?: boolean
|
||||||
|
canSaveSchema?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export interface BaseStore {
|
export interface BaseStore {
|
||||||
API: APIClient
|
API: APIClient
|
||||||
|
gridID: string
|
||||||
|
props: Writable<BaseStoreProps>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Store = BaseStore &
|
export type Store = BaseStore &
|
||||||
|
@ -70,23 +96,16 @@ export type Store = BaseStore &
|
||||||
Scroll.Store & {
|
Scroll.Store & {
|
||||||
// TODO while typing the rest of stores
|
// TODO while typing the rest of stores
|
||||||
sort: Writable<any>
|
sort: Writable<any>
|
||||||
initialFilter: Writable<any>
|
|
||||||
initialSortColumn: Writable<any>
|
|
||||||
initialSortOrder: Writable<any>
|
|
||||||
subscribe: any
|
subscribe: any
|
||||||
config: Writable<any>
|
|
||||||
dispatch: (event: string, data: any) => any
|
dispatch: (event: string, data: any) => any
|
||||||
notifications: Writable<any>
|
notifications: Writable<any>
|
||||||
schemaOverrides: Writable<any>
|
|
||||||
gridID: string
|
|
||||||
props: Writable<any>
|
|
||||||
width: Writable<number>
|
width: Writable<number>
|
||||||
fixedRowHeight: Writable<number>
|
|
||||||
bounds: Readable<any>
|
bounds: Readable<any>
|
||||||
height: Readable<number>
|
height: Readable<number>
|
||||||
} & Rows.Store &
|
} & Rows.Store &
|
||||||
Reorder.Store &
|
Reorder.Store &
|
||||||
Resize.Store
|
Resize.Store &
|
||||||
|
Config.Store
|
||||||
|
|
||||||
export const attachStores = (context: Store): Store => {
|
export const attachStores = (context: Store): Store => {
|
||||||
// Atomic store creation
|
// Atomic store creation
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
import { Readable, Writable } from "svelte/store"
|
||||||
|
|
||||||
|
declare module "./memo" {
|
||||||
|
export function memo<T>(value: T): Writable<T>
|
||||||
|
|
||||||
|
export function derivedMemo<TStore, TResult>(
|
||||||
|
store: Readable<TStore>,
|
||||||
|
derivation: (store: TStore) => TResult
|
||||||
|
): Readable<TResult>
|
||||||
|
}
|
|
@ -46,10 +46,7 @@ const columnTypeManyParser = {
|
||||||
|
|
||||||
export function enrichSchemaWithRelColumns(
|
export function enrichSchemaWithRelColumns(
|
||||||
schema: Record<string, UIFieldSchema>
|
schema: Record<string, UIFieldSchema>
|
||||||
): Record<string, UIFieldSchema> | undefined {
|
): Record<string, UIFieldSchema> {
|
||||||
if (!schema) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const result = Object.keys(schema).reduce<Record<string, UIFieldSchema>>(
|
const result = Object.keys(schema).reduce<Record<string, UIFieldSchema>>(
|
||||||
(result, fieldName) => {
|
(result, fieldName) => {
|
||||||
const field = schema[fieldName]
|
const field = schema[fieldName]
|
||||||
|
|
|
@ -27,6 +27,7 @@ export type UIFieldSchema = FieldSchema &
|
||||||
related?: { field: string; subField: string }
|
related?: { field: string; subField: string }
|
||||||
columns?: Record<string, UIRelationSchemaField>
|
columns?: Record<string, UIRelationSchemaField>
|
||||||
cellRenderType?: string
|
cellRenderType?: string
|
||||||
|
disabled?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UIRelationSchemaField extends RelationSchemaField {
|
interface UIRelationSchemaField extends RelationSchemaField {
|
||||||
|
|
Loading…
Reference in New Issue