Merge branch 'master' into 14452-my-account
[arvados-workbench2.git] / src / views / run-process-panel / inputs / file-input.tsx
index 7c4402fecd30432ec89441bbb3b11b68a89d21a3..0611100722a2e30512a675dc770b933d34cd88b6 100644 (file)
@@ -3,17 +3,23 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
+import { memoize } from 'lodash/fp';
 import {
-    getInputLabel,
     isRequiredInput,
     FileCommandInputParameter,
     File,
     CWLType
 } from '~/models/workflow';
 import { Field } from 'redux-form';
-import { require } from '~/validators/require';
-import { Input } from '@material-ui/core';
+import { ERROR_MESSAGE } from '~/validators/require';
+import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button } from '@material-ui/core';
 import { GenericInputProps, GenericInput } from './generic-input';
+import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
+import { connect, DispatchProp } from 'react-redux';
+import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
+import { TreeItem } from '~/components/tree/tree';
+import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
+import { CollectionFile, CollectionFileType } from '~/models/collection-file';
 
 export interface FileInputProps {
     input: FileCommandInputParameter;
@@ -21,22 +27,110 @@ export interface FileInputProps {
 export const FileInput = ({ input }: FileInputProps) =>
     <Field
         name={input.id}
-        commandInput={input}        
+        commandInput={input}
         component={FileInputComponent}
-        format={(value?: File) => value ? value.location : ''}
-        parse={(value: string): File => ({
-            class: CWLType.FILE,
-            location: value,
-            basename: value.split('/').slice(1).join('/')
-        })}
-        validate={[
-            isRequiredInput(input)
-                ? require
-                : () => undefined,
-        ]} />;
-
-const FileInputComponent = (props: GenericInputProps) =>
-    <GenericInput
-        component={props =>
-            <Input readOnly fullWidth value={props.input.value} error={props.meta.touched && !!props.meta.error}/>}
-        {...props} />;
+        format={format}
+        parse={parse}
+        validate={getValidation(input)} />;
+
+const format = (value?: File) => value ? value.basename : '';
+
+const parse = (file: CollectionFile): File => ({
+    class: CWLType.FILE,
+    location: `keep:${file.id}`,
+    basename: file.name,
+});
+
+const getValidation = memoize(
+    (input: FileCommandInputParameter) => ([
+        isRequiredInput(input)
+            ? (file?: File) => file ? undefined : ERROR_MESSAGE
+            : () => undefined,
+    ]));
+
+interface FileInputComponentState {
+    open: boolean;
+    file?: CollectionFile;
+}
+
+const FileInputComponent = connect()(
+    class FileInputComponent extends React.Component<GenericInputProps & DispatchProp, FileInputComponentState> {
+        state: FileInputComponentState = {
+            open: false,
+        };
+
+        componentDidMount() {
+            this.props.dispatch<any>(
+                initProjectsTreePicker(this.props.commandInput.id));
+        }
+
+        render() {
+            return <>
+                {this.renderInput()}
+                {this.renderDialog()}
+            </>;
+        }
+
+        openDialog = () => {
+            this.setState({ open: true });
+        }
+
+        closeDialog = () => {
+            this.setState({ open: false });
+        }
+
+        submit = () => {
+            this.closeDialog();
+            this.props.input.onChange(this.state.file);
+        }
+
+        setFile = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
+            if ('type' in data && data.type === CollectionFileType.FILE) {
+                this.setState({ file: data });
+            } else {
+                this.setState({ file: undefined });
+            }
+        }
+
+        renderInput() {
+            return <GenericInput
+                component={props =>
+                    <Input
+                        readOnly
+                        fullWidth
+                        disabled={props.commandInput.disabled}
+                        value={props.input.value}
+                        error={props.meta.touched && !!props.meta.error}
+                        onClick={!props.commandInput.disabled ? this.openDialog : undefined}
+                        onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
+                {...this.props} />;
+        }
+
+        renderDialog() {
+            return <Dialog
+                open={this.state.open}
+                onClose={this.closeDialog}
+                fullWidth
+                maxWidth='md'>
+                <DialogTitle>Choose a file</DialogTitle>
+                <DialogContent>
+                    <ProjectsTreePicker
+                        pickerId={this.props.commandInput.id}
+                        includeCollections
+                        includeFiles
+                        toggleItemActive={this.setFile} />
+                </DialogContent>
+                <DialogActions>
+                    <Button onClick={this.closeDialog}>Cancel</Button>
+                    <Button
+                        disabled={!this.state.file}
+                        variant='contained'
+                        color='primary'
+                        onClick={this.submit}>Ok</Button>
+                </DialogActions>
+            </Dialog>;
+        }
+
+    });
+
+