From 38fe8375ce5364eda62ceef7d2b06aa8d66fc719 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Fri, 27 Oct 2023 13:47:34 +0100 Subject: [PATCH] fix bindings references being updated --- .../builderStore/store/automation/index.js | 47 ++++++++++++++++++- .../FlowChart/FlowItem.svelte | 2 +- .../SetupPanel/AutomationBlockSetup.svelte | 7 +-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/builder/src/builderStore/store/automation/index.js b/packages/builder/src/builderStore/store/automation/index.js index f60e10e50b..1b94f9b05b 100644 --- a/packages/builder/src/builderStore/store/automation/index.js +++ b/packages/builder/src/builderStore/store/automation/index.js @@ -3,6 +3,7 @@ import { API } from "api" import { cloneDeep } from "lodash/fp" import { generate } from "shortid" import { selectedAutomation } from "builderStore" +import { notifications } from "@budibase/bbui" const initialAutomationState = { automations: [], @@ -21,6 +22,38 @@ export const getAutomationStore = () => { return store } +function updateReferencesInObject(obj, modifiedIndex, action) { + const regex = /{{\s*steps\.(\d+)\./g + + for (const key in obj) { + if (typeof obj[key] === "string") { + let matches + while ((matches = regex.exec(obj[key])) !== null) { + const referencedStep = parseInt(matches[1]) + if (action === "add" && referencedStep >= modifiedIndex) { + obj[key] = obj[key].replace( + `{{ steps.${referencedStep}.`, + `{{ steps.${referencedStep + 1}.` + ) + } else if (action === "delete" && referencedStep > modifiedIndex) { + obj[key] = obj[key].replace( + `{{ steps.${referencedStep}.`, + `{{ steps.${referencedStep - 1}.` + ) + } + } + } else if (typeof obj[key] === "object" && obj[key] !== null) { + updateReferencesInObject(obj[key], modifiedIndex, action) + } + } +} + +function updateStepReferences(steps, modifiedIndex, action) { + steps.forEach(step => { + updateReferencesInObject(step.inputs, modifiedIndex, action) + }) +} + const automationActions = store => ({ definitions: async () => { const response = await API.getAutomationDefinitions() @@ -218,6 +251,12 @@ const automationActions = store => ({ if (!automation) { return } + + try { + updateStepReferences(newAutomation.definition.steps, blockIdx, "add") + } catch (e) { + notifications.error("Error adding automation block") + } newAutomation.definition.steps.splice(blockIdx, 0, block) await store.actions.save(newAutomation) }, @@ -245,7 +284,7 @@ const automationActions = store => ({ await store.actions.save(newAutomation) }, - deleteAutomationBlock: async block => { + deleteAutomationBlock: async (block, blockIdx) => { const automation = get(selectedAutomation) let newAutomation = cloneDeep(automation) @@ -259,6 +298,12 @@ const automationActions = store => ({ ) delete newAutomation.definition.stepNames[block.id] } + try { + updateStepReferences(newAutomation.definition.steps, blockIdx, "delete") + } catch (e) { + notifications.error("Error deleting automation block") + } + await store.actions.save(newAutomation) }, replace: async (automationId, automation) => { diff --git a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItem.svelte b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItem.svelte index 2cdb1e1e72..f494728cb4 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItem.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItem.svelte @@ -82,7 +82,7 @@ if (loopBlock) { await automationStore.actions.deleteAutomationBlock(loopBlock) } - await automationStore.actions.deleteAutomationBlock(block) + await automationStore.actions.deleteAutomationBlock(block, blockIdx) } catch (error) { notifications.error("Error saving automation") } diff --git a/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte b/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte index b62053cc8c..9d931af7bb 100644 --- a/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte +++ b/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte @@ -58,7 +58,7 @@ let fillWidth = true let inputData let codeBindingOpen = false - + $: console.log($selectedAutomation?.definition) $: filters = lookForFilters(schemaProperties) || [] $: tempFilters = filters $: stepId = block.stepId @@ -136,6 +136,7 @@ await automationStore.actions.addTestDataToAutomation(newTestData) } else { const data = { schema, [key]: e.detail } + console.log(data) await automationStore.actions.updateBlockInputs(block, data) } } catch (error) { @@ -195,8 +196,8 @@ bindingRank = idx - loopBlockCount } let bindingName = - automation.stepNames?.[allSteps[bindingRank - loopBlockCount].id] - + automation.stepNames?.[allSteps[idx - loopBlockCount].id] + console.log(bindingName) bindings = bindings.concat( outputs.map(([name, value]) => { let runtimeName = isLoopBlock