Ensure branch conditions are reinitialised on save. Improved error catching and notification

This commit is contained in:
Dean 2024-10-31 12:24:54 +00:00
parent d37c3ab99f
commit 5a9c586135
2 changed files with 41 additions and 10 deletions

View File

@ -2,7 +2,7 @@
import FlowItem from "./FlowItem.svelte"
import BranchNode from "./BranchNode.svelte"
import { AutomationActionStepId } from "@budibase/types"
import { ActionButton } from "@budibase/bbui"
import { ActionButton, notifications } from "@budibase/bbui"
import { automationStore } from "stores/builder"
import { environment } from "stores/portal"
import { cloneDeep } from "lodash"
@ -88,7 +88,7 @@
pathTo={pathToCurrentNode}
branchIdx={bIdx}
isLast={rightMost}
on:change={e => {
on:change={async e => {
const updatedBranch = { ...branch, ...e.detail }
if (!step?.inputs?.branches?.[bIdx]) {
@ -99,12 +99,31 @@
let branchStepUpdate = cloneDeep(step)
branchStepUpdate.inputs.branches[bIdx] = updatedBranch
// Ensure valid base configuration for all branches
// Reinitialise empty branch conditions on update
branchStepUpdate.inputs.branches.forEach(
(branch, i, branchArray) => {
if (!Object.keys(branch.condition).length) {
branchArray[i] = {
...branch,
...automationStore.actions.generateDefaultConditions(),
}
}
}
)
const updated = automationStore.actions.updateStep(
blockRef?.pathTo,
automation,
branchStepUpdate
)
automationStore.actions.save(updated)
try {
await automationStore.actions.save(updated)
} catch (e) {
notifications.error("Error saving branch update")
console.error("Error saving automation branch", e)
}
}}
/>
</div>

View File

@ -962,6 +962,24 @@ const automationActions = store => ({
}
},
/**
* Generate empty condition config
* Used on initialisation and reset of a condition.
*
* @returns {Object} contains a condition and conditionUI entry.
*/
generateDefaultConditions: () => {
const baseConditionUI = {
logicalOperator: "all",
onEmptyFilter: "none",
groups: [],
}
return {
condition: QueryUtils.buildQuery(baseConditionUI),
conditionUI: baseConditionUI,
}
},
/**
* Generates a new branch in the tree at the given location.
* All steps below the path, if any, are added to a new default branch
@ -978,15 +996,9 @@ const automationActions = store => ({
// Generate a default empty branch
const createBranch = name => {
const baseConditionUI = {
logicalOperator: "all",
onEmptyFilter: "none",
groups: [],
}
return {
name: name,
condition: QueryUtils.buildQuery(baseConditionUI),
conditionUI: baseConditionUI,
...generateDefaultConditions(),
id: generate(),
}
}