20251: Fix flaky collection file browser by using race-free state update callback
[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 } 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 } 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 { getUserUuid } from "common/getuser";
27 import { getWorkflowInputs, parseWorkflowDefinition } from 'models/workflow';
28
29 export const WORKFLOW_PANEL_ID = "workflowPanel";
30 const UUID_PREFIX_PROPERTY_NAME = 'uuidPrefix';
31 const WORKFLOW_PANEL_DETAILS_UUID = 'workflowPanelDetailsUuid';
32 export const workflowPanelActions = bindDataExplorerActions(WORKFLOW_PANEL_ID);
33
34 export const loadWorkflowPanel = () =>
35     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36         dispatch(workflowPanelActions.REQUEST_ITEMS());
37         const response = await services.workflowService.list();
38         dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
39     };
40
41 export const setUuidPrefix = (uuidPrefix: string) =>
42     propertiesActions.SET_PROPERTY({ key: UUID_PREFIX_PROPERTY_NAME, value: uuidPrefix });
43
44 export const getUuidPrefix = (state: RootState) => {
45     return state.properties.uuidPrefix;
46 };
47
48 export const openRunProcess = (workflowUuid: string, ownerUuid?: string, name?: string, inputObj?: { [key: string]: any }) =>
49     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50         const response = await services.workflowService.list();
51         dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
52
53         const workflows = getState().runProcessPanel.searchWorkflows;
54         const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
55         if (workflow) {
56             dispatch<any>(navigateToRunProcess);
57             dispatch<any>(goToStep(1));
58             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
59             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
60             dispatch<any>(loadPresets(workflow.uuid));
61
62             dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, getWorkflowRunnerSettings(workflow)));
63             let owner;
64             if (ownerUuid) {
65                 // Must be writable.
66                 const userUuid = getUserUuid(getState());
67                 owner = getResource<ProjectResource | UserResource>(ownerUuid)(getState().resources);
68                 if (!owner || !userUuid || owner.writableBy.indexOf(userUuid) === -1) {
69                     owner = undefined;
70                 }
71             }
72             if (owner) {
73                 dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(owner.uuid));
74             }
75
76             dispatch(initialize(RUN_PROCESS_BASIC_FORM, { name, owner }));
77
78             const definition = parseWorkflowDefinition(workflow);
79             if (definition) {
80                 const inputs = getWorkflowInputs(definition);
81                 if (inputs) {
82                     const values = inputs.reduce((values, input) => ({
83                         ...values,
84                         [input.id]: input.default,
85                     }), {});
86                     dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
87                 }
88             }
89
90             if (inputObj) {
91                 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, inputObj));
92             }
93         } else {
94             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't run this process` }));
95         }
96     };
97
98 export const getPublicUserUuid = (state: RootState) => {
99     const prefix = state.auth.localCluster;
100     return `${prefix}-tpzed-anonymouspublic`;
101 };
102 export const getPublicGroupUuid = (state: RootState) => {
103     const prefix = state.auth.localCluster;
104     return `${prefix}-j7d0g-anonymouspublic`;
105 };
106
107 export const showWorkflowDetails = (uuid: string) =>
108     propertiesActions.SET_PROPERTY({ key: WORKFLOW_PANEL_DETAILS_UUID, value: uuid });
109
110 export const getWorkflowDetails = (state: RootState) => {
111     const uuid = getProperty<string>(WORKFLOW_PANEL_DETAILS_UUID)(state.properties);
112     const workflows = state.runProcessPanel.workflows;
113     const workflow = workflows.find(workflow => workflow.uuid === uuid);
114     return workflow || undefined;
115 };