From 5a9c58613553ff2428e4f50078a2e6aff1d623b6 Mon Sep 17 00:00:00 2001 From: Dean Date: Thu, 31 Oct 2024 12:24:54 +0000 Subject: [PATCH] Ensure branch conditions are reinitialised on save. Improved error catching and notification --- .../FlowChart/StepNode.svelte | 25 +++++++++++++++--- .../builder/src/stores/builder/automations.js | 26 ++++++++++++++----- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/StepNode.svelte b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/StepNode.svelte index 323dbaf7395..4132cf3516e 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/StepNode.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/StepNode.svelte @@ -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) + } }} /> diff --git a/packages/builder/src/stores/builder/automations.js b/packages/builder/src/stores/builder/automations.js index 1566dc48224..99d78e082cc 100644 --- a/packages/builder/src/stores/builder/automations.js +++ b/packages/builder/src/stores/builder/automations.js @@ -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(), } }