Improve validation
[arvados-workbench2.git] / src / views / run-process-panel / run-process-inputs-form.tsx
index 0e533206df139283184e089c9b20d707cb2e507f..9e6f5907f847279903df93ceb0c45658018bf756 100644 (file)
@@ -4,40 +4,74 @@
 
 import * as React from 'react';
 import { reduxForm, InjectedFormProps } from 'redux-form';
-import { WorkflowResource, CommandInputParameter, CWLType, IntCommandInputParameter, BooleanCommandInputParameter, FileCommandInputParameter } from '~/models/workflow';
+import { CommandInputParameter, CWLType, IntCommandInputParameter, BooleanCommandInputParameter, FileCommandInputParameter } from '~/models/workflow';
 import { IntInput } from '~/views/run-process-panel/inputs/int-input';
 import { StringInput } from '~/views/run-process-panel/inputs/string-input';
-import { StringCommandInputParameter, FloatCommandInputParameter, File } from '../../models/workflow';
+import { StringCommandInputParameter, FloatCommandInputParameter, isPrimitiveOfType, File, Directory, WorkflowInputsData } from '../../models/workflow';
 import { FloatInput } from '~/views/run-process-panel/inputs/float-input';
 import { BooleanInput } from './inputs/boolean-input';
 import { FileInput } from './inputs/file-input';
+import { connect } from 'react-redux';
+import { compose } from 'redux';
+import { Grid, StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core';
 
-const RUN_PROCESS_INPUTS_FORM = 'runProcessInputsForm';
+export const RUN_PROCESS_INPUTS_FORM = 'runProcessInputsForm';
 
 export interface RunProcessInputFormProps {
     inputs: CommandInputParameter[];
 }
 
-export const RunProcessInputsForm = reduxForm<any, RunProcessInputFormProps>({
-    form: RUN_PROCESS_INPUTS_FORM
-})((props: InjectedFormProps & RunProcessInputFormProps) =>
-    <form>
-        {props.inputs.map(input => {
-            switch (true) {
-                case input.type === CWLType.BOOLEAN:
-                    return <BooleanInput key={input.id} input={input as BooleanCommandInputParameter} />;
-                case input.type === CWLType.INT:
-                case input.type === CWLType.LONG:
-                    return <IntInput key={input.id} input={input as IntCommandInputParameter} />;
-                case input.type === CWLType.FLOAT:
-                case input.type === CWLType.DOUBLE:
-                    return <FloatInput key={input.id} input={input as FloatCommandInputParameter} />;
-                case input.type === CWLType.STRING:
-                    return <StringInput key={input.id} input={input as StringCommandInputParameter} />;
-                case input.type === CWLType.FILE:
-                    return <FileInput key={input.id} input={input as FileCommandInputParameter} />;
-                default:
-                    return null;
-            }
-        })}
-    </form>);
\ No newline at end of file
+export const RunProcessInputsForm = compose(
+    connect((_: any, props: RunProcessInputFormProps) => ({
+        initialValues: props.inputs.reduce(
+            (values, input) => ({ ...values, [input.id]: input.default }),
+            {}),
+    })),
+    reduxForm<WorkflowInputsData, RunProcessInputFormProps>({
+        form: RUN_PROCESS_INPUTS_FORM
+    }))(
+        (props: InjectedFormProps & RunProcessInputFormProps) =>
+            <form>
+                <Grid container spacing={32}>
+                    {props.inputs.map(input =>
+                        <InputItem input={input} key={input.id} />)}
+                </Grid>
+            </form>);
+
+type CssRules = 'inputItem';
+
+const styles: StyleRulesCallback<CssRules> = theme => ({
+    inputItem: {
+        marginBottom: theme.spacing.unit * 2,
+    }
+});
+
+const InputItem = withStyles(styles)(
+    (props: WithStyles<CssRules> & { input: CommandInputParameter }) =>
+        <Grid item xs={12} md={6} className={props.classes.inputItem}>
+            {getInputComponent(props.input)}
+        </Grid>);
+
+const getInputComponent = (input: CommandInputParameter) => {
+    switch (true) {
+        case isPrimitiveOfType(input, CWLType.BOOLEAN):
+            return <BooleanInput input={input as BooleanCommandInputParameter} />;
+
+        case isPrimitiveOfType(input, CWLType.INT):
+        case isPrimitiveOfType(input, CWLType.LONG):
+            return <IntInput input={input as IntCommandInputParameter} />;
+
+        case isPrimitiveOfType(input, CWLType.FLOAT):
+        case isPrimitiveOfType(input, CWLType.DOUBLE):
+            return <FloatInput input={input as FloatCommandInputParameter} />;
+
+        case isPrimitiveOfType(input, CWLType.STRING):
+            return <StringInput input={input as StringCommandInputParameter} />;
+
+        case isPrimitiveOfType(input, CWLType.FILE):
+            return <FileInput input={input as FileCommandInputParameter} />;
+
+        default:
+            return null;
+    }
+};