Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing grid validation stores #15238

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/frontend-core/src/components/grid/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export type Store = BaseStore &
Table.Store &
ViewV2.Store &
NonPlus.Store &
Datasource.Store & {
Datasource.Store &
Validation.Store & {
// TODO while typing the rest of stores
fetch: Writable<any>
filter: Writable<any>
Expand All @@ -76,6 +77,8 @@ export type Store = BaseStore &
dispatch: (event: string, data: any) => any
notifications: Writable<any>
schemaOverrides: Writable<any>
focusedCellId: Writable<any>
previousFocusedRowId: Writable<string>
}

export const attachStores = (context: Store): Store => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { writable, get, derived } from "svelte/store"
import { writable, get, derived, Writable, Readable } from "svelte/store"
import { Store as StoreContext } from "."
import { parseCellID } from "../lib/utils"

interface ValidationStore {
validation: Writable<Record<string, string>>
}

interface DerivedValidationStore {
validationRowLookupMap: Readable<Record<string, string[]>>
}

export type Store = ValidationStore & DerivedValidationStore

// Normally we would break out actions into the explicit "createActions"
// function, but for validation all these actions are pure so can go into
// "createStores" instead to make dependency ordering simpler
export const createStores = () => {
export const createStores = (): ValidationStore => {
const validation = writable({})

return {
validation,
}
}

export const deriveStores = context => {
export const deriveStores = (context: StoreContext): DerivedValidationStore => {
const { validation } = context

// Derive which rows have errors so that we can use that info later
const validationRowLookupMap = derived(validation, $validation => {
let map = {}
const map: Record<string, string[]> = {}
Object.entries($validation).forEach(([key, error]) => {
// Extract row ID from all errored cell IDs
if (error) {
Expand All @@ -36,10 +47,10 @@ export const deriveStores = context => {
}
}

export const createActions = context => {
export const createActions = (context: StoreContext) => {
const { validation, focusedCellId, validationRowLookupMap } = context

const setError = (cellId, error) => {
const setError = (cellId: string | undefined, error: string) => {
if (!cellId) {
return
}
Expand All @@ -49,11 +60,11 @@ export const createActions = context => {
}))
}

const rowHasErrors = rowId => {
const rowHasErrors = (rowId: string) => {
return get(validationRowLookupMap)[rowId]?.length > 0
}

const focusFirstRowError = rowId => {
const focusFirstRowError = (rowId: string) => {
const errorCells = get(validationRowLookupMap)[rowId]
const cellId = errorCells?.[0]
if (cellId) {
Expand All @@ -73,7 +84,7 @@ export const createActions = context => {
}
}

export const initialise = context => {
export const initialise = (context: StoreContext) => {
const { validation, previousFocusedRowId, validationRowLookupMap } = context

// Remove validation errors when changing rows
Expand Down
Loading