From 3c8502187fbcc8dda32fb4a46c98a66318faf4fb Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 12 Oct 2020 14:21:15 +0100 Subject: [PATCH] Fix relational data in client apps and use count for link fields in client apps --- .../standard-components/src/DataTable.svelte | 5 +++-- packages/standard-components/src/List.svelte | 3 ++- .../standard-components/src/RowDetail.svelte | 4 +++- packages/standard-components/src/fetchData.js | 16 +++++++--------- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/standard-components/src/DataTable.svelte b/packages/standard-components/src/DataTable.svelte index 7eca3ede3f..ab99f4bebe 100644 --- a/packages/standard-components/src/DataTable.svelte +++ b/packages/standard-components/src/DataTable.svelte @@ -20,6 +20,7 @@ let sort = {} let sorted = [] let schema = {} + let store = _bb.store $: cssVariables = { backgroundColor, @@ -39,7 +40,7 @@ onMount(async () => { if (!isEmpty(datasource)) { - data = await fetchData(datasource, _bb) + data = await fetchData(datasource, $store) if (data && data.length) { await fetchModel(data[0].modelId) headers = Object.keys(schema).filter(shouldDisplayField) @@ -99,7 +100,7 @@ {#if schema[header].type === 'attachment'} {:else if schema[header].type === 'link'} - {row[header]} related row(s) + {row[header].length} related row(s) {:else} {row[header] == null ? '' : row[header]} {/if} diff --git a/packages/standard-components/src/List.svelte b/packages/standard-components/src/List.svelte index e31d510fbf..410e100ce5 100644 --- a/packages/standard-components/src/List.svelte +++ b/packages/standard-components/src/List.svelte @@ -7,10 +7,11 @@ export let datasource = [] let target + let store = _bb.store onMount(async () => { if (!isEmpty(datasource)) { - const data = await fetchData(datasource, _bb) + const data = await fetchData(datasource, $store) _bb.attachChildren(target, { hydrate: false, context: data, diff --git a/packages/standard-components/src/RowDetail.svelte b/packages/standard-components/src/RowDetail.svelte index a974bc9d1a..f17c035b1e 100644 --- a/packages/standard-components/src/RowDetail.svelte +++ b/packages/standard-components/src/RowDetail.svelte @@ -52,7 +52,9 @@ const modelObj = await fetchModel(record.modelId) for (let key of Object.keys(modelObj.schema)) { if (modelObj.schema[key].type === "link") { - record[key] = Array.isArray(record[key]) ? record[key].length : 0 + record[`${key}_count`] = Array.isArray(record[key]) + ? record[key].length + : 0 } } diff --git a/packages/standard-components/src/fetchData.js b/packages/standard-components/src/fetchData.js index 2193010530..12f021029b 100644 --- a/packages/standard-components/src/fetchData.js +++ b/packages/standard-components/src/fetchData.js @@ -1,10 +1,10 @@ import api from "./api" -export default async function fetchData(datasource, _bb) { +export default async function fetchData(datasource, store) { const { type, name } = datasource if (name) { - let records + let records = [] if (type === "model") { records = await fetchModelData() } else if (type === "view") { @@ -20,7 +20,9 @@ export default async function fetchData(datasource, _bb) { records.forEach(record => { for (let key of keys) { if (model.schema[key].type === "link") { - record[key] = Array.isArray(record[key]) ? record[key].length : 0 + record[`${key}_count`] = Array.isArray(record[key]) + ? record[key].length + : 0 } } }) @@ -62,14 +64,10 @@ export default async function fetchData(datasource, _bb) { } async function fetchLinkedRecordsData() { - if ( - !_bb.store.state || - !_bb.store.state.data || - !_bb.store.state.data._id - ) { + if (!store || !store.data || !store.data._id) { return [] } - const QUERY_URL = `/api/${_bb.store.state.data.modelId}/${_bb.store.state.data._id}/enrich` + const QUERY_URL = `/api/${store.data.modelId}/${store.data._id}/enrich` const response = await api.get(QUERY_URL) const record = await response.json() return record[datasource.fieldName]