Update duplicate row action to use new context structure

This commit is contained in:
Andrew Kingston 2023-09-15 08:40:22 +01:00
parent b24c47d53d
commit b36b061bb2
1 changed files with 23 additions and 35 deletions

View File

@ -3,7 +3,7 @@
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, getComponentContexts,
getSchemaForDatasourcePlus, getSchemaForDatasourcePlus,
} from "builderStore/dataBinding" } from "builderStore/dataBinding"
import SaveFields from "./SaveFields.svelte" import SaveFields from "./SaveFields.svelte"
@ -11,18 +11,18 @@
export let parameters export let parameters
export let bindings = [] export let bindings = []
$: formComponents = getContextProviderComponents( $: formContexts = getComponentContexts(
$currentAsset, $currentAsset,
$store.selectedComponentId, $store.selectedComponentId,
"form" "form"
) )
$: schemaComponents = getContextProviderComponents( $: schemaContexts = getComponentContexts(
$currentAsset, $currentAsset,
$store.selectedComponentId, $store.selectedComponentId,
"schema" "schema"
) )
$: providerOptions = getProviderOptions(formComponents, schemaComponents) $: providerOptions = getProviderOptions(formContexts, schemaContexts)
$: schemaFields = getSchemaFields($currentAsset, parameters?.tableId) $: schemaFields = getSchemaFields(parameters?.tableId)
$: tableOptions = $tables.list.map(table => ({ $: tableOptions = $tables.list.map(table => ({
label: table.name, label: table.name,
resourceId: table._id, resourceId: table._id,
@ -33,44 +33,32 @@
})) }))
$: 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 = (asset, tableId) => { const getSchemaFields = resourceId => {
const { schema } = getSchemaForDatasourcePlus(tableId) let { schema } = getSchemaForDatasourcePlus(resourceId)
// Since we're duplicating, we don't want to allow changing the ID or rev
delete schema._id delete schema._id
delete schema._rev delete schema._rev
return Object.values(schema || {}) return Object.values(schema || {})
} }