queries working with repeater

This commit is contained in:
Martin McKeaveney 2021-01-18 14:41:59 +00:00
parent 7ed062bf1e
commit b5c4bde662
10 changed files with 52 additions and 7 deletions

View File

@ -23,13 +23,20 @@ import { cloneDeep, difference } from "lodash/fp"
* @param {fetchBindablePropertiesParameter} param * @param {fetchBindablePropertiesParameter} param
* @returns {Array.<BindableProperty>} * @returns {Array.<BindableProperty>}
*/ */
export default function({ componentInstanceId, screen, components, tables }) { export default function({
componentInstanceId,
screen,
components,
tables,
queries,
}) {
const result = walk({ const result = walk({
// cloning so we are free to mutate props (e.g. by adding _contexts) // cloning so we are free to mutate props (e.g. by adding _contexts)
instance: cloneDeep(screen.props), instance: cloneDeep(screen.props),
targetId: componentInstanceId, targetId: componentInstanceId,
components, components,
tables, tables,
queries,
}) })
return [ return [
@ -37,6 +44,9 @@ export default function({ componentInstanceId, screen, components, tables }) {
.filter(isInstanceInSharedContext(result)) .filter(isInstanceInSharedContext(result))
.map(componentInstanceToBindable), .map(componentInstanceToBindable),
...(result.target?._contexts.map(contextToBindables(tables)).flat() ?? []), ...(result.target?._contexts.map(contextToBindables(tables)).flat() ?? []),
...(result.target?._contexts
.map(context => queriesToBindables(queries, context))
.flat() ?? []),
] ]
} }
@ -61,9 +71,36 @@ const componentInstanceToBindable = i => {
} }
} }
const queriesToBindables = (queries, context) => {
let queryId = context.table._id
const query = queries.find(query => query._id === queryId)
let schema = query?.schema
// Avoid crashing whenever no data source has been selected
if (!schema) {
return []
}
const queryBindings = Object.entries(schema).map(([key, value]) => ({
type: "context",
fieldSchema: value,
instance: context.instance,
// how the binding expression persists, and is used in the app at runtime
runtimeBinding: `${context.instance._id}.${key}`,
// how the binding expressions looks to the user of the builder
readableBinding: `${context.instance._instanceName}.${query.name}.${key}`,
// table / view info
table: context.table,
}))
return queryBindings
}
const contextToBindables = tables => context => { const contextToBindables = tables => context => {
const tableId = context.table?.tableId ?? context.table let tableId = context.table?.tableId ?? context.table
const table = tables.find(table => table._id === tableId)
const table = tables.find(table => table._id === tableId || context.table._id)
let schema = let schema =
context.table?.type === "view" context.table?.type === "view"
? table?.views?.[context.table.name]?.schema ? table?.views?.[context.table.name]?.schema
@ -152,8 +189,8 @@ const walk = ({ instance, targetId, components, tables, result }) => {
const currentContexts = [...result.currentContexts] const currentContexts = [...result.currentContexts]
for (let child of instance._children || []) { for (let child of instance._children || []) {
// attaching _contexts of components, for eas comparison later // attaching _contexts of components, for easy comparison later
// these have been deep cloned above, so shouln't modify the // these have been deep cloned above, so shouldn't modify the
// original component instances // original component instances
child._contexts = currentContexts child._contexts = currentContexts
walk({ instance: child, targetId, components, tables, result }) walk({ instance: child, targetId, components, tables, result })

View File

@ -11,6 +11,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries,
}) })
const tableFields = tableId => { const tableFields = tableId => {

View File

@ -12,6 +12,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries,
}) })
$: idFields = bindableProperties.filter( $: idFields = bindableProperties.filter(

View File

@ -15,6 +15,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries
}).map(property => ({ }).map(property => ({
...property, ...property,
category: property.type === "instance" ? "Component" : "Table", category: property.type === "instance" ? "Component" : "Table",

View File

@ -44,6 +44,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries,
}) })
const addField = () => { const addField = () => {
@ -114,8 +115,7 @@
<Spacer small /> <Spacer small />
<TextButton text small blue on:click={addField}> <TextButton text small blue on:click={addField}>
Add Add {fieldLabel}
{fieldLabel}
<div style="height: 20px; width: 20px;"> <div style="height: 20px; width: 20px;">
<AddIcon /> <AddIcon />
</div> </div>

View File

@ -20,6 +20,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries,
}) })
$: { $: {

View File

@ -15,6 +15,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries,
}) })
let idFields let idFields

View File

@ -35,6 +35,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries,
}) })
} }

View File

@ -31,6 +31,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries,
}) })
const detailScreens = $allScreens.filter(screen => const detailScreens = $allScreens.filter(screen =>

View File

@ -45,6 +45,7 @@
components: $store.components, components: $store.components,
screen: $currentAsset, screen: $currentAsset,
tables: $backendUiStore.tables, tables: $backendUiStore.tables,
queries: $backendUiStore.queries
}) })
$: queryBindableProperties = bindableProperties.map(property => ({ $: queryBindableProperties = bindableProperties.map(property => ({