From 88c4d0cd203c0b1f8788b2898810c381c5c1af1f Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 5 Oct 2023 08:23:01 +0100 Subject: [PATCH] Update grids to work with all datasources --- .../src/components/grid/stores/config.js | 2 + .../src/components/grid/stores/datasource.js | 12 +++--- .../{query.js => datasources/nonPlus.js} | 38 ++++++++++++------- .../grid/stores/{ => datasources}/table.js | 0 .../grid/stores/{ => datasources}/viewV2.js | 0 .../src/components/grid/stores/index.js | 8 ++-- 6 files changed, 38 insertions(+), 22 deletions(-) rename packages/frontend-core/src/components/grid/stores/{query.js => datasources/nonPlus.js} (64%) rename packages/frontend-core/src/components/grid/stores/{ => datasources}/table.js (100%) rename packages/frontend-core/src/components/grid/stores/{ => datasources}/viewV2.js (100%) diff --git a/packages/frontend-core/src/components/grid/stores/config.js b/packages/frontend-core/src/components/grid/stores/config.js index 0efdc2104e..6da6ebf11e 100644 --- a/packages/frontend-core/src/components/grid/stores/config.js +++ b/packages/frontend-core/src/components/grid/stores/config.js @@ -55,6 +55,8 @@ export const deriveStores = context => { config.canEditRows = false config.canDeleteRows = false config.canExpandRows = false + config.canSaveSchema = false + config.canEditColumns = false } return config diff --git a/packages/frontend-core/src/components/grid/stores/datasource.js b/packages/frontend-core/src/components/grid/stores/datasource.js index 0d70424550..6a10cb8b9b 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.js +++ b/packages/frontend-core/src/components/grid/stores/datasource.js @@ -75,21 +75,23 @@ export const createActions = context => { dispatch, table, viewV2, - query, + nonPlus, } = context // Gets the appropriate API for the configured datasource type const getAPI = () => { const $datasource = get(datasource) - switch ($datasource?.type) { + const type = $datasource?.type + if (!type) { + return null + } + switch (type) { case "table": return table case "viewV2": return viewV2 - case "query": - return query default: - return null + return nonPlus } } diff --git a/packages/frontend-core/src/components/grid/stores/query.js b/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.js similarity index 64% rename from packages/frontend-core/src/components/grid/stores/query.js rename to packages/frontend-core/src/components/grid/stores/datasources/nonPlus.js index 6ac0547d27..ea024643aa 100644 --- a/packages/frontend-core/src/components/grid/stores/query.js +++ b/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.js @@ -1,26 +1,32 @@ import { get } from "svelte/store" export const createActions = context => { - const { API, columns, stickyColumn } = context + const { columns, stickyColumn, table, viewV2 } = context - const saveDefinition = async newDefinition => { - await API.saveQuery(newDefinition) + const saveDefinition = async () => { + throw "This datasource does not support updating the definition" } const saveRow = async () => { - throw "Rows cannot be updated through queries" + throw "This datasource does not support saving rows" } const deleteRows = async () => { - throw "Rows cannot be deleted through queries" + throw "This datasource does not support deleting rows" } const getRow = () => { - throw "Queries don't support fetching individual rows" + throw "This datasource does not support fetching individual rows" } const isDatasourceValid = datasource => { - return datasource?.type === "query" && datasource?._id + // There are many different types and shapes of datasource, so we only + // check that we aren't null + return ( + !table.actions.isDatasourceValid(datasource) && + !viewV2.actions.isDatasourceValid(datasource) && + datasource?.type != null + ) } const canUseColumn = name => { @@ -30,7 +36,7 @@ export const createActions = context => { } return { - query: { + nonPlus: { actions: { saveDefinition, addRow: saveRow, @@ -44,18 +50,22 @@ export const createActions = context => { } } +// Small util to compare datasource definitions +const isSameDatasource = (a, b) => { + return JSON.stringify(a) === JSON.stringify(b) +} + export const initialise = context => { const { datasource, sort, filter, - query, + nonPlus, initialFilter, initialSortColumn, initialSortOrder, fetch, } = context - // Keep a list of subscriptions so that we can clear them when the datasource // config changes let unsubscribers = [] @@ -65,7 +75,7 @@ export const initialise = context => { // Clear previous subscriptions unsubscribers?.forEach(unsubscribe => unsubscribe()) unsubscribers = [] - if (!query.actions.isDatasourceValid($datasource)) { + if (!nonPlus.actions.isDatasourceValid($datasource)) { return } @@ -81,7 +91,8 @@ export const initialise = context => { filter.subscribe($filter => { // Ensure we're updating the correct fetch const $fetch = get(fetch) - if ($fetch?.options?.datasource?._id !== $datasource._id) { + if (!isSameDatasource($fetch?.options?.datasource, $datasource)) { + console.log("skip, different ds") return } $fetch.update({ @@ -95,7 +106,8 @@ export const initialise = context => { sort.subscribe($sort => { // Ensure we're updating the correct fetch const $fetch = get(fetch) - if ($fetch?.options?.datasource?._id !== $datasource._id) { + if (!isSameDatasource($fetch?.options?.datasource, $datasource)) { + console.log("skip, different ds") return } $fetch.update({ diff --git a/packages/frontend-core/src/components/grid/stores/table.js b/packages/frontend-core/src/components/grid/stores/datasources/table.js similarity index 100% rename from packages/frontend-core/src/components/grid/stores/table.js rename to packages/frontend-core/src/components/grid/stores/datasources/table.js diff --git a/packages/frontend-core/src/components/grid/stores/viewV2.js b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js similarity index 100% rename from packages/frontend-core/src/components/grid/stores/viewV2.js rename to packages/frontend-core/src/components/grid/stores/datasources/viewV2.js diff --git a/packages/frontend-core/src/components/grid/stores/index.js b/packages/frontend-core/src/components/grid/stores/index.js index 5a46227b49..10fe932aab 100644 --- a/packages/frontend-core/src/components/grid/stores/index.js +++ b/packages/frontend-core/src/components/grid/stores/index.js @@ -15,10 +15,10 @@ import * as Config from "./config" import * as Sort from "./sort" import * as Filter from "./filter" import * as Notifications from "./notifications" -import * as Table from "./table" -import * as ViewV2 from "./viewV2" -import * as Query from "./query" import * as Datasource from "./datasource" +import * as Table from "./datasources/table" +import * as ViewV2 from "./datasources/viewV2" +import * as NonPlus from "./datasources/nonPlus" const DependencyOrderedStores = [ Sort, @@ -27,7 +27,7 @@ const DependencyOrderedStores = [ Scroll, Table, ViewV2, - Query, + NonPlus, Datasource, Columns, Rows,