21720: ran codemods, updated spacing, minor layout fixes
[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 { CustomStyleRulesCallback } from 'common/custom-theme';
17 import { Grid } from '@mui/material';
18 import { WithStyles } from '@mui/styles';
19 import withStyles from '@mui/styles/withStyles';
20 import { EnumInput } from './inputs/enum-input';
21 import { DirectoryInput } from './inputs/directory-input';
22 import { StringArrayInput } from './inputs/string-array-input';
23 import { createStructuredSelector, createSelector } from 'reselect';
24 import { FileArrayInput } from './inputs/file-array-input';
25 import { DirectoryArrayInput } from './inputs/directory-array-input';
26 import { FloatArrayInput } from './inputs/float-array-input';
27 import { IntArrayInput } from './inputs/int-array-input';
28
29 export const RUN_PROCESS_INPUTS_FORM = 'runProcessInputsForm';
30
31 export interface RunProcessInputFormProps {
32     inputs: CommandInputParameter[];
33 }
34
35 const inputsSelector = (props: RunProcessInputFormProps) =>
36     props.inputs;
37
38 const initialValuesSelector = createSelector(
39     inputsSelector,
40     inputs => inputs.reduce(
41         (values, input) => ({ ...values, [input.id]: input.value || input.default }),
42         {}));
43
44 const propsSelector = createStructuredSelector({
45     initialValues: initialValuesSelector,
46 });
47
48 const mapStateToProps = (_: any, props: RunProcessInputFormProps) =>
49     propsSelector(props);
50
51 export const RunProcessInputsForm = compose(
52     connect(mapStateToProps),
53     reduxForm<WorkflowInputsData, RunProcessInputFormProps>({
54         form: RUN_PROCESS_INPUTS_FORM
55     }))(
56         (props: InjectedFormProps & RunProcessInputFormProps) =>
57             <form>
58                 <Grid container spacing={4}>
59                     {props.inputs.map(input =>
60                         <InputItem input={input} key={input.id} />)}
61                 </Grid>
62             </form>);
63
64 type CssRules = 'inputItem';
65
66 const styles: CustomStyleRulesCallback<CssRules> = theme => ({
67     inputItem: {
68         marginBottom: theme.spacing(2),
69     }
70 });
71
72 const InputItem = withStyles(styles)(
73     (props: WithStyles<CssRules> & { input: CommandInputParameter }) =>
74         <Grid item xs={12} md={6} className={props.classes.inputItem}>
75             {getInputComponent(props.input)}
76         </Grid>);
77
78 const getInputComponent = (input: CommandInputParameter) => {
79     switch (true) {
80         case isPrimitiveOfType(input, CWLType.BOOLEAN):
81             return <BooleanInput input={input as BooleanCommandInputParameter} />;
82
83         case isPrimitiveOfType(input, CWLType.INT):
84         case isPrimitiveOfType(input, CWLType.LONG):
85             return <IntInput input={input as IntCommandInputParameter} />;
86
87         case isPrimitiveOfType(input, CWLType.FLOAT):
88         case isPrimitiveOfType(input, CWLType.DOUBLE):
89             return <FloatInput input={input as FloatCommandInputParameter} />;
90
91         case isPrimitiveOfType(input, CWLType.STRING):
92             return <StringInput input={input as StringCommandInputParameter} />;
93
94         case isPrimitiveOfType(input, CWLType.FILE):
95             return <FileInput options={{ showOnlyOwned: false, showOnlyWritable: false }} input={input as FileCommandInputParameter} />;
96
97         case isPrimitiveOfType(input, CWLType.DIRECTORY):
98             return <DirectoryInput options={{ showOnlyOwned: false, showOnlyWritable: false }} input={input as DirectoryCommandInputParameter} />;
99
100         case getEnumType(input) !== null:
101             return <EnumInput input={input as EnumCommandInputParameter} />;
102
103         case isArrayOfType(input, CWLType.STRING):
104             return <StringArrayInput input={input as StringArrayCommandInputParameter} />;
105
106         case isArrayOfType(input, CWLType.INT):
107         case isArrayOfType(input, CWLType.LONG):
108             return <IntArrayInput input={input as IntArrayCommandInputParameter} />;
109
110         case isArrayOfType(input, CWLType.FLOAT):
111         case isArrayOfType(input, CWLType.DOUBLE):
112             return <FloatArrayInput input={input as FloatArrayCommandInputParameter} />;
113
114         case isArrayOfType(input, CWLType.FILE):
115             return <FileArrayInput options={{ showOnlyOwned: false, showOnlyWritable: false }} input={input as FileArrayCommandInputParameter} />;
116
117         case isArrayOfType(input, CWLType.DIRECTORY):
118             return <DirectoryArrayInput options={{ showOnlyOwned: false, showOnlyWritable: false }} input={input as DirectoryArrayCommandInputParameter} />;
119
120         default:
121             return null;
122     }
123 };