From 403c52ddf89e370bf3d1bf926ff9bd590befac93 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Wed, 18 Dec 2019 14:17:31 -0600 Subject: [PATCH] :bug: Add missing file --- .../src/components/mixins/workflowSave.ts | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 packages/editor-ui/src/components/mixins/workflowSave.ts diff --git a/packages/editor-ui/src/components/mixins/workflowSave.ts b/packages/editor-ui/src/components/mixins/workflowSave.ts new file mode 100644 index 0000000000..2596329032 --- /dev/null +++ b/packages/editor-ui/src/components/mixins/workflowSave.ts @@ -0,0 +1,109 @@ +import { + IWorkflowData, +} from '../../Interface'; + +import { restApi } from '@/components/mixins/restApi'; +import { nodeHelpers } from '@/components/mixins/nodeHelpers'; +import { showMessage } from '@/components/mixins/showMessage'; +import { workflowHelpers } from '@/components/mixins/workflowHelpers'; + +import mixins from 'vue-typed-mixins'; + +export const workflowSave = mixins( + nodeHelpers, + restApi, + showMessage, + workflowHelpers, +) + .extend({ + methods: { + // Saves the currently loaded workflow to the database. + async saveCurrentWorkflow (withNewName = false) { + const currentWorkflow = this.$route.params.name; + let workflowName: string | null | undefined = ''; + if (currentWorkflow === undefined || withNewName === true) { + // Currently no workflow name is set to get it from user + workflowName = await this.$prompt( + 'Enter workflow name', + 'Name', + { + confirmButtonText: 'Save', + cancelButtonText: 'Cancel', + } + ) + .then((data) => { + // @ts-ignore + return data.value; + }) + .catch(() => { + // User did cancel + return undefined; + }); + + if (workflowName === undefined) { + // User did cancel + return; + } else if (['', null].includes(workflowName)) { + // User did not enter a name + this.$showMessage({ + title: 'Name missing', + message: `No name for the workflow got entered and could so not be saved!`, + type: 'error', + }); + return; + } + } + + try { + this.$store.commit('addActiveAction', 'workflowSaving'); + + let workflowData: IWorkflowData = await this.getWorkflowDataToSave(); + + if (currentWorkflow === undefined || withNewName === true) { + // Workflow is new or is supposed to get saved under a new name + // so create a new entry in database + workflowData.name = workflowName.trim() as string; + + if (withNewName === true) { + // If an existing workflow gets resaved with a new name + // make sure that the new ones is not active + workflowData.active = false; + } + + workflowData = await this.restApi().createNewWorkflow(workflowData); + + this.$store.commit('setActive', workflowData.active || false); + this.$store.commit('setWorkflowId', workflowData.id); + this.$store.commit('setWorkflowName', workflowData.name); + this.$store.commit('setWorkflowSettings', workflowData.settings || {}); + } else { + // Workflow exists already so update it + await this.restApi().updateWorkflow(currentWorkflow, workflowData); + } + + if (this.$route.params.name !== workflowData.id) { + this.$router.push({ + name: 'NodeViewExisting', + params: { name: workflowData.id as string, action: 'workflowSave' }, + }); + } + + this.$store.commit('removeActiveAction', 'workflowSaving'); + + this.$showMessage({ + title: 'Workflow saved', + message: `The workflow "${workflowData.name}" got saved!`, + type: 'success', + }); + } catch (e) { + this.$store.commit('removeActiveAction', 'workflowSaving'); + + this.$showMessage({ + title: 'Problem saving workflow', + message: `There was a problem saving the workflow: "${e.message}"`, + type: 'error', + }); + } + }, + }, + });