From 9d528412192c19440fe507d96b7654fb1516123f Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 25 Jan 2022 15:44:08 +0000 Subject: [PATCH] Refactor automation store to properly handle errors --- .../builderStore/store/automation/index.js | 165 ++++++++---------- .../FlowChart/ActionModal.svelte | 23 ++- .../FlowChart/FlowChart.svelte | 10 +- .../FlowChart/FlowItem.svelte | 13 +- .../FlowChart/TestDataModal.svelte | 27 ++- .../AutomationPanel/AutomationList.svelte | 10 +- .../CreateAutomationModal.svelte | 46 ++--- .../EditAutomationPopover.svelte | 10 +- .../UpdateAutomationModal.svelte | 18 +- .../SetupPanel/AutomationBlockSetup.svelte | 43 +++-- .../Shared/CreateWebhookModal.svelte | 25 ++- .../ButtonActionEditor.svelte | 49 +++--- 12 files changed, 242 insertions(+), 197 deletions(-) diff --git a/packages/builder/src/builderStore/store/automation/index.js b/packages/builder/src/builderStore/store/automation/index.js index 73aa2757ec..e521e167dd 100644 --- a/packages/builder/src/builderStore/store/automation/index.js +++ b/packages/builder/src/builderStore/store/automation/index.js @@ -22,109 +22,86 @@ export const getAutomationStore = () => { const automationActions = store => ({ fetch: async () => { - try { - const responses = await Promise.all([ - API.getAutomations(), - API.getAutomationDefinitions(), - ]) - store.update(state => { - let selected = state.selectedAutomation?.automation - state.automations = responses[0] - state.blockDefinitions = { - TRIGGER: responses[1].trigger, - ACTION: responses[1].action, - } - // If previously selected find the new obj and select it - if (selected) { - selected = responses[0].filter( - automation => automation._id === selected._id - ) - state.selectedAutomation = new Automation(selected[0]) - } - return state - }) - } catch (error) { - notifications.error("Error fetching automations") - store.set(initialAutomationState) - } + const responses = await Promise.all([ + API.getAutomations(), + API.getAutomationDefinitions(), + ]) + store.update(state => { + let selected = state.selectedAutomation?.automation + state.automations = responses[0] + state.blockDefinitions = { + TRIGGER: responses[1].trigger, + ACTION: responses[1].action, + } + // If previously selected find the new obj and select it + if (selected) { + selected = responses[0].filter( + automation => automation._id === selected._id + ) + state.selectedAutomation = new Automation(selected[0]) + } + return state + }) }, create: async ({ name }) => { - try { - const automation = { - name, - type: "automation", - definition: { - steps: [], - }, - } - const response = await API.createAutomation(automation) - store.update(state => { - state.automations = [...state.automations, response.automation] - store.actions.select(response.automation) - return state - }) - } catch (error) { - notifications.error("Error creating automation") + const automation = { + name, + type: "automation", + definition: { + steps: [], + }, } + const response = await API.createAutomation(automation) + store.update(state => { + state.automations = [...state.automations, response.automation] + store.actions.select(response.automation) + return state + }) }, save: async automation => { - try { - const response = await API.updateAutomation(automation) - store.update(state => { - const updatedAutomation = response.automation - const existingIdx = state.automations.findIndex( - existing => existing._id === automation._id - ) - if (existingIdx !== -1) { - state.automations.splice(existingIdx, 1, updatedAutomation) - state.automations = [...state.automations] - store.actions.select(updatedAutomation) - return state - } - }) - notifications.success("Automation saved successfully") - } catch (error) { - notifications.error("Error saving automation") - } + const response = await API.updateAutomation(automation) + store.update(state => { + const updatedAutomation = response.automation + const existingIdx = state.automations.findIndex( + existing => existing._id === automation._id + ) + if (existingIdx !== -1) { + state.automations.splice(existingIdx, 1, updatedAutomation) + state.automations = [...state.automations] + store.actions.select(updatedAutomation) + return state + } + }) }, delete: async automation => { - try { - await API.deleteAutomation({ - automationId: automation?._id, - automationRev: automation?._rev, - }) - store.update(state => { - const existingIdx = state.automations.findIndex( - existing => existing._id === automation?._id - ) - state.automations.splice(existingIdx, 1) - state.automations = [...state.automations] - state.selectedAutomation = null - state.selectedBlock = null - return state - }) - notifications.success("Automation deleted successfully") - } catch (error) { - notifications.error("Error deleting automation") - } + await API.deleteAutomation({ + automationId: automation?._id, + automationRev: automation?._rev, + }) + store.update(state => { + const existingIdx = state.automations.findIndex( + existing => existing._id === automation?._id + ) + state.automations.splice(existingIdx, 1) + state.automations = [...state.automations] + state.selectedAutomation = null + state.selectedBlock = null + return state + }) }, test: async (automation, testData) => { - try { - const result = await API.testAutomation({ - automationId: automation?._id, - testData, - }) - store.update(state => { - state.selectedAutomation.testResults = result - return state - }) - } catch (error) { - notifications.error("Error testing automation") - store.update(state => { - state.selectedAutomation.testResults = null - return state - }) - } + store.update(state => { + state.selectedAutomation.testResults = null + return state + }) + const result = await API.testAutomation({ + automationId: automation?._id, + testData, + }) + store.update(state => { + state.selectedAutomation.testResults = result + return state + }) }, select: automation => { store.update(state => { diff --git a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/ActionModal.svelte b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/ActionModal.svelte index 5ae031e033..4e1e5e1103 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/ActionModal.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/ActionModal.svelte @@ -6,6 +6,7 @@ Body, Icon, Tooltip, + notifications, } from "@budibase/bbui" import { automationStore } from "builderStore" import { admin } from "stores/portal" @@ -47,15 +48,19 @@ } async function addBlockToAutomation() { - const newBlock = $automationStore.selectedAutomation.constructBlock( - "ACTION", - actionVal.stepId, - actionVal - ) - automationStore.actions.addBlockToAutomation(newBlock, blockIdx + 1) - await automationStore.actions.save( - $automationStore.selectedAutomation?.automation - ) + try { + const newBlock = $automationStore.selectedAutomation.constructBlock( + "ACTION", + actionVal.stepId, + actionVal + ) + automationStore.actions.addBlockToAutomation(newBlock, blockIdx + 1) + await automationStore.actions.save( + $automationStore.selectedAutomation?.automation + ) + } catch (error) { + notifications.error("Error saving automation") + } } diff --git a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowChart.svelte b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowChart.svelte index 5bf625940a..777fcd710a 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowChart.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowChart.svelte @@ -30,9 +30,13 @@ } async function deleteAutomation() { - await automationStore.actions.delete( - $automationStore.selectedAutomation?.automation - ) + try { + await automationStore.actions.delete( + $automationStore.selectedAutomation?.automation + ) + } catch (error) { + notifications.error("Error deleting automation") + } } diff --git a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItem.svelte b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItem.svelte index fe94b7e63f..f13a827f31 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItem.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/FlowItem.svelte @@ -10,6 +10,7 @@ Button, StatusLight, ActionButton, + notifications, } from "@budibase/bbui" import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte" import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte" @@ -54,10 +55,14 @@ ).every(x => block?.inputs[x]) async function deleteStep() { - automationStore.actions.deleteAutomationBlock(block) - await automationStore.actions.save( - $automationStore.selectedAutomation?.automation - ) + try { + automationStore.actions.deleteAutomationBlock(block) + await automationStore.actions.save( + $automationStore.selectedAutomation?.automation + ) + } catch (error) { + notifications.error("Error saving notification") + } } diff --git a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte index e43729edbe..ffd59b4e6a 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte @@ -1,5 +1,12 @@ { - automationStore.actions.test( - $automationStore.selectedAutomation?.automation, - testData - ) - }} + onConfirm={testAutomation} cancelText="Cancel" > { - automationStore.actions.fetch() + + onMount(async () => { + try { + await automationStore.actions.fetch() + } catch (error) { + notifications.error("Error getting automations list") + } }) function selectAutomation(automation) { diff --git a/packages/builder/src/components/automation/AutomationPanel/CreateAutomationModal.svelte b/packages/builder/src/components/automation/AutomationPanel/CreateAutomationModal.svelte index 36723d7726..4fb912939a 100644 --- a/packages/builder/src/components/automation/AutomationPanel/CreateAutomationModal.svelte +++ b/packages/builder/src/components/automation/AutomationPanel/CreateAutomationModal.svelte @@ -24,29 +24,33 @@ nameTouched && !name ? "Please specify a name for the automation." : null async function createAutomation() { - await automationStore.actions.create({ - name, - instanceId, - }) - const newBlock = $automationStore.selectedAutomation.constructBlock( - "TRIGGER", - triggerVal.stepId, - triggerVal - ) + try { + await automationStore.actions.create({ + name, + instanceId, + }) + const newBlock = $automationStore.selectedAutomation.constructBlock( + "TRIGGER", + triggerVal.stepId, + triggerVal + ) - automationStore.actions.addBlockToAutomation(newBlock) - if (triggerVal.stepId === "WEBHOOK") { - webhookModal.show + automationStore.actions.addBlockToAutomation(newBlock) + if (triggerVal.stepId === "WEBHOOK") { + webhookModal.show + } + + await automationStore.actions.save( + $automationStore.selectedAutomation?.automation + ) + + notifications.success(`Automation ${name} created`) + + $goto(`./${$automationStore.selectedAutomation.automation._id}`) + analytics.captureEvent(Events.AUTOMATION.CREATED, { name }) + } catch (error) { + notifications.error("Error creating automation") } - - await automationStore.actions.save( - $automationStore.selectedAutomation?.automation - ) - - notifications.success(`Automation ${name} created.`) - - $goto(`./${$automationStore.selectedAutomation.automation._id}`) - analytics.captureEvent(Events.AUTOMATION.CREATED, { name }) } $: triggers = Object.entries($automationStore.blockDefinitions.TRIGGER) diff --git a/packages/builder/src/components/automation/AutomationPanel/EditAutomationPopover.svelte b/packages/builder/src/components/automation/AutomationPanel/EditAutomationPopover.svelte index fc12b60676..0d858d7a19 100644 --- a/packages/builder/src/components/automation/AutomationPanel/EditAutomationPopover.svelte +++ b/packages/builder/src/components/automation/AutomationPanel/EditAutomationPopover.svelte @@ -11,9 +11,13 @@ let updateAutomationDialog async function deleteAutomation() { - await automationStore.actions.delete(automation) - notifications.success("Automation deleted.") - $goto("../automate") + try { + await automationStore.actions.delete(automation) + notifications.success("Automation deleted successfully") + $goto("../automate") + } catch (error) { + notifications.error("Error deleting automation") + } } diff --git a/packages/builder/src/components/automation/AutomationPanel/UpdateAutomationModal.svelte b/packages/builder/src/components/automation/AutomationPanel/UpdateAutomationModal.svelte index 64197c3a77..cc2512be8f 100644 --- a/packages/builder/src/components/automation/AutomationPanel/UpdateAutomationModal.svelte +++ b/packages/builder/src/components/automation/AutomationPanel/UpdateAutomationModal.svelte @@ -20,14 +20,18 @@ } async function saveAutomation() { - const updatedAutomation = { - ...automation, - name, + try { + const updatedAutomation = { + ...automation, + name, + } + await automationStore.actions.save(updatedAutomation) + notifications.success(`Automation ${name} updated successfully`) + analytics.captureEvent(Events.AUTOMATION.SAVED, { name }) + hide() + } catch (error) { + notifications.error("Error saving automation") } - await automationStore.actions.save(updatedAutomation) - notifications.success(`Automation ${name} updated successfully.`) - analytics.captureEvent(Events.AUTOMATION.SAVED, { name }) - hide() } function checkValid(evt) { diff --git a/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte b/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte index aa84013d46..64b7cff78d 100644 --- a/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte +++ b/packages/builder/src/components/automation/SetupPanel/AutomationBlockSetup.svelte @@ -11,6 +11,7 @@ Drawer, Modal, Detail, + notifications, } from "@budibase/bbui" import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte" @@ -54,28 +55,32 @@ $: schemaFields = table ? Object.values(table.schema) : [] const onChange = debounce(async function (e, key) { - if (isTestModal) { - // Special case for webhook, as it requires a body, but the schema already brings back the body's contents - if (stepId === "WEBHOOK") { + try { + if (isTestModal) { + // Special case for webhook, as it requires a body, but the schema already brings back the body's contents + if (stepId === "WEBHOOK") { + automationStore.actions.addTestDataToAutomation({ + body: { + [key]: e.detail, + ...$automationStore.selectedAutomation.automation.testData.body, + }, + }) + } automationStore.actions.addTestDataToAutomation({ - body: { - [key]: e.detail, - ...$automationStore.selectedAutomation.automation.testData.body, - }, + [key]: e.detail, }) + testData[key] = e.detail + await automationStore.actions.save( + $automationStore.selectedAutomation?.automation + ) + } else { + block.inputs[key] = e.detail + await automationStore.actions.save( + $automationStore.selectedAutomation?.automation + ) } - automationStore.actions.addTestDataToAutomation({ - [key]: e.detail, - }) - testData[key] = e.detail - await automationStore.actions.save( - $automationStore.selectedAutomation?.automation - ) - } else { - block.inputs[key] = e.detail - await automationStore.actions.save( - $automationStore.selectedAutomation?.automation - ) + } catch (error) { + notifications.error("Error saving automation") } }, 800) diff --git a/packages/builder/src/components/automation/Shared/CreateWebhookModal.svelte b/packages/builder/src/components/automation/Shared/CreateWebhookModal.svelte index 20dd696981..d8bc7fca3b 100644 --- a/packages/builder/src/components/automation/Shared/CreateWebhookModal.svelte +++ b/packages/builder/src/components/automation/Shared/CreateWebhookModal.svelte @@ -1,5 +1,5 @@