21336: Add type checking to output data reducer instead of accepting any, fix
[arvados.git] / services / workbench2 / src / store / process-panel / process-panel-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { OutputDetails, ProcessPanel } from "store/process-panel/process-panel";
6 import { ProcessPanelAction, processPanelActions } from "store/process-panel/process-panel-actions";
7
8 const initialState: ProcessPanel = {
9     containerRequestUuid: "",
10     filters: {},
11     inputRaw: null,
12     inputParams: null,
13     outputData: null,
14     nodeInfo: null,
15     outputDefinitions: [],
16     outputParams: null,
17 };
18
19 export type OutputDataUpdate = {
20     uuid: string;
21     payload: OutputDetails;
22 };
23
24 export const processPanelReducer = (state = initialState, action: ProcessPanelAction): ProcessPanel =>
25     processPanelActions.match(action, {
26         RESET_PROCESS_PANEL: () => initialState,
27         SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID: containerRequestUuid => ({
28             ...state,
29             containerRequestUuid,
30         }),
31         SET_PROCESS_PANEL_FILTERS: statuses => {
32             const filters = statuses.reduce((filters, status) => ({ ...filters, [status]: true }), {});
33             return { ...state, filters };
34         },
35         TOGGLE_PROCESS_PANEL_FILTER: status => {
36             const filters = { ...state.filters, [status]: !state.filters[status] };
37             return { ...state, filters };
38         },
39         SET_INPUT_RAW: inputRaw => {
40             // Since mounts can disappear and reappear, only set inputs
41             //   if current state is null or new inputs has content
42             if (state.inputRaw === null || (inputRaw && Object.keys(inputRaw).length)) {
43                 return { ...state, inputRaw };
44             } else {
45                 return state;
46             }
47         },
48         SET_INPUT_PARAMS: inputParams => {
49             // Since mounts can disappear and reappear, only set inputs
50             //   if current state is null or new inputs has content
51             if (state.inputParams === null || (inputParams && inputParams.length)) {
52                 return { ...state, inputParams };
53             } else {
54                 return state;
55             }
56         },
57         SET_OUTPUT_DATA: (update: OutputDataUpdate) => {
58             //never set output to {} unless initializing
59             if (state.outputData?.raw && Object.keys(state.outputData?.raw).length && state.containerRequestUuid === update.uuid) {
60                 return state;
61             }
62             return { ...state, outputData: update.payload };
63         },
64         SET_NODE_INFO: ({ nodeInfo }) => {
65             return { ...state, nodeInfo };
66         },
67         SET_OUTPUT_DEFINITIONS: outputDefinitions => {
68             // Set output definitions is only additive to avoid clearing when mounts go temporarily missing
69             if (outputDefinitions.length) {
70                 return { ...state, outputDefinitions };
71             } else {
72                 return state;
73             }
74         },
75         SET_OUTPUT_PARAMS: outputParams => {
76             return { ...state, outputParams };
77         },
78         default: () => state,
79     });