16073: Refactor process io loading into actions and reducers to eliminate infinite...
[arvados-workbench2.git] / src / store / process-panel / process-panel-actions.ts
index 7d7bd4268106a54b17ebbdd511e285a5d75ba8b2..64fc493fb2ea4f5b41a207cfecf63fb294f0d51c 100644 (file)
@@ -3,7 +3,7 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { unionize, ofType, UnionOf } from "common/unionize";
-import { getRawOutputs, loadProcess } from 'store/processes/processes-actions';
+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';
@@ -16,11 +16,22 @@ import { loadSubprocessPanel } from "../subprocess-panel/subprocess-panel-action
 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 } from 'models/workflow';
+import { getIOParamDisplayValue, ProcessIOParameter } from "views/process-panel/process-io-card";
+import { OutputDetails } from "./process-panel";
+import { AuthState } from "store/auth/auth-reducer";
 
 export const processPanelActions = unionize({
+    RESET_PROCESS_PANEL: ofType<{}>(),
     SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID: ofType<string>(),
     SET_PROCESS_PANEL_FILTERS: ofType<string[]>(),
     TOGGLE_PROCESS_PANEL_FILTER: ofType<string>(),
+    SET_INPUT_RAW: ofType<CommandInputParameter[] | null>(),
+    SET_INPUT_PARAMS: ofType<ProcessIOParameter[] | null>(),
+    SET_OUTPUT_RAW: ofType<OutputDetails | null>(),
+    SET_OUTPUT_DEFINITIONS: ofType<CommandOutputParameter[]>(),
+    SET_OUTPUT_PARAMS: ofType<ProcessIOParameter[] | null>(),
 });
 
 export type ProcessPanelAction = UnionOf<typeof processPanelActions>;
@@ -29,6 +40,7 @@ export const toggleProcessPanelFilter = processPanelActions.TOGGLE_PROCESS_PANEL
 
 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));
@@ -47,10 +59,19 @@ export const navigateToOutput = (uuid: string) =>
         }
     };
 
-export const loadOutputs = (containerRequest: ContainerRequestResource, setOutputs) =>
+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) => {
         const noOutputs = {rawOutputs: {}};
-        if (!containerRequest.outputUuid) {setOutputs(noOutputs); return;};
+        if (!containerRequest.outputUuid) {
+            dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
+            return;
+        };
         try {
             const propsOutputs = getRawOutputs(containerRequest);
             const filesPromise = services.collectionService.files(containerRequest.outputUuid);
@@ -59,22 +80,42 @@ export const loadOutputs = (containerRequest: ContainerRequestResource, setOutpu
 
             // If has propsOutput, skip fetching cwl.output.json
             if (propsOutputs !== undefined) {
-                setOutputs({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;
                 let outputData = outputFile ? await services.collectionService.getFileContents(outputFile) : undefined;
                 if (outputData && (outputData = JSON.parse(outputData)) && collection.portableDataHash) {
-                    setOutputs({
+                    dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({
                         rawOutputs: outputData,
                         pdh: collection.portableDataHash,
-                    });
+                    }));
                 } else {
-                    setOutputs(noOutputs);
+                    dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
                 }
             }
         } catch {
-            setOutputs(noOutputs);
+            dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
+        }
+    };
+
+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;
+
+        if (outputRaw !== null && outputRaw.rawOutputs) {
+            dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_PARAMS(formatOutputData(outputDefinitions, outputRaw.rawOutputs, outputRaw.pdh, getState().auth)));
         }
     };
 
@@ -94,3 +135,23 @@ export const initProcessPanelFilters = processPanelActions.SET_PROCESS_PANEL_FIL
     ProcessStatus.WARNING,
     ProcessStatus.CANCELLED
 ]);
+
+const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
+    return inputs.map(input => {
+        return {
+            id: getIOParamId(input),
+            label: input.label || "",
+            value: getIOParamDisplayValue(auth, input)
+        };
+    });
+};
+
+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)
+        };
+    });
+};