refactor activation logic

This commit is contained in:
Mutasem 2022-01-17 20:55:03 +01:00
parent 21d3d40f29
commit 686548cc28
2 changed files with 40 additions and 50 deletions

View file

@ -81,12 +81,12 @@ export default mixins(
} }
return '#13ce66'; return '#13ce66';
}, },
isCurrentWorkflowOpen(): boolean { isCurrentWorkflow(): boolean {
return this.$store.getters['workflowId'] === this.workflowId; return this.$store.getters['workflowId'] === this.workflowId;
}, },
disabled(): boolean { disabled(): boolean {
const isNewWorkflow = !this.workflowId; const isNewWorkflow = !this.workflowId;
if (isNewWorkflow || this.isCurrentWorkflowOpen) { if (isNewWorkflow || this.isCurrentWorkflow) {
return !this.workflowActive && !this.containsTrigger; return !this.workflowActive && !this.containsTrigger;
} }
@ -103,11 +103,7 @@ export default mixins(
}, },
}, },
methods: { methods: {
async activeChanged (newActiveState: boolean) { async updateCurrentWorkflow(newActiveState: boolean) {
if (this.workflowId === undefined) {
await this.saveCurrentWorkflow();
}
if (this.nodesIssuesExist === true) { if (this.nodesIssuesExist === true) {
this.$showMessage({ this.$showMessage({
title: this.$locale.baseText('workflowActivator.showMessage.activeChangedNodesIssuesExistTrue.title'), title: this.$locale.baseText('workflowActivator.showMessage.activeChangedNodesIssuesExistTrue.title'),
@ -117,21 +113,37 @@ export default mixins(
return; return;
} }
// Set that the active state should be changed await this.saveCurrentWorkflow({
let data: IWorkflowDataUpdate = {}; active: newActiveState,
});
const activeWorkflowId = this.$store.getters.workflowId; if (newActiveState && window.localStorage.getItem(LOCAL_STORAGE_ACTIVATION_FLAG) !== 'true') {
if (newActiveState === true && this.workflowId === activeWorkflowId) { this.$store.dispatch('ui/openModal', WORKFLOW_ACTIVE_MODAL_KEY);
// Get the current workflow data that it gets saved together with the activation
data = await this.getWorkflowDataToSave();
} }
},
async activateOtherWorkflow(newActiveState: boolean) {
// Set that the active state should be changed
const data: IWorkflowDataUpdate = {};
data.active = newActiveState; data.active = newActiveState;
await this.restApi().updateWorkflow(this.workflowId, data);
if (newActiveState === true) {
this.$store.commit('setWorkflowActive', this.workflowId);
} else {
this.$store.commit('setWorkflowInactive', this.workflowId);
}
},
async activeChanged (newActiveState: boolean) {
this.loading = true; this.loading = true;
try { try {
await this.restApi().updateWorkflow(this.workflowId, data); if (!this.workflowId || this.isCurrentWorkflow) {
await this.updateCurrentWorkflow(newActiveState);
}
else {
await this.activateOtherWorkflow(newActiveState);
}
} catch (error) { } catch (error) {
const newStateName = newActiveState === true ? 'activated' : 'deactivated'; const newStateName = newActiveState === true ? 'activated' : 'deactivated';
this.$showError( this.$showError(
@ -146,26 +158,7 @@ export default mixins(
return; return;
} }
const currentWorkflowId = this.$store.getters.workflowId; const activationEventName = (!this.workflowId || this.isCurrentWorkflow) ? 'workflow.activeChangeCurrent' : 'workflow.activeChange';
let activationEventName = 'workflow.activeChange';
if (currentWorkflowId === this.workflowId) {
// If the status of the current workflow got changed
// commit it specifically
this.$store.commit('setActive', newActiveState);
activationEventName = 'workflow.activeChangeCurrent';
}
if (newActiveState === true) {
this.$store.commit('setWorkflowActive', this.workflowId);
// Show activation dialog only for current workflow and if user hasn't deactivated it
if (this.isCurrentWorkflowOpen && window.localStorage.getItem(LOCAL_STORAGE_ACTIVATION_FLAG) !== 'true') {
this.$store.dispatch('ui/openModal', WORKFLOW_ACTIVE_MODAL_KEY);
}
} else {
this.$store.commit('setWorkflowInactive', this.workflowId);
}
this.$externalHooks().run(activationEventName, { workflowId: this.workflowId, active: newActiveState }); this.$externalHooks().run(activationEventName, { workflowId: this.workflowId, active: newActiveState });
this.$telemetry.track('User set workflow active status', { workflow_id: this.workflowId, is_active: newActiveState }); this.$telemetry.track('User set workflow active status', { workflow_id: this.workflowId, is_active: newActiveState });

View file

@ -437,7 +437,7 @@ export const workflowHelpers = mixins(
return returnData['__xxxxxxx__']; return returnData['__xxxxxxx__'];
}, },
async saveCurrentWorkflow({name, tags}: {name?: string, tags?: string[]} = {}): Promise<boolean> { async saveCurrentWorkflow({name, tags, active}: {name?: string, tags?: string[], active?: boolean} = {}): Promise<boolean> {
const currentWorkflow = this.$route.params.name; const currentWorkflow = this.$route.params.name;
if (!currentWorkflow) { if (!currentWorkflow) {
return this.saveAsNewWorkflow({name, tags}); return this.saveAsNewWorkflow({name, tags});
@ -457,20 +457,9 @@ export const workflowHelpers = mixins(
workflowDataRequest.tags = tags; workflowDataRequest.tags = tags;
} }
// // Check if trigger was removed from active workflow if (active !== undefined) {
// let triggerIsRemoved = false; workflowDataRequest.active = active;
// if (this.$store.getters.isActive) { }
// const triggers = this.$store.getters.workflowTriggerNodes
// .filter((node: INodeUi) => {
// return !node.disabled;
// });
// triggerIsRemoved = triggers.length === 0;
// if (triggerIsRemoved) {
// // Only set active if trigger was removed
// workflowDataRequest.active = false;
// }
// }
const workflowData = await this.restApi().updateWorkflow(currentWorkflow, workflowDataRequest); const workflowData = await this.restApi().updateWorkflow(currentWorkflow, workflowDataRequest);
@ -491,6 +480,14 @@ export const workflowHelpers = mixins(
this.$store.commit('setWorkflowTagIds', tagIds); this.$store.commit('setWorkflowTagIds', tagIds);
} }
this.$store.commit('setActive', workflowData.active);
if (workflowData.active === true) {
this.$store.commit('setWorkflowActive', workflowData.id);
} else {
this.$store.commit('setWorkflowInactive', workflowData.id);
}
this.$store.commit('setStateDirty', false); this.$store.commit('setStateDirty', false);
this.$store.commit('removeActiveAction', 'workflowSaving'); this.$store.commit('removeActiveAction', 'workflowSaving');
this.$externalHooks().run('workflow.afterUpdate', { workflowData }); this.$externalHooks().run('workflow.afterUpdate', { workflowData });