Skip to content

Commit

Permalink
feat: Add workflow data reset action (#4618)
Browse files Browse the repository at this point in the history
* feat: add workflow data reset action

* fix: remove console.log

* chore: fix linting issues
  • Loading branch information
alexgrozav committed Nov 17, 2022
1 parent f7a9ef9 commit 0daa36c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 37 deletions.
2 changes: 0 additions & 2 deletions packages/editor-ui/src/stores/workflows.ee.ts
Expand Up @@ -9,8 +9,6 @@ import {useSettingsStore} from "@/stores/settings";
import {defineStore} from "pinia";
import {useWorkflowsStore} from "@/stores/workflows";

// @TODO Move to workflows store as part of workflows store refactoring
//
export const useWorkflowsEEStore = defineStore(STORES.WORKFLOWS_EE, {
state() { return {}; },
actions: {
Expand Down
96 changes: 71 additions & 25 deletions packages/editor-ui/src/stores/workflows.ts
@@ -1,4 +1,11 @@
import { DEFAULT_NEW_WORKFLOW_NAME, DUPLICATE_POSTFFIX, MAX_WORKFLOW_NAME_LENGTH, PLACEHOLDER_EMPTY_WORKFLOW_ID, STORES } from "@/constants";
import {
DEFAULT_NEW_WORKFLOW_NAME,
DUPLICATE_POSTFFIX,
EnterpriseEditionFeature,
MAX_WORKFLOW_NAME_LENGTH,
PLACEHOLDER_EMPTY_WORKFLOW_ID,
STORES,
} from "@/constants";
import {
IExecutionResponse,
IExecutionsCurrentSummaryExtended,
Expand All @@ -13,34 +20,59 @@ import {
IWorkflowsMap,
WorkflowsState,
} from "@/Interface";
import { defineStore } from "pinia";
import { IConnection, IConnections, IDataObject, INode, INodeConnections, INodeCredentials, INodeCredentialsDetails, INodeExecutionData, INodeIssueData, IPinData, IRunData, ITaskData, IWorkflowSettings } from 'n8n-workflow';
import {defineStore} from "pinia";
import {
IConnection,
IConnections,
IDataObject,
INode,
INodeConnections,
INodeCredentials,
INodeCredentialsDetails,
INodeExecutionData,
INodeIssueData,
IPinData,
IRunData,
ITaskData,
IWorkflowSettings,
} from 'n8n-workflow';
import Vue from "vue";
import { useRootStore } from "./n8nRootStore";
import { getActiveWorkflows, getCurrentExecutions, getFinishedExecutions, getNewWorkflow, getWorkflows } from "@/api/workflows";
import { useUIStore } from "./ui";
import { getPairedItemsMapping } from "@/pairedItemUtils";
import { dataPinningEventBus } from "@/event-bus/data-pinning-event-bus";
import { isJsonKeyObject } from "@/utils";
import { stringSizeInBytes } from "@/components/helpers";
import { useNDVStore } from "./ndv";
import { useNodeTypesStore } from "./nodeTypes";
import {useRootStore} from "./n8nRootStore";
import {
getActiveWorkflows,
getCurrentExecutions,
getFinishedExecutions,
getNewWorkflow,
getWorkflows,
} from "@/api/workflows";
import {useUIStore} from "./ui";
import {getPairedItemsMapping} from "@/pairedItemUtils";
import {dataPinningEventBus} from "@/event-bus/data-pinning-event-bus";
import {isJsonKeyObject} from "@/utils";
import {stringSizeInBytes} from "@/components/helpers";
import {useNDVStore} from "./ndv";
import {useNodeTypesStore} from "./nodeTypes";
import {useWorkflowsEEStore} from "@/stores/workflows.ee";
import {useUsersStore} from "@/stores/users";
import {useSettingsStore} from "@/stores/settings";

const createEmptyWorkflow = (): IWorkflowDb => ({
id: PLACEHOLDER_EMPTY_WORKFLOW_ID,
name: '',
active: false,
createdAt: -1,
updatedAt: -1,
connections: {},
nodes: [],
settings: {},
tags: [],
pinData: {},
hash: '',
});

export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
state: (): WorkflowsState => ({
workflow: {
id: PLACEHOLDER_EMPTY_WORKFLOW_ID,
name: '',
active: false,
createdAt: -1,
updatedAt: -1,
connections: {},
nodes: [],
settings: {},
tags: [],
pinData: {},
hash: '',
},
workflow: createEmptyWorkflow(),
activeWorkflows: [],
activeExecutions: [],
currentWorkflowExecutions: [],
Expand Down Expand Up @@ -199,6 +231,8 @@ export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
},

async getNewWorkflowData(name?: string): Promise<INewWorkflowData> {
const workflowsEEStore = useWorkflowsEEStore();

let workflowData = {
name: '',
onboardingFlowEnabled: false,
Expand All @@ -213,9 +247,21 @@ export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
}

this.setWorkflowName({ newName: workflowData.name, setStateDirty: false });

return workflowData;
},

resetWorkflow() {
const usersStore = useUsersStore();
const settingsStore = useSettingsStore();

this.workflow = createEmptyWorkflow();

if (settingsStore.isEnterpriseFeatureEnabled(EnterpriseEditionFeature.WorkflowSharing)) {
Vue.set(this.workflow, 'ownedBy', usersStore.currentUser);
}
},

setWorkflowId (id: string): void {
this.workflow.id = id === 'new' ? PLACEHOLDER_EMPTY_WORKFLOW_ID : id;
},
Expand Down
27 changes: 17 additions & 10 deletions packages/editor-ui/src/views/NodeView.vue
Expand Up @@ -841,7 +841,6 @@ export default mixins(
this.workflowsStore.setWorkflowPinData(data.pinData || {});
this.workflowsStore.setWorkflowHash(data.hash);
// @TODO
this.workflowsStore.addWorkflow({
id: data.id,
name: data.name,
Expand All @@ -853,16 +852,23 @@ export default mixins(
updatedAt: data.updatedAt,
nodes: data.nodes,
connections: data.connections,
});
this.workflowsEEStore.setWorkflowOwnedBy({
workflowId: data.id,
ownedBy: data.ownedBy,
});
this.workflowsEEStore.setWorkflowSharedWith({
workflowId: data.id,
sharedWith: data.sharedWith,
hash: '',
});
if (data.ownedBy) {
this.workflowsEEStore.setWorkflowOwnedBy({
workflowId: data.id,
ownedBy: data.ownedBy,
});
}
if (data.sharedWith) {
this.workflowsEEStore.setWorkflowSharedWith({
workflowId: data.id,
sharedWith: data.sharedWith,
});
}
if (data.usedCredentials) {
this.credentialsStore.addCredentials(data.usedCredentials);
}
Expand Down Expand Up @@ -3084,6 +3090,8 @@ export default mixins(
return Promise.resolve(data);
},
resetWorkspace() {
this.workflowsStore.resetWorkflow();
// Reset nodes
this.deleteEveryEndpoint();
Expand All @@ -3104,7 +3112,6 @@ export default mixins(
this.workflowsStore.resetAllNodesIssues();
// vm.$forceUpdate();
this.workflowsStore.$patch({ workflow: {} });
this.workflowsStore.setActive(false);
this.workflowsStore.setWorkflowId(PLACEHOLDER_EMPTY_WORKFLOW_ID);
this.workflowsStore.setWorkflowName({ newName: '', setStateDirty: false });
Expand Down

0 comments on commit 0daa36c

Please sign in to comment.