Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / process-panel / process-panel-actions.ts
index 9b1e984706af6a487036d2f21ebd868017696e49..2111afdb2fc89d05eaba56ad46add0c43beccf19 100644 (file)
@@ -3,26 +3,24 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { unionize, ofType, UnionOf } from "common/unionize";
-import { getInputs, getOutputParameters, getRawInputs, getRawOutputs, loadProcess } from 'store/processes/processes-actions';
-import { Dispatch } from 'redux';
-import { ProcessStatus } from 'store/processes/process';
-import { RootState } from 'store/store';
+import { getInputs, getOutputParameters, getRawInputs, getRawOutputs, loadProcess } from "store/processes/processes-actions";
+import { Dispatch } from "redux";
+import { ProcessStatus } from "store/processes/process";
+import { RootState } from "store/store";
 import { ServiceRepository } from "services/services";
-import { navigateTo, navigateToWorkflows } from 'store/navigation/navigation-action';
-import { snackbarActions } from 'store/snackbar/snackbar-actions';
-import { SnackbarKind } from '../snackbar/snackbar-actions';
-import { showWorkflowDetails } from 'store/workflow-panel/workflow-panel-actions';
-import { loadSubprocessPanel } from "../subprocess-panel/subprocess-panel-actions";
+import { navigateTo } from "store/navigation/navigation-action";
+import { snackbarActions } from "store/snackbar/snackbar-actions";
+import { SnackbarKind } from "../snackbar/snackbar-actions";
+import { loadSubprocessPanel, subprocessPanelActions } from "../subprocess-panel/subprocess-panel-actions";
 import { initProcessLogsPanel, processLogsPanelActions } from "store/process-logs-panel/process-logs-panel-actions";
 import { CollectionFile } from "models/collection-file";
 import { ContainerRequestResource } from "models/container-request";
-import { CommandOutputParameter } from 'cwlts/mappings/v1.0/CommandOutputParameter';
-import { CommandInputParameter, getIOParamId, WorkflowInputsData } from 'models/workflow';
+import { CommandOutputParameter } from "cwlts/mappings/v1.0/CommandOutputParameter";
+import { CommandInputParameter, getIOParamId, WorkflowInputsData } from "models/workflow";
 import { getIOParamDisplayValue, ProcessIOParameter } from "views/process-panel/process-io-card";
 import { OutputDetails, NodeInstanceType, NodeInfo } from "./process-panel";
 import { AuthState } from "store/auth/auth-reducer";
-import { CommonService } from "services/common-service/common-service";
-import { camelCase } from "lodash";
+import { ContextMenuResource } from "store/context-menu/context-menu-actions";
 
 export const processPanelActions = unionize({
     RESET_PROCESS_PANEL: ofType<{}>(),
@@ -41,40 +39,44 @@ export type ProcessPanelAction = UnionOf<typeof processPanelActions>;
 
 export const toggleProcessPanelFilter = processPanelActions.TOGGLE_PROCESS_PANEL_FILTER;
 
-export const loadProcessPanel = (uuid: string) =>
-    async (dispatch: Dispatch) => {
-        dispatch(processPanelActions.RESET_PROCESS_PANEL());
-        dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
-        dispatch<ProcessPanelAction>(processPanelActions.SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID(uuid));
-        await dispatch<any>(loadProcess(uuid));
-        dispatch(initProcessPanelFilters);
-        dispatch<any>(initProcessLogsPanel(uuid));
-        dispatch<any>(loadSubprocessPanel());
-    };
+export const loadProcessPanel = (uuid: string) => async (dispatch: Dispatch, getState: () => RootState) => {
+    // Reset subprocess data explorer if navigating to new process
+    //  Avoids resetting pagination when refreshing same process
+    if (getState().processPanel.containerRequestUuid !== uuid) {
+        dispatch(subprocessPanelActions.CLEAR());
+    }
+    dispatch(processPanelActions.RESET_PROCESS_PANEL());
+    dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
+    dispatch<ProcessPanelAction>(processPanelActions.SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID(uuid));
+    await dispatch<any>(loadProcess(uuid));
+    dispatch(initProcessPanelFilters);
+    dispatch<any>(initProcessLogsPanel(uuid));
+    dispatch<any>(loadSubprocessPanel());
+};
 
-export const navigateToOutput = (uuid: string) =>
-    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
-        try {
-            await services.collectionService.get(uuid);
-            dispatch<any>(navigateTo(uuid));
-        } catch {
-            dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This collection does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
-        }
-    };
+export const navigateToOutput = (resource: ContextMenuResource | ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+    try {
+        await services.collectionService.get(resource.outputUuid || '');
+        dispatch<any>(navigateTo(resource.outputUuid || ''));
+    } catch {
+        dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Output collection was trashed or deleted.", hideDuration: 4000, kind: SnackbarKind.WARNING }));
+    }
+};
 
-export const loadInputs = (containerRequest: ContainerRequestResource) =>
-    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+export const loadInputs =
+    (containerRequest: ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_RAW(getRawInputs(containerRequest)));
         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_PARAMS(formatInputData(getInputs(containerRequest), getState().auth)));
     };
 
-export const loadOutputs = (containerRequest: ContainerRequestResource) =>
-    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+export const loadOutputs =
+    (containerRequest: ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         const noOutputs = { rawOutputs: {} };
+
         if (!containerRequest.outputUuid) {
-            dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
+            dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({ uuid: containerRequest.uuid, outputRaw: noOutputs }));
             return;
-        };
+        }
         try {
             const propsOutputs = getRawOutputs(containerRequest);
             const filesPromise = services.collectionService.files(containerRequest.outputUuid);
@@ -83,48 +85,53 @@ export const loadOutputs = (containerRequest: ContainerRequestResource) =>
 
             // If has propsOutput, skip fetching cwl.output.json
             if (propsOutputs !== undefined) {
-                dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({
-                    rawOutputs: propsOutputs,
-                    pdh: collection.portableDataHash
-                }));
+                dispatch<ProcessPanelAction>(
+                    processPanelActions.SET_OUTPUT_RAW({
+                        rawOutputs: propsOutputs,
+                        pdh: collection.portableDataHash,
+                    })
+                );
             } else {
                 // Fetch outputs from keep
-                const outputFile = files.find((file) => file.name === 'cwl.output.json') as CollectionFile | undefined;
+                const outputFile = files.find(file => file.name === "cwl.output.json") as CollectionFile | undefined;
                 let outputData = outputFile ? await services.collectionService.getFileContents(outputFile) : undefined;
                 if (outputData && (outputData = JSON.parse(outputData)) && collection.portableDataHash) {
-                    dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({
-                        rawOutputs: outputData,
-                        pdh: collection.portableDataHash,
-                    }));
+                    dispatch<ProcessPanelAction>(
+                        processPanelActions.SET_OUTPUT_RAW({
+                            uuid: containerRequest.uuid,
+                            outputRaw: { rawOutputs: outputData, pdh: collection.portableDataHash },
+                        })
+                    );
                 } else {
-                    dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
+                    dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({ uuid: containerRequest.uuid, outputRaw: noOutputs }));
                 }
             }
         } catch {
-            dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
+            dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({ uuid: containerRequest.uuid, outputRaw: noOutputs }));
         }
     };
 
-
-export const loadNodeJson = (containerRequest: ContainerRequestResource) =>
-    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+export const loadNodeJson =
+    (containerRequest: ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         const noLog = { nodeInfo: null };
         if (!containerRequest.logUuid) {
             dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
             return;
-        };
+        }
         try {
             const filesPromise = services.collectionService.files(containerRequest.logUuid);
             const collectionPromise = services.collectionService.get(containerRequest.logUuid);
-            const [files, collection] = await Promise.all([filesPromise, collectionPromise]);
+            const [files] = await Promise.all([filesPromise, collectionPromise]);
 
             // Fetch node.json from keep
-            const nodeFile = files.find((file) => file.name === 'node.json') as CollectionFile | undefined;
+            const nodeFile = files.find(file => file.name === "node.json") as CollectionFile | undefined;
             let nodeData = nodeFile ? await services.collectionService.getFileContents(nodeFile) : undefined;
             if (nodeData && (nodeData = JSON.parse(nodeData))) {
-                dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO({
-                    nodeInfo: nodeData as NodeInstanceType
-                }));
+                dispatch<ProcessPanelAction>(
+                    processPanelActions.SET_NODE_INFO({
+                        nodeInfo: nodeData as NodeInstanceType,
+                    })
+                );
             } else {
                 dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
             }
@@ -133,28 +140,27 @@ export const loadNodeJson = (containerRequest: ContainerRequestResource) =>
         }
     };
 
-export const loadOutputDefinitions = (containerRequest: ContainerRequestResource) =>
-    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+export const loadOutputDefinitions =
+    (containerRequest: ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         if (containerRequest && containerRequest.mounts) {
             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_DEFINITIONS(getOutputParameters(containerRequest)));
         }
     };
 
-export const updateOutputParams = () =>
-    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
-        const outputDefinitions = getState().processPanel.outputDefinitions;
-        const outputRaw = getState().processPanel.outputRaw;
+export const updateOutputParams = () => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+    const outputDefinitions = getState().processPanel.outputDefinitions;
+    const outputRaw = getState().processPanel.outputRaw;
 
-        if (outputRaw !== null && outputRaw.rawOutputs) {
-            dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_PARAMS(formatOutputData(outputDefinitions, outputRaw.rawOutputs, outputRaw.pdh, getState().auth)));
-        }
-    };
+    if (outputRaw && outputRaw.rawOutputs) {
+        dispatch<ProcessPanelAction>(
+            processPanelActions.SET_OUTPUT_PARAMS(formatOutputData(outputDefinitions, outputRaw.rawOutputs, outputRaw.pdh, getState().auth))
+        );
+    }
+};
 
-export const openWorkflow = (uuid: string) =>
-    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        dispatch<any>(navigateToWorkflows);
-        dispatch<any>(showWorkflowDetails(uuid));
-    };
+export const openWorkflow = (uuid: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+    dispatch<any>(navigateTo(uuid));
+};
 
 export const initProcessPanelFilters = processPanelActions.SET_PROCESS_PANEL_FILTERS([
     ProcessStatus.QUEUED,
@@ -164,25 +170,30 @@ export const initProcessPanelFilters = processPanelActions.SET_PROCESS_PANEL_FIL
     ProcessStatus.ONHOLD,
     ProcessStatus.FAILING,
     ProcessStatus.WARNING,
-    ProcessStatus.CANCELLED
+    ProcessStatus.CANCELLED,
 ]);
 
-const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
+export const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
     return inputs.map(input => {
         return {
             id: getIOParamId(input),
             label: input.label || "",
-            value: getIOParamDisplayValue(auth, input)
+            value: getIOParamDisplayValue(auth, input),
         };
     });
 };
 
-const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
+export const formatOutputData = (
+    definitions: CommandOutputParameter[],
+    values: any,
+    pdh: string | undefined,
+    auth: AuthState
+): ProcessIOParameter[] => {
     return definitions.map(output => {
         return {
             id: getIOParamId(output),
             label: output.label || "",
-            value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
+            value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh),
         };
     });
 };