Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / workflow-panel / workflow-panel-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { bindDataExplorerActions } from 'store/data-explorer/data-explorer-action';
9 import { propertiesActions } from 'store/properties/properties-actions';
10 import { getProperty } from 'store/properties/properties';
11 import { navigateToRunProcess, navigateTo } from 'store/navigation/navigation-action';
12 import {
13     goToStep,
14     runProcessPanelActions,
15     loadPresets,
16     getWorkflowRunnerSettings
17 } from 'store/run-process-panel/run-process-panel-actions';
18 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
19 import { initialize } from 'redux-form';
20 import { RUN_PROCESS_BASIC_FORM } from 'views/run-process-panel/run-process-basic-form';
21 import { RUN_PROCESS_INPUTS_FORM } from 'views/run-process-panel/run-process-inputs-form';
22 import { RUN_PROCESS_ADVANCED_FORM } from 'views/run-process-panel/run-process-advanced-form';
23 import { getResource } from 'store/resources/resources';
24 import { ProjectResource } from 'models/project';
25 import { UserResource } from 'models/user';
26 import { getWorkflowInputs, parseWorkflowDefinition } from 'models/workflow';
27
28 export const WORKFLOW_PANEL_ID = "workflowPanel";
29 const UUID_PREFIX_PROPERTY_NAME = 'uuidPrefix';
30 const WORKFLOW_PANEL_DETAILS_UUID = 'workflowPanelDetailsUuid';
31 export const workflowPanelActions = bindDataExplorerActions(WORKFLOW_PANEL_ID);
32
33 export const loadWorkflowPanel = () =>
34     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35         dispatch(workflowPanelActions.REQUEST_ITEMS());
36         const response = await services.workflowService.list();
37         dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
38     };
39
40 export const setUuidPrefix = (uuidPrefix: string) =>
41     propertiesActions.SET_PROPERTY({ key: UUID_PREFIX_PROPERTY_NAME, value: uuidPrefix });
42
43 export const getUuidPrefix = (state: RootState) => {
44     return state.properties.uuidPrefix;
45 };
46
47 export const openRunProcess = (workflowUuid: string, ownerUuid?: string, name?: string, inputObj?: { [key: string]: any }) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         const response = await services.workflowService.list();
50         dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
51
52         const workflows = getState().runProcessPanel.searchWorkflows;
53         const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
54         if (workflow) {
55             dispatch<any>(navigateToRunProcess);
56             dispatch<any>(goToStep(1));
57             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
58             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
59             dispatch<any>(loadPresets(workflow.uuid));
60
61             dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, getWorkflowRunnerSettings(workflow)));
62             let owner;
63             if (ownerUuid) {
64                 // Must be writable.
65                 owner = getResource<ProjectResource | UserResource>(ownerUuid)(getState().resources);
66                 if (!owner || !owner.canWrite) {
67                     owner = undefined;
68                 }
69             }
70             if (owner) {
71                 dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(owner.uuid));
72             }
73
74             dispatch(initialize(RUN_PROCESS_BASIC_FORM, { name, owner }));
75
76             const definition = parseWorkflowDefinition(workflow);
77             if (definition) {
78                 const inputs = getWorkflowInputs(definition);
79                 if (inputs) {
80                     const values = inputs.reduce((values, input) => ({
81                         ...values,
82                         [input.id]: input.default,
83                     }), {});
84                     dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
85                 }
86             }
87
88             if (inputObj) {
89                 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, inputObj));
90             }
91         } else {
92             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't run this process` }));
93         }
94     };
95
96 export const getPublicUserUuid = (state: RootState) => {
97     const prefix = state.auth.localCluster;
98     return `${prefix}-tpzed-anonymouspublic`;
99 };
100 export const getPublicGroupUuid = (state: RootState) => {
101     const prefix = state.auth.localCluster;
102     return `${prefix}-j7d0g-anonymouspublic`;
103 };
104 export const getAllUsersGroupUuid = (state: RootState) => {
105     const prefix = state.auth.localCluster;
106     return `${prefix}-j7d0g-fffffffffffffff`;
107 };
108
109 export const showWorkflowDetails = (uuid: string) =>
110     propertiesActions.SET_PROPERTY({ key: WORKFLOW_PANEL_DETAILS_UUID, value: uuid });
111
112 export const getWorkflowDetails = (state: RootState) => {
113     const uuid = getProperty<string>(WORKFLOW_PANEL_DETAILS_UUID)(state.properties);
114     const workflows = state.runProcessPanel.workflows;
115     const workflow = workflows.find(workflow => workflow.uuid === uuid);
116     return workflow || undefined;
117 };
118
119 export const deleteWorkflow = (workflowUuid: string, ownerUuid: string) =>
120     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
121         dispatch<any>(navigateTo(ownerUuid));
122         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
123         await services.workflowService.delete(workflowUuid);
124         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
125     };