2021-01-26 09:55:44 +01:00
|
|
|
<script>
|
2021-08-19 13:53:31 +02:00
|
|
|
import { getContext, onMount } from "svelte"
|
2021-06-10 12:17:14 +02:00
|
|
|
import InnerForm from "./InnerForm.svelte"
|
2021-01-26 09:55:44 +01:00
|
|
|
|
2021-03-19 14:09:22 +01:00
|
|
|
export let dataSource
|
2021-01-26 09:55:44 +01:00
|
|
|
export let theme
|
|
|
|
export let size
|
2021-02-17 16:16:44 +01:00
|
|
|
export let disabled = false
|
2021-06-09 13:55:17 +02:00
|
|
|
export let actionType = "Create"
|
2021-01-26 09:55:44 +01:00
|
|
|
|
2021-02-01 19:51:22 +01:00
|
|
|
const context = getContext("context")
|
2021-08-19 13:53:31 +02:00
|
|
|
const { API } = getContext("sdk")
|
|
|
|
|
|
|
|
let loaded = false
|
|
|
|
let schema
|
|
|
|
let table
|
2021-01-26 09:55:44 +01:00
|
|
|
|
2021-02-16 16:30:20 +01:00
|
|
|
// Returns the closes data context which isn't a built in context
|
2021-06-09 13:53:12 +02:00
|
|
|
const getInitialValues = (type, dataSource, context) => {
|
|
|
|
// Only inherit values for update forms
|
|
|
|
if (type !== "Update") {
|
|
|
|
return {}
|
|
|
|
}
|
2021-06-25 13:15:45 +02:00
|
|
|
// Only inherit values for forms targeting internal tables
|
2021-06-09 13:53:12 +02:00
|
|
|
if (!dataSource?.tableId) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
// Don't inherit values representing built in contexts
|
2021-02-16 16:30:20 +01:00
|
|
|
if (["user", "url"].includes(context.closestComponentId)) {
|
|
|
|
return {}
|
|
|
|
}
|
2021-08-19 18:18:41 +02:00
|
|
|
// Always inherit the closest data source
|
2021-06-09 13:53:12 +02:00
|
|
|
const closestContext = context[`${context.closestComponentId}`] || {}
|
2021-08-19 18:18:41 +02:00
|
|
|
return closestContext || {}
|
2021-02-16 16:30:20 +01:00
|
|
|
}
|
|
|
|
|
2021-08-19 13:53:31 +02:00
|
|
|
// Fetches the form schema from this form's dataSource, if one exists
|
|
|
|
const fetchSchema = async () => {
|
|
|
|
if (!dataSource?.tableId) {
|
|
|
|
schema = {}
|
|
|
|
table = null
|
|
|
|
} else {
|
|
|
|
table = await API.fetchTableDefinition(dataSource?.tableId)
|
|
|
|
if (table) {
|
|
|
|
if (dataSource?.type === "query") {
|
|
|
|
schema = {}
|
|
|
|
const params = table.parameters || []
|
|
|
|
params.forEach(param => {
|
|
|
|
schema[param.name] = { ...param, type: "string" }
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
schema = table.schema || {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loaded = true
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:17:14 +02:00
|
|
|
$: initialValues = getInitialValues(actionType, dataSource, $context)
|
|
|
|
$: resetKey = JSON.stringify(initialValues)
|
2021-08-19 13:53:31 +02:00
|
|
|
|
|
|
|
// Load the form schema on mount
|
|
|
|
onMount(fetchSchema)
|
2021-01-26 09:55:44 +01:00
|
|
|
</script>
|
|
|
|
|
2021-08-19 13:53:31 +02:00
|
|
|
{#if loaded}
|
|
|
|
{#key resetKey}
|
|
|
|
<InnerForm
|
|
|
|
{dataSource}
|
|
|
|
{theme}
|
|
|
|
{size}
|
|
|
|
{disabled}
|
|
|
|
{actionType}
|
|
|
|
{schema}
|
|
|
|
{table}
|
|
|
|
{initialValues}
|
|
|
|
>
|
|
|
|
<slot />
|
|
|
|
</InnerForm>
|
|
|
|
{/key}
|
|
|
|
{/if}
|