Generate bindings for unsaved "Update State" actions (#9508)

* Ensure update state actions have a binding for their own key

* Make own binding generation more futureproof

* Refactor array.find to array.some

* Move state binding generation up a level to account for all actions needing to be able to reference newly created state bindings
This commit is contained in:
Andrew Kingston 2023-02-01 17:53:50 +00:00 committed by GitHub
parent abfa7db36e
commit 91a3707527
2 changed files with 45 additions and 12 deletions

View File

@ -509,21 +509,24 @@ const getSelectedRowsBindings = asset => {
return bindings return bindings
} }
export const makeStateBinding = key => {
return {
type: "context",
runtimeBinding: `${makePropSafe("state")}.${makePropSafe(key)}`,
readableBinding: `State.${key}`,
category: "State",
icon: "AutomatedSegment",
display: { name: key },
}
}
/** /**
* Gets all state bindings that are globally available. * Gets all state bindings that are globally available.
*/ */
const getStateBindings = () => { const getStateBindings = () => {
let bindings = [] let bindings = []
if (get(store).clientFeatures?.state) { if (get(store).clientFeatures?.state) {
const safeState = makePropSafe("state") bindings = getAllStateVariables().map(makeStateBinding)
bindings = getAllStateVariables().map(key => ({
type: "context",
runtimeBinding: `${safeState}.${makePropSafe(key)}`,
readableBinding: `State.${key}`,
category: "State",
icon: "AutomatedSegment",
display: { name: key },
}))
} }
return bindings return bindings
} }

View File

@ -11,7 +11,10 @@
} from "@budibase/bbui" } from "@budibase/bbui"
import { getAvailableActions } from "./index" import { getAvailableActions } from "./index"
import { generate } from "shortid" import { generate } from "shortid"
import { getEventContextBindings } from "builderStore/dataBinding" import {
getEventContextBindings,
makeStateBinding,
} from "builderStore/dataBinding"
import { currentAsset, store } from "builderStore" import { currentAsset, store } from "builderStore"
const flipDurationMs = 150 const flipDurationMs = 150
@ -52,7 +55,7 @@
actions, actions,
selectedAction?.id selectedAction?.id
) )
$: allBindings = eventContexBindings.concat(bindings) $: allBindings = getAllBindings(bindings, eventContexBindings, actions)
$: { $: {
// Ensure each action has a unique ID // Ensure each action has a unique ID
if (actions) { if (actions) {
@ -111,6 +114,33 @@
function handleDndFinalize(e) { function handleDndFinalize(e) {
actions = e.detail.items actions = e.detail.items
} }
const getAllBindings = (bindings, eventContextBindings, actions) => {
let allBindings = eventContextBindings.concat(bindings)
// Ensure bindings are generated for all "update state" action keys
actions
.filter(action => {
// Find all "Update State" actions which set values
return (
action[EVENT_TYPE_KEY] === "Update State" &&
action.parameters?.type === "set" &&
action.parameters.key
)
})
.forEach(action => {
// Check we have a binding for this action, and generate one if not
const stateBinding = makeStateBinding(action.parameters.key)
const hasKey = allBindings.some(binding => {
return binding.runtimeBinding === stateBinding.runtimeBinding
})
if (!hasKey) {
allBindings.push(stateBinding)
}
})
return allBindings
}
</script> </script>
<DrawerContent> <DrawerContent>
@ -186,7 +216,7 @@
<div class="selected-action-container"> <div class="selected-action-container">
<svelte:component <svelte:component
this={selectedActionComponent} this={selectedActionComponent}
parameters={selectedAction.parameters} bind:parameters={selectedAction.parameters}
bindings={allBindings} bindings={allBindings}
{nested} {nested}
/> />