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

View File

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

View File

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

View File

@ -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() {
try {
automationStore.actions.deleteAutomationBlock(block)
await automationStore.actions.save(
$automationStore.selectedAutomation?.automation
)
} catch (error) {
notifications.error("Error saving notification")
}
}
</script>

View File

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

View File

@ -4,10 +4,16 @@
import { automationStore } from "builderStore"
import NavItem from "components/common/NavItem.svelte"
import EditAutomationPopover from "./EditAutomationPopover.svelte"
import { notifications } from "@budibase/bbui"
$: 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) {

View File

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

View File

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

View File

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

View File

@ -11,6 +11,7 @@
Drawer,
Modal,
Detail,
notifications,
} from "@budibase/bbui"
import CreateWebhookModal from "components/automation/Shared/CreateWebhookModal.svelte"
@ -54,6 +55,7 @@
$: schemaFields = table ? Object.values(table.schema) : []
const onChange = debounce(async function (e, key) {
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") {
@ -77,6 +79,9 @@
$automationStore.selectedAutomation?.automation
)
}
} catch (error) {
notifications.error("Error saving automation")
}
}, 800)
function getAvailableBindings(block, automation) {

View File

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

View File

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