21128: readonly collections good Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa.knox...
[arvados.git] / src / store / run-process-panel / run-process-panel-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { RunProcessPanelAction, runProcessPanelActions } from 'store/run-process-panel/run-process-panel-actions';
6 import { WorkflowResource, CommandInputParameter, getWorkflowInputs, parseWorkflowDefinition } from 'models/workflow';
7
8 interface RunProcessPanel {
9     processPathname: string;
10     processOwnerUuid: string;
11     currentStep: number;
12     isStepChanged: boolean;
13     workflows: WorkflowResource[];
14     searchWorkflows: WorkflowResource[];
15     selectedWorkflow: WorkflowResource | undefined;
16     presets?: WorkflowResource[];
17     selectedPreset?: WorkflowResource;
18     inputs: CommandInputParameter[];
19 }
20
21 const initialState: RunProcessPanel = {
22     processPathname: '',
23     processOwnerUuid: '',
24     currentStep: 0,
25     isStepChanged: false,
26     workflows: [],
27     selectedWorkflow: undefined,
28     inputs: [],
29     searchWorkflows: [],
30 };
31
32 export const runProcessPanelReducer = (state = initialState, action: RunProcessPanelAction): RunProcessPanel =>
33     runProcessPanelActions.match(action, {
34         SET_PROCESS_PATHNAME: processPathname => ({ ...state, processPathname }),
35         SET_PROCESS_OWNER_UUID: processOwnerUuid => ({ ...state, processOwnerUuid }),
36         SET_CURRENT_STEP: currentStep => ({ ...state, currentStep }),
37         SET_STEP_CHANGED: isStepChanged => ({ ...state, isStepChanged }),
38         SET_SELECTED_WORKFLOW: selectedWorkflow => ({
39             ...state,
40             selectedWorkflow,
41             presets: undefined,
42             selectedPreset: selectedWorkflow,
43             inputs: getWorkflowInputs(parseWorkflowDefinition(selectedWorkflow)) || [],
44         }),
45         SET_WORKFLOW_PRESETS: presets => ({
46             ...state,
47             presets,
48         }),
49         SELECT_WORKFLOW_PRESET: selectedPreset => ({
50             ...state,
51             selectedPreset,
52         }),
53         SET_WORKFLOWS: workflows => ({ ...state, workflows, searchWorkflows: workflows }),
54         SEARCH_WORKFLOWS: term => {
55             const termRegex = new RegExp(term, 'i');
56             return {
57                 ...state,
58                 searchWorkflows: state.workflows.filter(workflow => termRegex.test(workflow.name)),
59             };
60         },
61         RESET_RUN_PROCESS_PANEL: () => ({ ...initialState, processOwnerUuid: state.processOwnerUuid }),
62         default: () => state
63     });