diff --git a/packages/builder/src/builderStore/store/automation/Automation.js b/packages/builder/src/builderStore/store/automation/Automation.js index cbdcabeccc..a9dce88258 100644 --- a/packages/builder/src/builderStore/store/automation/Automation.js +++ b/packages/builder/src/builderStore/store/automation/Automation.js @@ -56,4 +56,13 @@ export default class Automation { steps.splice(stepIdx, 1) this.automation.definition.steps = steps } + + constructBlock(type, stepId, blockDefinition) { + return { + ...blockDefinition, + inputs: blockDefinition.inputs || {}, + stepId, + type, + } + } } diff --git a/packages/builder/src/builderStore/store/automation/index.js b/packages/builder/src/builderStore/store/automation/index.js index 3f0743aa1e..7a01bccfab 100644 --- a/packages/builder/src/builderStore/store/automation/index.js +++ b/packages/builder/src/builderStore/store/automation/index.js @@ -2,6 +2,7 @@ import { writable } from "svelte/store" import api from "../../api" import Automation from "./Automation" import { cloneDeep } from "lodash/fp" +import analytics from "analytics" const automationActions = store => ({ fetch: async () => { @@ -93,6 +94,9 @@ const automationActions = store => ({ state.selectedBlock = newBlock return state }) + analytics.captureEvent("Added Automation Block", { + name: block.name, + }) }, deleteAutomationBlock: block => { store.update(state => { diff --git a/packages/builder/src/components/automation/AutomationBuilder/BlockList.svelte b/packages/builder/src/components/automation/AutomationBuilder/BlockList.svelte index de3e9660ad..5ea1d64e51 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/BlockList.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/BlockList.svelte @@ -49,16 +49,9 @@ } function addBlockToAutomation(stepId, blockDefinition) { - const newBlock = { - ...blockDefinition, - inputs: blockDefinition.inputs || {}, - stepId, - type: selectedTab, - } + const newBlock = $automationStore.selectedAutomation.constructBlock( + selectedTab, stepId, blockDefinition) automationStore.actions.addBlockToAutomation(newBlock) - analytics.captureEvent("Added Automation Block", { - name: blockDefinition.name, - }) closePopover() if (stepId === "WEBHOOK") { webhookModal.show() diff --git a/packages/builder/src/components/automation/SetupPanel/SchemaSetup.svelte b/packages/builder/src/components/automation/SetupPanel/SchemaSetup.svelte index 281ec9f0d4..6a68f08980 100644 --- a/packages/builder/src/components/automation/SetupPanel/SchemaSetup.svelte +++ b/packages/builder/src/components/automation/SetupPanel/SchemaSetup.svelte @@ -8,7 +8,6 @@ })) let addNewName = "" - let addNewType = "string" function addField() { if (!addNewName) return @@ -76,7 +75,6 @@ bind:value={addNewName} placeholder="Enter field name" /> - diff --git a/packages/builder/src/components/userInterface/EventsEditor/EventEditorModal.svelte b/packages/builder/src/components/userInterface/EventsEditor/EventEditorModal.svelte index 27e91cd29e..d92ebed33e 100644 --- a/packages/builder/src/components/userInterface/EventsEditor/EventEditorModal.svelte +++ b/packages/builder/src/components/userInterface/EventsEditor/EventEditorModal.svelte @@ -3,6 +3,7 @@ import { AddIcon, ArrowDownIcon } from "components/common/Icons/" import actionTypes from "./actions" import { createEventDispatcher } from "svelte" + import { automationStore } from "builderStore" const dispatch = createEventDispatcher() const eventTypeKey = "##eventHandlerType" @@ -13,17 +14,11 @@ let addActionDropdown let selectedAction - let draftEventHandler = { parameters: [] } - $: actions = event || [] $: selectedActionComponent = selectedAction && actionTypes.find(t => t.name === selectedAction[eventTypeKey]).component - const updateEventHandler = (updatedHandler, index) => { - actions[index] = updatedHandler - } - const deleteAction = index => { actions.splice(index, 1) actions = actions @@ -44,8 +39,43 @@ selectedAction = action } - const saveEventData = () => { - dispatch("change", actions) + const saveEventData = async () => { + // e.g. The Trigger Automation action exposes beforeSave, so it can + // create any automations it needs to + for (let action of actions) { + if (action[eventTypeKey] === "Trigger Automation") { + await createAutomation(action.parameters) + } + } + dispatch("change", actions) + } + + // 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}) + + const appActionDefinition = $automationStore.blockDefinitions.TRIGGER.APP + + const newBlock = $automationStore.selectedAutomation.constructBlock( + "TRIGGER", "APP", appActionDefinition) + + + newBlock.inputs = { + fields: Object.entries(parameters.fields).reduce((fields, [key, value]) => { + fields[key] = value.type + return fields + }, {}) + } + + automationStore.actions.addBlockToAutomation(newBlock) + + await automationStore.actions.save( + $automationStore.selectedAutomation) + + parameters.automationId = $automationStore.selectedAutomation.automation._id + delete parameters.newAutomationName } diff --git a/packages/builder/src/components/userInterface/EventsEditor/actions/SaveFields.svelte b/packages/builder/src/components/userInterface/EventsEditor/actions/SaveFields.svelte index 3fe85cbfda..71d0ddf17f 100644 --- a/packages/builder/src/components/userInterface/EventsEditor/actions/SaveFields.svelte +++ b/packages/builder/src/components/userInterface/EventsEditor/actions/SaveFields.svelte @@ -1,6 +1,6 @@