diff --git a/packages/frontend-core/src/components/grid/stores/datasources/table.ts b/packages/frontend-core/src/components/grid/stores/datasources/table.ts index 8a2dea1a70..2e947c586e 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/table.ts +++ b/packages/frontend-core/src/components/grid/stores/datasources/table.ts @@ -49,7 +49,7 @@ export const createActions = (context: StoreContext): TableActions => { return datasource?.type === "table" && !!datasource?.tableId } - const getRow = async (id: string) => { + const getRow = async (id: any) => { const res = await API.searchTable(get(datasource).tableId, { limit: 1, query: { diff --git a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts similarity index 81% rename from packages/frontend-core/src/components/grid/stores/datasources/viewV2.js rename to packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts index b9f4851a60..a24dff1f23 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js +++ b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts @@ -1,16 +1,42 @@ import { get } from "svelte/store" -import { SortOrder } from "@budibase/types" +import { + Row, + SaveRowRequest, + SortOrder, + UpdateViewRequest, +} from "@budibase/types" +import { Store as StoreContext } from ".." const SuppressErrors = true -export const createActions = context => { +interface ViewActions { + viewV2: { + actions: { + saveDefinition: (newDefinition: UpdateViewRequest) => Promise + addRow: (row: SaveRowRequest) => Promise + updateRow: (row: SaveRowRequest) => Promise + deleteRows: (rows: (string | Row)[]) => Promise + getRow: (id: string) => Promise + isDatasourceValid: (datasource: { + type: string + id: string + tableId: string + }) => boolean + canUseColumn: (name: string) => boolean + } + } +} + +export type Store = ViewActions + +export const createActions = (context: StoreContext): ViewActions => { const { API, datasource, columns } = context - const saveDefinition = async newDefinition => { + const saveDefinition = async (newDefinition: UpdateViewRequest) => { await API.viewV2.update(newDefinition) } - const saveRow = async row => { + const saveRow = async (row: SaveRowRequest) => { const $datasource = get(datasource) row = { ...row, @@ -23,11 +49,11 @@ export const createActions = context => { } } - const deleteRows = async rows => { + const deleteRows = async (rows: (string | Row)[]) => { await API.deleteRows(get(datasource).id, rows) } - const getRow = async id => { + const getRow = async (id: string) => { const res = await API.viewV2.fetch(get(datasource).id, { limit: 1, query: { @@ -40,13 +66,17 @@ export const createActions = context => { return res?.rows?.[0] } - const isDatasourceValid = datasource => { + const isDatasourceValid = (datasource: { + type: string + id: string + tableId: string + }) => { return ( - datasource?.type === "viewV2" && datasource?.id && datasource?.tableId + datasource?.type === "viewV2" && !!datasource?.id && !!datasource?.tableId ) } - const canUseColumn = name => { + const canUseColumn = (name: string) => { return get(columns).some(col => col.name === name && col.visible) } @@ -65,7 +95,7 @@ export const createActions = context => { } } -export const initialise = context => { +export const initialise = (context: StoreContext) => { const { definition, datasource, diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index 677d8d3f83..c8f26fa90a 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -57,7 +57,8 @@ export interface BaseStore { export type Store = BaseStore & Columns.Store & - Table.Store & { + Table.Store & + ViewV2.Store & { // TODO while typing the rest of stores datasource: Writable & { actions: any } definition: Writable @@ -70,6 +71,9 @@ export type Store = BaseStore & initialFilter: Writable initialSortColumn: Writable initialSortOrder: Writable + rows: Writable & { actions: any } + subscribe: any + config: Writable } export const attachStores = (context: Store): Store => {