Refactor automation store to properly handle errors

This commit is contained in:
Andrew Kingston 2022-01-25 15:44:08 +00:00
parent c06947cf46
commit 9ea5be7851
12 changed files with 242 additions and 197 deletions

View File

@ -22,7 +22,6 @@ export const getAutomationStore = () => {
const automationActions = store => ({ const automationActions = store => ({
fetch: async () => { fetch: async () => {
try {
const responses = await Promise.all([ const responses = await Promise.all([
API.getAutomations(), API.getAutomations(),
API.getAutomationDefinitions(), API.getAutomationDefinitions(),
@ -43,13 +42,8 @@ const automationActions = store => ({
} }
return state return state
}) })
} catch (error) {
notifications.error("Error fetching automations")
store.set(initialAutomationState)
}
}, },
create: async ({ name }) => { create: async ({ name }) => {
try {
const automation = { const automation = {
name, name,
type: "automation", type: "automation",
@ -63,12 +57,8 @@ const automationActions = store => ({
store.actions.select(response.automation) store.actions.select(response.automation)
return state return state
}) })
} catch (error) {
notifications.error("Error creating automation")
}
}, },
save: async automation => { save: async automation => {
try {
const response = await API.updateAutomation(automation) const response = await API.updateAutomation(automation)
store.update(state => { store.update(state => {
const updatedAutomation = response.automation const updatedAutomation = response.automation
@ -82,13 +72,8 @@ const automationActions = store => ({
return state return state
} }
}) })
notifications.success("Automation saved successfully")
} catch (error) {
notifications.error("Error saving automation")
}
}, },
delete: async automation => { delete: async automation => {
try {
await API.deleteAutomation({ await API.deleteAutomation({
automationId: automation?._id, automationId: automation?._id,
automationRev: automation?._rev, automationRev: automation?._rev,
@ -103,13 +88,12 @@ const automationActions = store => ({
state.selectedBlock = null state.selectedBlock = null
return state return state
}) })
notifications.success("Automation deleted successfully")
} catch (error) {
notifications.error("Error deleting automation")
}
}, },
test: async (automation, testData) => { test: async (automation, testData) => {
try { store.update(state => {
state.selectedAutomation.testResults = null
return state
})
const result = await API.testAutomation({ const result = await API.testAutomation({
automationId: automation?._id, automationId: automation?._id,
testData, testData,
@ -118,13 +102,6 @@ const automationActions = store => ({
state.selectedAutomation.testResults = result state.selectedAutomation.testResults = result
return state return state
}) })
} catch (error) {
notifications.error("Error testing automation")
store.update(state => {
state.selectedAutomation.testResults = null
return state
})
}
}, },
select: automation => { select: automation => {
store.update(state => { store.update(state => {

View File

@ -6,6 +6,7 @@
Body, Body,
Icon, Icon,
Tooltip, Tooltip,
notifications,
} from "@budibase/bbui" } from "@budibase/bbui"
import { automationStore } from "builderStore" import { automationStore } from "builderStore"
import { admin } from "stores/portal" import { admin } from "stores/portal"
@ -47,6 +48,7 @@
} }
async function addBlockToAutomation() { async function addBlockToAutomation() {
try {
const newBlock = $automationStore.selectedAutomation.constructBlock( const newBlock = $automationStore.selectedAutomation.constructBlock(
"ACTION", "ACTION",
actionVal.stepId, actionVal.stepId,
@ -56,6 +58,9 @@
await automationStore.actions.save( await automationStore.actions.save(
$automationStore.selectedAutomation?.automation $automationStore.selectedAutomation?.automation
) )
} catch (error) {
notifications.error("Error saving automation")
}
} }
</script> </script>

View File

@ -30,9 +30,13 @@
} }
async function deleteAutomation() { async function deleteAutomation() {
try {
await automationStore.actions.delete( await automationStore.actions.delete(
$automationStore.selectedAutomation?.automation $automationStore.selectedAutomation?.automation
) )
} catch (error) {
notifications.error("Error deleting automation")
}
} }
</script> </script>

View File

@ -10,6 +10,7 @@
Button, Button,
StatusLight, StatusLight,
ActionButton, ActionButton,
notifications,
} from "@budibase/bbui" } from "@budibase/bbui"
import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte" import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte"
import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte" import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte"
@ -54,10 +55,14 @@
).every(x => block?.inputs[x]) ).every(x => block?.inputs[x])
async function deleteStep() { async function deleteStep() {
try {
automationStore.actions.deleteAutomationBlock(block) automationStore.actions.deleteAutomationBlock(block)
await automationStore.actions.save( await automationStore.actions.save(
$automationStore.selectedAutomation?.automation $automationStore.selectedAutomation?.automation
) )
} catch (error) {
notifications.error("Error saving notification")
}
} }
</script> </script>

View File

@ -1,5 +1,12 @@
<script> <script>
import { ModalContent, Tabs, Tab, TextArea, Label } from "@budibase/bbui" import {
ModalContent,
Tabs,
Tab,
TextArea,
Label,
notifications,
} from "@budibase/bbui"
import { automationStore } from "builderStore" import { automationStore } from "builderStore"
import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte" import AutomationBlockSetup from "../../SetupPanel/AutomationBlockSetup.svelte"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
@ -37,6 +44,17 @@
failedParse = "Invalid JSON" failedParse = "Invalid JSON"
} }
} }
const testAutomation = async () => {
try {
await automationStore.actions.test(
$automationStore.selectedAutomation?.automation,
testData
)
} catch (error) {
notifications.error("Error testing notification")
}
}
</script> </script>
<ModalContent <ModalContent
@ -44,12 +62,7 @@
confirmText="Test" confirmText="Test"
showConfirmButton={true} showConfirmButton={true}
disabled={isError} disabled={isError}
onConfirm={() => { onConfirm={testAutomation}
automationStore.actions.test(
$automationStore.selectedAutomation?.automation,
testData
)
}}
cancelText="Cancel" cancelText="Cancel"
> >
<Tabs selected="Form" quiet <Tabs selected="Form" quiet

View File

@ -4,10 +4,16 @@
import { automationStore } from "builderStore" import { automationStore } from "builderStore"
import NavItem from "components/common/NavItem.svelte" import NavItem from "components/common/NavItem.svelte"
import EditAutomationPopover from "./EditAutomationPopover.svelte" import EditAutomationPopover from "./EditAutomationPopover.svelte"
import { notifications } from "@budibase/bbui"
$: selectedAutomationId = $automationStore.selectedAutomation?.automation?._id $: selectedAutomationId = $automationStore.selectedAutomation?.automation?._id
onMount(() => {
automationStore.actions.fetch() onMount(async () => {
try {
await automationStore.actions.fetch()
} catch (error) {
notifications.error("Error getting automations list")
}
}) })
function selectAutomation(automation) { function selectAutomation(automation) {

View File

@ -24,6 +24,7 @@
nameTouched && !name ? "Please specify a name for the automation." : null nameTouched && !name ? "Please specify a name for the automation." : null
async function createAutomation() { async function createAutomation() {
try {
await automationStore.actions.create({ await automationStore.actions.create({
name, name,
instanceId, instanceId,
@ -43,10 +44,13 @@
$automationStore.selectedAutomation?.automation $automationStore.selectedAutomation?.automation
) )
notifications.success(`Automation ${name} created.`) notifications.success(`Automation ${name} created`)
$goto(`./${$automationStore.selectedAutomation.automation._id}`) $goto(`./${$automationStore.selectedAutomation.automation._id}`)
analytics.captureEvent(Events.AUTOMATION.CREATED, { name }) analytics.captureEvent(Events.AUTOMATION.CREATED, { name })
} catch (error) {
notifications.error("Error creating automation")
}
} }
$: triggers = Object.entries($automationStore.blockDefinitions.TRIGGER) $: triggers = Object.entries($automationStore.blockDefinitions.TRIGGER)

View File

@ -11,9 +11,13 @@
let updateAutomationDialog let updateAutomationDialog
async function deleteAutomation() { async function deleteAutomation() {
try {
await automationStore.actions.delete(automation) await automationStore.actions.delete(automation)
notifications.success("Automation deleted.") notifications.success("Automation deleted successfully")
$goto("../automate") $goto("../automate")
} catch (error) {
notifications.error("Error deleting automation")
}
} }
</script> </script>

View File

@ -20,14 +20,18 @@
} }
async function saveAutomation() { async function saveAutomation() {
try {
const updatedAutomation = { const updatedAutomation = {
...automation, ...automation,
name, name,
} }
await automationStore.actions.save(updatedAutomation) await automationStore.actions.save(updatedAutomation)
notifications.success(`Automation ${name} updated successfully.`) notifications.success(`Automation ${name} updated successfully`)
analytics.captureEvent(Events.AUTOMATION.SAVED, { name }) analytics.captureEvent(Events.AUTOMATION.SAVED, { name })
hide() hide()
} catch (error) {
notifications.error("Error saving automation")
}
} }
function checkValid(evt) { function checkValid(evt) {

View File

@ -11,6 +11,7 @@
Drawer, Drawer,
Modal, Modal,
Detail, Detail,
notifications,
} from "@budibase/bbui" } from "@budibase/bbui"
import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte" import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte"
@ -54,6 +55,7 @@
$: schemaFields = table ? Object.values(table.schema) : [] $: schemaFields = table ? Object.values(table.schema) : []
const onChange = debounce(async function (e, key) { const onChange = debounce(async function (e, key) {
try {
if (isTestModal) { if (isTestModal) {
// Special case for webhook, as it requires a body, but the schema already brings back the body's contents // Special case for webhook, as it requires a body, but the schema already brings back the body's contents
if (stepId === "WEBHOOK") { if (stepId === "WEBHOOK") {
@ -77,6 +79,9 @@
$automationStore.selectedAutomation?.automation $automationStore.selectedAutomation?.automation
) )
} }
} catch (error) {
notifications.error("Error saving automation")
}
}, 800) }, 800)
function getAvailableBindings(block, automation) { function getAvailableBindings(block, automation) {

View File

@ -1,5 +1,5 @@
<script> <script>
import { Icon } from "@budibase/bbui" import { Icon, notifications } from "@budibase/bbui"
import { automationStore } from "builderStore" import { automationStore } from "builderStore"
import WebhookDisplay from "./WebhookDisplay.svelte" import WebhookDisplay from "./WebhookDisplay.svelte"
import { ModalContent } from "@budibase/bbui" import { ModalContent } from "@budibase/bbui"
@ -16,16 +16,25 @@
onMount(async () => { onMount(async () => {
if (!automation?.definition?.trigger?.inputs.schemaUrl) { if (!automation?.definition?.trigger?.inputs.schemaUrl) {
// save the automation initially // save the automation initially
try {
await automationStore.actions.save(automation) await automationStore.actions.save(automation)
} catch (error) {
notifications.error("Error saving automation")
}
} }
interval = setInterval(async () => { interval = setInterval(async () => {
try {
await automationStore.actions.fetch() await automationStore.actions.fetch()
const outputs = automation?.definition?.trigger.schema.outputs?.properties const outputs =
automation?.definition?.trigger.schema.outputs?.properties
// always one prop for the "body" // always one prop for the "body"
if (Object.keys(outputs).length > 1) { if (Object.keys(outputs).length > 1) {
propCount = Object.keys(outputs).length - 1 propCount = Object.keys(outputs).length - 1
finished = true finished = true
} }
} catch (error) {
notifications.error("Error getting automations list")
}
}, POLL_RATE_MS) }, POLL_RATE_MS)
schemaURL = automation?.definition?.trigger?.inputs.schemaUrl schemaURL = automation?.definition?.trigger?.inputs.schemaUrl
}) })

View File

@ -36,8 +36,13 @@
// called by the parent modal when actions are saved // called by the parent modal when actions are saved
const createAutomation = async parameters => { const createAutomation = async parameters => {
if (parameters.automationId || !parameters.newAutomationName) return if (parameters.automationId || !parameters.newAutomationName) {
await automationStore.actions.create({ name: parameters.newAutomationName }) return
}
try {
await automationStore.actions.create({
name: parameters.newAutomationName,
})
const appActionDefinition = $automationStore.blockDefinitions.TRIGGER.APP const appActionDefinition = $automationStore.blockDefinitions.TRIGGER.APP
const newBlock = $automationStore.selectedAutomation.constructBlock( const newBlock = $automationStore.selectedAutomation.constructBlock(
"TRIGGER", "TRIGGER",
@ -56,8 +61,12 @@
await automationStore.actions.save( await automationStore.actions.save(
$automationStore.selectedAutomation?.automation $automationStore.selectedAutomation?.automation
) )
parameters.automationId = $automationStore.selectedAutomation.automation._id parameters.automationId =
$automationStore.selectedAutomation.automation._id
delete parameters.newAutomationName delete parameters.newAutomationName
} catch (error) {
notifications.error("Error creating automation")
}
} }
</script> </script>