Update save row action to account for new context structure

This commit is contained in:
Andrew Kingston 2023-09-15 08:35:30 +01:00
parent 2f786aa29a
commit b24c47d53d
2 changed files with 31 additions and 44 deletions

View File

@ -199,7 +199,12 @@ export const getComponentBindableProperties = (asset, componentId) => {
* both global and local bindings, taking into account a component's position * both global and local bindings, taking into account a component's position
* in the component tree. * in the component tree.
*/ */
const getComponentContexts = (asset, componentId, type) => { export const getComponentContexts = (
asset,
componentId,
type,
options = { includeSelf: false }
) => {
if (!asset || !componentId) { if (!asset || !componentId) {
return [] return []
} }
@ -245,6 +250,11 @@ const getComponentContexts = (asset, componentId, type) => {
const localComponents = findComponentPath(asset.props, componentId) const localComponents = findComponentPath(asset.props, componentId)
localComponents.forEach(processContexts("local")) localComponents.forEach(processContexts("local"))
// Exclude self if required
if (!options?.includeSelf) {
delete map[componentId]
}
return Object.values(map) return Object.values(map)
} }
@ -257,17 +267,9 @@ export const getContextProviderComponents = (
type, type,
options = { includeSelf: false } options = { includeSelf: false }
) => { ) => {
let componentContexts = getComponentContexts(asset, componentId, type) return getComponentContexts(asset, componentId, type, options).map(
entry => entry.component
// Exclude self if required )
if (!options?.includeSelf) {
componentContexts = componentContexts.filter(
entry => entry.component._id !== componentId
)
}
// Ignore contexts and just return the component instances
return componentContexts.map(entry => entry.component)
} }
/** /**

View File

@ -3,8 +3,8 @@
import { store, currentAsset } from "builderStore" import { store, currentAsset } from "builderStore"
import { tables, viewsV2 } from "stores/backend" import { tables, viewsV2 } from "stores/backend"
import { import {
getContextProviderComponents,
getSchemaForDatasourcePlus, getSchemaForDatasourcePlus,
getComponentContexts,
} from "builderStore/dataBinding" } from "builderStore/dataBinding"
import SaveFields from "./SaveFields.svelte" import SaveFields from "./SaveFields.svelte"
@ -12,18 +12,18 @@
export let bindings = [] export let bindings = []
export let nested export let nested
$: formComponents = getContextProviderComponents( $: formContexts = getComponentContexts(
$currentAsset, $currentAsset,
$store.selectedComponentId, $store.selectedComponentId,
"form", "form",
{ includeSelf: nested } { includeSelf: nested }
) )
$: schemaComponents = getContextProviderComponents( $: schemaContexts = getComponentContexts(
$currentAsset, $currentAsset,
$store.selectedComponentId, $store.selectedComponentId,
"schema" "schema"
) )
$: providerOptions = getProviderOptions(formComponents, schemaComponents) $: providerOptions = getProviderOptions(formContexts, schemaContexts)
$: schemaFields = getSchemaFields(parameters?.tableId) $: schemaFields = getSchemaFields(parameters?.tableId)
$: tableOptions = $tables.list.map(table => ({ $: tableOptions = $tables.list.map(table => ({
label: table.name, label: table.name,
@ -35,38 +35,23 @@
})) }))
$: options = [...(tableOptions || []), ...(viewOptions || [])] $: options = [...(tableOptions || []), ...(viewOptions || [])]
// 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 // Gets options for valid context keys which provide valid data to submit
const getProviderOptions = (formComponents, schemaComponents) => { const getProviderOptions = (formContexts, schemaContexts) => {
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) const allContexts = formContexts.concat(schemaContexts)
let options = []
return allContexts.map(({ component, context }) => { allContexts.forEach(({ component, contexts }) => {
let runtimeBinding = component._id let runtimeBinding = component._id
if (context.suffix) { contexts.forEach(context => {
runtimeBinding += `-${context.suffix}` if (context.suffix) {
} runtimeBinding += `-${context.suffix}`
return { }
label: component._instanceName, options.push({
value: runtimeBinding, label: component._instanceName,
} value: runtimeBinding,
})
})
}) })
return options
} }
const getSchemaFields = resourceId => { const getSchemaFields = resourceId => {