Skip to content

Commit

Permalink
Ensure branch conditions are reinitialised on save. Improved error ca…
Browse files Browse the repository at this point in the history
…tching and notification
  • Loading branch information
deanhannigan committed Oct 31, 2024
1 parent d37c3ab commit 5a9c586
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]) {
Expand All @@ -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>
Expand Down
26 changes: 19 additions & 7 deletions packages/builder/src/stores/builder/automations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),

Check failure on line 1001 in packages/builder/src/stores/builder/automations.js

View workflow job for this annotation

GitHub Actions / lint

'generateDefaultConditions' is not defined
id: generate(),
}
}
Expand Down

0 comments on commit 5a9c586

Please sign in to comment.