Merge branch '21764-project-picker-crash' into main. Closes #21764
[arvados.git] / services / workbench2 / src / views / run-process-panel / run-process-inputs-form.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { reduxForm, InjectedFormProps } from 'redux-form';
7 import { CommandInputParameter, CWLType, IntCommandInputParameter, BooleanCommandInputParameter, FileCommandInputParameter, DirectoryCommandInputParameter, DirectoryArrayCommandInputParameter, FloatArrayCommandInputParameter, IntArrayCommandInputParameter } from 'models/workflow';
8 import { IntInput } from 'views/run-process-panel/inputs/int-input';
9 import { StringInput } from 'views/run-process-panel/inputs/string-input';
10 import { StringCommandInputParameter, FloatCommandInputParameter, isPrimitiveOfType, WorkflowInputsData, EnumCommandInputParameter, isArrayOfType, StringArrayCommandInputParameter, FileArrayCommandInputParameter, getEnumType } from '../../models/workflow';
11 import { FloatInput } from 'views/run-process-panel/inputs/float-input';
12 import { BooleanInput } from './inputs/boolean-input';
13 import { FileInput } from './inputs/file-input';
14 import { connect } from 'react-redux';
15 import { compose } from 'redux';
16 import { Grid, StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core';
17 import { EnumInput } from './inputs/enum-input';
18 import { DirectoryInput } from './inputs/directory-input';
19 import { StringArrayInput } from './inputs/string-array-input';
20 import { createStructuredSelector, createSelector } from 'reselect';
21 import { FileArrayInput } from './inputs/file-array-input';
22 import { DirectoryArrayInput } from './inputs/directory-array-input';
23 import { FloatArrayInput } from './inputs/float-array-input';
24 import { IntArrayInput } from './inputs/int-array-input';
25
26 export const RUN_PROCESS_INPUTS_FORM = 'runProcessInputsForm';
27
28 export interface RunProcessInputFormProps {
29     inputs: CommandInputParameter[];
30 }
31
32 const inputsSelector = (props: RunProcessInputFormProps) =>
33     props.inputs;
34
35 const initialValuesSelector = createSelector(
36     inputsSelector,
37     inputs => inputs.reduce(
38         (values, input) => ({ ...values, [input.id]: input.value || input.default }),
39         {}));
40
41 const propsSelector = createStructuredSelector({
42     initialValues: initialValuesSelector,
43 });
44
45 const mapStateToProps = (_: any, props: RunProcessInputFormProps) =>
46     propsSelector(props);
47
48 export const RunProcessInputsForm = compose(
49     connect(mapStateToProps),
50     reduxForm<WorkflowInputsData, RunProcessInputFormProps>({
51         form: RUN_PROCESS_INPUTS_FORM
52     }))(
53         (props: InjectedFormProps & RunProcessInputFormProps) =>
54             <form>
55                 <Grid container spacing={32}>
56                     {props.inputs.map(input =>
57                         <InputItem input={input} key={input.id} />)}
58                 </Grid>
59             </form>);
60
61 type CssRules = 'inputItem';
62
63 const styles: StyleRulesCallback<CssRules> = theme => ({
64     inputItem: {
65         marginBottom: theme.spacing.unit * 2,
66     }
67 });
68
69 const InputItem = withStyles(styles)(
70     (props: WithStyles<CssRules> & { input: CommandInputParameter }) =>
71         <Grid item xs={12} md={6} className={props.classes.inputItem}>
72             {getInputComponent(props.input)}
73         </Grid>);
74
75 const getInputComponent = (input: CommandInputParameter) => {
76     switch (true) {
77         case isPrimitiveOfType(input, CWLType.BOOLEAN):
78             return <BooleanInput input={input as BooleanCommandInputParameter} />;
79
80         case isPrimitiveOfType(input, CWLType.INT):
81         case isPrimitiveOfType(input, CWLType.LONG):
82             return <IntInput input={input as IntCommandInputParameter} />;
83
84         case isPrimitiveOfType(input, CWLType.FLOAT):
85         case isPrimitiveOfType(input, CWLType.DOUBLE):
86             return <FloatInput input={input as FloatCommandInputParameter} />;
87
88         case isPrimitiveOfType(input, CWLType.STRING):
89             return <StringInput input={input as StringCommandInputParameter} />;
90
91         case isPrimitiveOfType(input, CWLType.FILE):
92             return <FileInput options={{ showOnlyOwned: false, showOnlyWritable: false }} input={input as FileCommandInputParameter} />;
93
94         case isPrimitiveOfType(input, CWLType.DIRECTORY):
95             return <DirectoryInput options={{ showOnlyOwned: false, showOnlyWritable: false }} input={input as DirectoryCommandInputParameter} />;
96
97         case getEnumType(input) !== null:
98             return <EnumInput input={input as EnumCommandInputParameter} />;
99
100         case isArrayOfType(input, CWLType.STRING):
101             return <StringArrayInput input={input as StringArrayCommandInputParameter} />;
102
103         case isArrayOfType(input, CWLType.INT):
104         case isArrayOfType(input, CWLType.LONG):
105             return <IntArrayInput input={input as IntArrayCommandInputParameter} />;
106
107         case isArrayOfType(input, CWLType.FLOAT):
108         case isArrayOfType(input, CWLType.DOUBLE):
109             return <FloatArrayInput input={input as FloatArrayCommandInputParameter} />;
110
111         case isArrayOfType(input, CWLType.FILE):
112             return <FileArrayInput options={{ showOnlyOwned: false, showOnlyWritable: false }} input={input as FileArrayCommandInputParameter} />;
113
114         case isArrayOfType(input, CWLType.DIRECTORY):
115             return <DirectoryArrayInput options={{ showOnlyOwned: false, showOnlyWritable: false }} input={input as DirectoryArrayCommandInputParameter} />;
116
117         default:
118             return null;
119     }
120 };