Fix multiple issues with resetting state in both builder and client lib when updating multi step form config

This commit is contained in:
Andrew Kingston 2023-12-12 21:00:45 +00:00
parent 69ea28ad46
commit 1579fe36c7
4 changed files with 47 additions and 34 deletions

View File

@ -4,7 +4,7 @@
import { getDatasourceForProvider } from "builderStore/dataBinding" import { getDatasourceForProvider } from "builderStore/dataBinding"
import { currentAsset, store } from "builderStore" import { currentAsset, store } from "builderStore"
import { Helpers } from "@budibase/bbui" import { Helpers } from "@budibase/bbui"
import { writable } from "svelte/store" import { derived, writable } from "svelte/store"
import { Utils } from "@budibase/frontend-core" import { Utils } from "@budibase/frontend-core"
export let componentInstance export let componentInstance
@ -17,21 +17,21 @@
stepCount: value?.length ?? 0, stepCount: value?.length ?? 0,
currentStep: 0, currentStep: 0,
}) })
const currentStep = derived(multiStepStore, state => state.currentStep)
setContext("multi-step-form-block", multiStepStore) setContext("multi-step-form-block", multiStepStore)
$: defaultProps = Utils.buildMultiStepFormBlockDefaultProps({ $: defaultProps = Utils.buildMultiStepFormBlockDefaultProps({
_id: componentInstance._id, _id: componentInstance._id,
stepCount: stepCount, stepCount: stepCount,
currentStep: currentStep, step: $multiStepStore.currentStep,
}) })
$: currentStep = $multiStepStore.currentStep
$: stepCount = value?.length || 0 $: stepCount = value?.length || 0
$: multiStepStore.update(state => ({ ...state, stepCount })) $: updateStore(stepCount)
$: dataSource = getDatasourceForProvider($currentAsset, componentInstance) $: dataSource = getDatasourceForProvider($currentAsset, componentInstance)
$: emitCurrentStep(currentStep) $: emitCurrentStep($currentStep)
$: sectionName = getSectionName($multiStepStore) $: sectionName = getSectionName($multiStepStore)
$: stepInstance = buildPseudoInstance(value[currentStep], defaultProps) $: stepInstance = buildPseudoInstance(value, $currentStep, defaultProps)
$: stepDef = { $: stepDef = {
settings: [ settings: [
{ {
@ -72,6 +72,16 @@
], ],
} }
const updateStore = stepCount => {
multiStepStore.update(state => {
state.stepCount = stepCount
if (state.currentStep >= stepCount) {
state.currentStep = 0
}
return { ...state }
})
}
const getSectionName = ({ stepCount, currentStep }) => { const getSectionName = ({ stepCount, currentStep }) => {
if (stepCount <= 1) { if (stepCount <= 1) {
return "Details" return "Details"
@ -81,49 +91,47 @@
const emitCurrentStep = step => { const emitCurrentStep = step => {
store.actions.preview.sendEvent("builder-meta", { store.actions.preview.sendEvent("builder-meta", {
component: { componentId: componentInstance._id,
_id: componentInstance._id, step: step,
step: step,
},
}) })
} }
const addStep = () => { const addStep = () => {
dispatch("change", value.toSpliced(currentStep + 1, 0, {})) dispatch("change", value.toSpliced($currentStep + 1, 0, {}))
multiStepStore.update(state => ({ multiStepStore.update(state => ({
...state, ...state,
currentStep: currentStep + 1, currentStep: $currentStep + 1,
})) }))
} }
const removeStep = () => { const removeStep = () => {
dispatch("change", value.toSpliced(currentStep, 1)) dispatch("change", value.toSpliced($currentStep, 1))
multiStepStore.update(state => ({ multiStepStore.update(state => ({
...state, ...state,
currentStep: Math.min(currentStep, stepCount - 2), currentStep: Math.min($currentStep, stepCount - 2),
})) }))
} }
const previousStep = () => { const previousStep = () => {
multiStepStore.update(state => ({ multiStepStore.update(state => ({
...state, ...state,
currentStep: Math.max(currentStep - 1, 0), currentStep: Math.max($currentStep - 1, 0),
})) }))
} }
const nextStep = () => { const nextStep = () => {
multiStepStore.update(state => ({ multiStepStore.update(state => ({
...state, ...state,
currentStep: Math.min(currentStep + 1, value.length - 1), currentStep: Math.min($currentStep + 1, value.length - 1),
})) }))
} }
const updateStep = (field, val) => { const updateStep = (field, val) => {
const newStep = { const newStep = {
...value[currentStep], ...value[$currentStep],
[field.key]: val, [field.key]: val,
} }
dispatch("change", value.toSpliced(currentStep, 1, newStep)) dispatch("change", value.toSpliced($currentStep, 1, newStep))
} }
const handleStepAction = action => { const handleStepAction = action => {
@ -150,8 +158,8 @@
} }
} }
const buildPseudoInstance = (instance, defaultProps) => { const buildPseudoInstance = (value, currentStep, defaultProps) => {
const { buttons, fields, title, desc } = instance || {} const { buttons, fields, title, desc } = value?.[currentStep] || {}
return { return {
_id: Helpers.uuid(), _id: Helpers.uuid(),
_component: "@budibase/standard-components/multistepformblockstep", _component: "@budibase/standard-components/multistepformblockstep",

View File

@ -33,16 +33,17 @@
$: fetchSchema(dataSource) $: fetchSchema(dataSource)
$: enrichedSteps = enrichSteps(steps, schema, $component.id) $: enrichedSteps = enrichSteps(steps, schema, $component.id)
$: currentStep = getCurrentStep( $: currentStep = getCurrentStep($builderStore, $component)
$builderStore?.component?._id,
componentInstance
)
const getCurrentStep = () => { const getCurrentStep = (builderStore, component) => {
if ($builderStore?.component?._id === $component.id) { if (
return $builderStore?.component.step !component.selected ||
!builderStore.inBuilder ||
builderStore.metadata?.componentId !== component.id
) {
return null
} }
return 0 return (builderStore.metadata.step || 0) + 1
} }
const getPropsForField = field => { const getPropsForField = field => {
@ -108,7 +109,7 @@
props={{ props={{
dataSource, dataSource,
initialFormStep, initialFormStep,
step: $builderStore.inBuilder === true ? currentStep + 1 : null, step: currentStep,
}} }}
context="form" context="form"
> >

View File

@ -76,11 +76,8 @@ const loadBudibase = async () => {
} else { } else {
dndStore.actions.reset() dndStore.actions.reset()
} }
} else if ("builder-meta") { } else if (type === "builder-meta") {
builderStore.update(state => ({ builderStore.actions.setMetadata(data)
...state,
...data,
}))
} }
} }

View File

@ -18,6 +18,7 @@ const createBuilderStore = () => {
hiddenComponentIds: [], hiddenComponentIds: [],
usedPlugins: null, usedPlugins: null,
eventResolvers: {}, eventResolvers: {},
metadata: null,
// Legacy - allow the builder to specify a layout // Legacy - allow the builder to specify a layout
layout: null, layout: null,
@ -123,6 +124,12 @@ const createBuilderStore = () => {
parentType, parentType,
}) })
}, },
setMetadata: metadata => {
store.update(state => ({
...state,
metadata,
}))
},
} }
return { return {
...store, ...store,