From 3dcd9d32e2d97e6f7db31a6ea74ed4d7f45b263d Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 19 Nov 2020 13:50:20 +0000 Subject: [PATCH] Fix fetching of relationship data in lists --- .../userInterface/AppPreview/iframeTemplate.js | 2 +- packages/client/src/api/datasources.js | 10 +++++++--- packages/client/src/api/relationships.js | 2 +- packages/client/src/components/Router.svelte | 8 ++------ packages/client/src/utils/enrichDataBinding.js | 1 + packages/standard-components/src/List.svelte | 3 ++- .../standard-components/src/charts/LineChart.svelte | 3 ++- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js index 884b6a5741..7b4e0d6b33 100644 --- a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js +++ b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js @@ -55,7 +55,7 @@ export default ` window["##BUDIBASE_FRONTEND_DEFINITION##"] = data.frontendDefinition; if (window.loadBudibase) { - loadBudibase({ window, localStorage }) + loadBudibase() } } let styles diff --git a/packages/client/src/api/datasources.js b/packages/client/src/api/datasources.js index 4c614b0085..66890b230b 100644 --- a/packages/client/src/api/datasources.js +++ b/packages/client/src/api/datasources.js @@ -6,20 +6,24 @@ import { enrichRows } from "./rows" /** * Fetches all rows for a particular Budibase data source. */ -export const fetchDatasource = async datasource => { +export const fetchDatasource = async (datasource, context) => { if (!datasource || !datasource.type) { return [] } // Fetch all rows in data source - const { type, tableId } = datasource + const { type, tableId, fieldName } = datasource let rows = [] if (type === "table") { rows = await fetchTableData(tableId) } else if (type === "view") { rows = await fetchViewData(datasource) } else if (type === "link") { - rows = await fetchRelationshipData(tableId) + rows = await fetchRelationshipData({ + tableId: context?.row?.tableId, + fieldName, + rowId: context?.row?._id, + }) } // Enrich rows diff --git a/packages/client/src/api/relationships.js b/packages/client/src/api/relationships.js index d988220f14..af2331291c 100644 --- a/packages/client/src/api/relationships.js +++ b/packages/client/src/api/relationships.js @@ -5,7 +5,7 @@ import { enrichRows } from "./rows" * Fetches related rows for a certain field of a certain row. */ export const fetchRelationshipData = async ({ tableId, rowId, fieldName }) => { - if (!tableId || !rowId) { + if (!tableId || !rowId || !fieldName) { return [] } const response = await api.get({ url: `/api/${tableId}/${rowId}/enrich` }) diff --git a/packages/client/src/components/Router.svelte b/packages/client/src/components/Router.svelte index f0db3bb2e0..99f4a5722a 100644 --- a/packages/client/src/components/Router.svelte +++ b/packages/client/src/components/Router.svelte @@ -8,11 +8,7 @@ export let styles let routes - onMount(() => { - initialiseRouter() - }) - - const initialiseRouter = async () => { + onMount(async () => { await routeStore.actions.fetchRoutes() await screenStore.actions.fetchScreens() routes = {} @@ -22,7 +18,7 @@ // Add catch-all route so that we serve the Screen component always routes["*"] = Screen - } + }) function onRouteLoading({ detail }) { routeStore.actions.setActiveRoute(detail.route) diff --git a/packages/client/src/utils/enrichDataBinding.js b/packages/client/src/utils/enrichDataBinding.js index bd058fbf17..0c4fd16cf3 100644 --- a/packages/client/src/utils/enrichDataBinding.js +++ b/packages/client/src/utils/enrichDataBinding.js @@ -28,5 +28,6 @@ export default (input, context) => { if (!looksLikeMustache.test(input)) { return input } + console.log(input) return mustache.render(input, context) } diff --git a/packages/standard-components/src/List.svelte b/packages/standard-components/src/List.svelte index e68d39e0f0..343915ccfb 100644 --- a/packages/standard-components/src/List.svelte +++ b/packages/standard-components/src/List.svelte @@ -4,6 +4,7 @@ import DataProvider from "./DataProvider.svelte" const { API, styleable } = getContext("app") + const dataContext = getContext("data") export let datasource = [] export let styles @@ -12,7 +13,7 @@ onMount(async () => { if (!isEmpty(datasource)) { - rows = await API.fetchDatasource(datasource) + rows = await API.fetchDatasource(datasource, $dataContext) } }) diff --git a/packages/standard-components/src/charts/LineChart.svelte b/packages/standard-components/src/charts/LineChart.svelte index f5c37df50b..4e91444b24 100644 --- a/packages/standard-components/src/charts/LineChart.svelte +++ b/packages/standard-components/src/charts/LineChart.svelte @@ -5,6 +5,7 @@ import { isEmpty } from "lodash/fp" const { API } = getContext("app") + const dataContext = getContext("data") // Common props export let title @@ -41,7 +42,7 @@ // Fetch, filter and sort data const schema = (await API.fetchTableDefinition(datasource.tableId)).schema - const result = await API.fetchDatasource(datasource) + const result = await API.fetchDatasource(datasource, $dataContext) const reducer = row => (valid, column) => valid && row[column] != null const hasAllColumns = row => allCols.reduce(reducer(row), true) const data = result