Add login in/out handling, fix async validation
[arvados-workbench2.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     processOwnerUuid: string;
10     currentStep: number;
11     isStepChanged: boolean;
12     workflows: WorkflowResource[];
13     searchWorkflows: WorkflowResource[];
14     selectedWorkflow: WorkflowResource | undefined;
15     inputs: CommandInputParameter[];
16 }
17
18 const initialState: RunProcessPanel = {
19     processOwnerUuid: '',
20     currentStep: 0,
21     isStepChanged: false,
22     workflows: [],
23     selectedWorkflow: undefined,
24     inputs: [],
25     searchWorkflows: [],
26 };
27
28 export const runProcessPanelReducer = (state = initialState, action: RunProcessPanelAction): RunProcessPanel =>
29     runProcessPanelActions.match(action, {
30         SET_PROCESS_OWNER_UUID: processOwnerUuid => ({ ...state, processOwnerUuid }),
31         SET_CURRENT_STEP: currentStep => ({ ...state, currentStep }),
32         SET_STEP_CHANGED: isStepChanged => ({ ...state, isStepChanged }),
33         SET_SELECTED_WORKFLOW: selectedWorkflow => ({
34             ...state,
35             selectedWorkflow,
36             inputs: getWorkflowInputs(parseWorkflowDefinition(selectedWorkflow)) || [],
37         }),
38         SET_WORKFLOWS: workflows => ({ ...state, workflows, searchWorkflows: workflows }),
39         SEARCH_WORKFLOWS: term => {
40             const termRegex = new RegExp(term, 'i');
41             return {
42                 ...state,
43                 searchWorkflows: state.workflows.filter(workflow => termRegex.test(workflow.name)),
44             };
45         },
46         RESET_RUN_PROCESS_PANEL: () => ({ ...initialState, processOwnerUuid: state.processOwnerUuid }),
47         default: () => state
48     });