Ensuire the save row action determines the correct runtime bindings and only uses valid context types

This commit is contained in:
Andrew Kingston 2021-12-01 10:32:55 +00:00
parent 52b7e41e5f
commit a3af1ece09
2 changed files with 57 additions and 7 deletions

View File

@ -61,7 +61,7 @@ export const getComponentBindableProperties = (asset, componentId) => {
/** /**
* Gets all data provider components above a component. * Gets all data provider components above a component.
*/ */
export const getContextProviderComponents = (asset, componentId) => { export const getContextProviderComponents = (asset, componentId, type) => {
if (!asset || !componentId) { if (!asset || !componentId) {
return [] return []
} }
@ -74,7 +74,18 @@ export const getContextProviderComponents = (asset, componentId) => {
// Filter by only data provider components // Filter by only data provider components
return path.filter(component => { return path.filter(component => {
const def = store.actions.components.getDefinition(component._component) const def = store.actions.components.getDefinition(component._component)
return def?.context != null if (!def?.context) {
return false
}
// If no type specified, return anything that exposes context
if (!type) {
return true
}
// Otherwise only match components with the specific context type
const contexts = Array.isArray(def.context) ? def.context : [def.context]
return contexts.find(context => context.type === type) != null
}) })
} }

View File

@ -11,13 +11,54 @@
export let parameters export let parameters
export let bindings = [] export let bindings = []
$: dataProviderComponents = getContextProviderComponents( $: formComponents = getContextProviderComponents(
$currentAsset, $currentAsset,
$store.selectedComponentId $store.selectedComponentId,
"form"
) )
$: schemaComponents = getContextProviderComponents(
$currentAsset,
$store.selectedComponentId,
"schema"
)
$: providerOptions = getProviderOptions(formComponents, schemaComponents)
$: schemaFields = getSchemaFields($currentAsset, parameters?.tableId) $: schemaFields = getSchemaFields($currentAsset, parameters?.tableId)
$: tableOptions = $tables.list || [] $: tableOptions = $tables.list || []
// Gets a context definition of a certain type from a component definition
const extractComponentContext = (component, contextType) => {
const def = store.actions.components.getDefinition(component?._component)
if (!def) {
return null
}
const contexts = Array.isArray(def.context) ? def.context : [def.context]
return contexts.find(context => context?.type === contextType)
}
// Gets options for valid context keys which provide valid data to submit
const getProviderOptions = (formComponents, schemaComponents) => {
const formContexts = formComponents.map(component => ({
component,
context: extractComponentContext(component, "form"),
}))
const schemaContexts = schemaComponents.map(component => ({
component,
context: extractComponentContext(component, "schema"),
}))
const allContexts = formContexts.concat(schemaContexts)
return allContexts.map(({ component, context }) => {
let runtimeBinding = component._id
if (context.suffix) {
runtimeBinding += `-${context.suffix}`
}
return {
label: component._instanceName,
value: runtimeBinding,
}
})
}
const getSchemaFields = (asset, tableId) => { const getSchemaFields = (asset, tableId) => {
const { schema } = getSchemaForDatasource(asset, { type: "table", tableId }) const { schema } = getSchemaForDatasource(asset, { type: "table", tableId })
return Object.values(schema || {}) return Object.values(schema || {})
@ -39,10 +80,8 @@
<Label small>Data Source</Label> <Label small>Data Source</Label>
<Select <Select
bind:value={parameters.providerId} bind:value={parameters.providerId}
options={dataProviderComponents} options={providerOptions}
placeholder="None" placeholder="None"
getOptionLabel={option => option._instanceName}
getOptionValue={option => option._id}
/> />
<Label small>Table</Label> <Label small>Table</Label>