1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
14 runProcessPanelActions,
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, RUN_PROCESS_INPUTS_FORM, RUN_PROCESS_ADVANCED_FORM } from 'store/run-process-panel/run-process-panel-actions';
21 import { getResource } from 'store/resources/resources';
22 import { ProjectResource } from 'models/project';
23 import { UserResource } from 'models/user';
24 import { getWorkflowInputs, parseWorkflowDefinition } from 'models/workflow';
25 import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
26 import { dialogActions } from 'store/dialog/dialog-actions';
27 import { ResourceKind, Resource } from 'models/resource';
28 import { selectedToArray } from "components/multiselect-toolbar/MultiselectToolbar";
29 import { CommonResourceServiceError, getCommonResourceServiceError } from "services/common-service/common-resource-service";
30 import { projectPanelDataActions } from "store/project-panel/project-panel-action-bind";
32 export const WORKFLOW_PANEL_ID = "workflowPanel";
33 const UUID_PREFIX_PROPERTY_NAME = 'uuidPrefix';
34 const WORKFLOW_PANEL_DETAILS_UUID = 'workflowPanelDetailsUuid';
35 export const workflowPanelActions = bindDataExplorerActions(WORKFLOW_PANEL_ID);
37 export const WORKFLOW_PROCESSES_PANEL_ID = "workflowProcessesPanel";
38 export const workflowProcessesPanelActions = bindDataExplorerActions(WORKFLOW_PROCESSES_PANEL_ID);
40 export const loadWorkflowPanel = () =>
41 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42 dispatch(workflowPanelActions.REQUEST_ITEMS());
43 const response = await services.workflowService.list();
44 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
47 export const setUuidPrefix = (uuidPrefix: string) =>
48 propertiesActions.SET_PROPERTY({ key: UUID_PREFIX_PROPERTY_NAME, value: uuidPrefix });
50 export const getUuidPrefix = (state: RootState) => {
51 return state.properties.uuidPrefix;
54 export const openRunProcess = (workflowUuid: string, ownerUuid?: string, name?: string, inputObj?: { [key: string]: any }) =>
55 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56 const response = await services.workflowService.list();
57 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
59 const workflows = getState().runProcessPanel.searchWorkflows;
60 const listedWorkflow = workflows.find(workflow => workflow.uuid === workflowUuid);
61 const workflow = listedWorkflow || (await services.workflowService.get(workflowUuid));
63 dispatch<any>(navigateToRunProcess);
64 dispatch<any>(goToStep(1));
65 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
66 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
67 dispatch<any>(loadPresets(workflow.uuid));
69 dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, getWorkflowRunnerSettings(workflow)));
73 owner = getResource<ProjectResource | UserResource>(ownerUuid)(getState().resources);
74 if (!owner || !owner.canWrite) {
79 dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(owner.uuid));
82 dispatch(initialize(RUN_PROCESS_BASIC_FORM, { name, owner }));
84 const definition = parseWorkflowDefinition(workflow);
86 const inputs = getWorkflowInputs(definition);
88 const values = inputs.reduce((values, input) => ({
90 [input.id]: input.default,
92 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
97 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, inputObj));
100 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't run this process` }));
104 export const getPublicUserUuid = (state: RootState) => {
105 const prefix = state.auth.localCluster;
106 return `${prefix}-tpzed-anonymouspublic`;
108 export const getPublicGroupUuid = (state: RootState) => {
109 const prefix = state.auth.localCluster;
110 return `${prefix}-j7d0g-anonymouspublic`;
112 export const getAllUsersGroupUuid = (state: RootState) => {
113 const prefix = state.auth.localCluster;
114 return `${prefix}-j7d0g-fffffffffffffff`;
117 export const showWorkflowDetails = (uuid: string) =>
118 propertiesActions.SET_PROPERTY({ key: WORKFLOW_PANEL_DETAILS_UUID, value: uuid });
120 export const getWorkflowDetails = (state: RootState) => {
121 const uuid = getProperty<string>(WORKFLOW_PANEL_DETAILS_UUID)(state.properties);
122 const workflows = state.runProcessPanel.workflows;
123 const workflow = workflows.find(workflow => workflow.uuid === uuid);
124 return workflow || undefined;
127 export const openRemoveWorkflowDialog =
128 (resource: ContextMenuResource, numOfWorkflows: Number) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
129 const confirmationText =
131 ? "Are you sure you want to remove this workflow?"
132 : `Are you sure you want to remove these ${numOfWorkflows} workflows?`;
133 const titleText = numOfWorkflows === 1 ? "Remove workflow permanently" : "Remove workflows permanently";
136 dialogActions.OPEN_DIALOG({
137 id: REMOVE_WORKFLOW_DIALOG,
140 text: confirmationText,
141 confirmButtonLabel: "Remove",
149 export const REMOVE_WORKFLOW_DIALOG = "removeWorkflowDialog";
151 export const removeWorkflowPermanently = (uuid: string, ownerUuid?: string) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
152 const resource = getState().dialog.removeWorkflowDialog.data.resource;
153 const checkedList = getState().multiselect.checkedList;
155 const uuidsToRemove: string[] = resource.fromContextMenu ? [resource.uuid] : selectedToArray(checkedList);
157 //if no items in checkedlist, default to normal context menu behavior
158 if (!uuidsToRemove.length) uuidsToRemove.push(uuid);
159 if(ownerUuid) dispatch<any>(navigateTo(ownerUuid));
161 const workflowsToRemove = uuidsToRemove
162 .map(uuid => getResource(uuid)(getState().resources) as Resource)
163 .filter(resource => resource.kind === ResourceKind.WORKFLOW);
165 for (const workflow of workflowsToRemove) {
167 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Removing ...", kind: SnackbarKind.INFO }));
168 await services.workflowService.delete(workflow.uuid);
169 dispatch(projectPanelDataActions.REQUEST_ITEMS());
170 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Removed.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
172 const error = getCommonResourceServiceError(e);
173 if (error === CommonResourceServiceError.PERMISSION_ERROR_FORBIDDEN) {
174 dispatch(snackbarActions.OPEN_SNACKBAR({ message: `Access denied`, hideDuration: 2000, kind: SnackbarKind.ERROR }));
176 dispatch(snackbarActions.OPEN_SNACKBAR({ message: `Deletion failed`, hideDuration: 2000, kind: SnackbarKind.ERROR }));