Merge chips input
[arvados.git] / src / views / run-process-panel / inputs / file-input.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import {
7     isRequiredInput,
8     FileCommandInputParameter,
9     File,
10     CWLType
11 } from '~/models/workflow';
12 import { Field } from 'redux-form';
13 import { ERROR_MESSAGE } from '~/validators/require';
14 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button } from '@material-ui/core';
15 import { GenericInputProps, GenericInput } from './generic-input';
16 import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
17 import { connect, DispatchProp } from 'react-redux';
18 import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
19 import { TreeItem } from '~/components/tree/tree';
20 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
21 import { CollectionFile, CollectionFileType } from '~/models/collection-file';
22 import { getFileFullPath } from '~/services/collection-service/collection-service-files-response';
23
24 export interface FileInputProps {
25     input: FileCommandInputParameter;
26 }
27 export const FileInput = ({ input }: FileInputProps) =>
28     <Field
29         name={input.id}
30         commandInput={input}
31         component={FileInputComponent}
32         format={(value?: File) => value ? value.basename : ''}
33         parse={(file: CollectionFile): File => ({
34             class: CWLType.FILE,
35             location: `keep:${file.id}`,
36             basename: file.name,
37         })}
38         validate={[
39             isRequiredInput(input)
40                 ? (file?: File) => file ? undefined : ERROR_MESSAGE
41                 : () => undefined,
42         ]} />;
43
44
45 interface FileInputComponentState {
46     open: boolean;
47     file?: CollectionFile;
48 }
49
50 const FileInputComponent = connect()(
51     class FileInputComponent extends React.Component<GenericInputProps & DispatchProp, FileInputComponentState> {
52         state: FileInputComponentState = {
53             open: false,
54         };
55
56         componentDidMount() {
57             this.props.dispatch<any>(
58                 initProjectsTreePicker(this.props.commandInput.id));
59         }
60
61         render() {
62             return <>
63                 {this.renderInput()}
64                 {this.renderDialog()}
65             </>;
66         }
67
68         openDialog = () => {
69             this.setState({ open: true });
70         }
71
72         closeDialog = () => {
73             this.setState({ open: false });
74         }
75
76         submit = () => {
77             this.closeDialog();
78             this.props.input.onChange(this.state.file);
79         }
80
81         setFile = (event: React.MouseEvent<HTMLElement>, { data }: TreeItem<ProjectsTreePickerItem>, pickerId: string) => {
82             if ('type' in data && data.type === CollectionFileType.FILE) {
83                 this.setState({ file: data });
84             } else {
85                 this.setState({ file: undefined });
86             }
87         }
88
89         renderInput() {
90             return <GenericInput
91                 component={props =>
92                     <Input
93                         readOnly
94                         fullWidth
95                         value={props.input.value}
96                         error={props.meta.touched && !!props.meta.error}
97                         onClick={this.openDialog}
98                         onKeyPress={this.openDialog} />}
99                 {...this.props} />;
100         }
101
102         renderDialog() {
103             return <Dialog
104                 open={this.state.open}
105                 onClose={this.closeDialog}
106                 fullWidth
107                 maxWidth='md'>
108                 <DialogTitle>Choose a file</DialogTitle>
109                 <DialogContent>
110                     <ProjectsTreePicker
111                         pickerId={this.props.commandInput.id}
112                         includeCollections
113                         includeFiles
114                         toggleItemActive={this.setFile} />
115                 </DialogContent>
116                 <DialogActions>
117                     <Button onClick={this.closeDialog}>Cancel</Button>
118                     <Button
119                         disabled={!this.state.file}
120                         variant='contained'
121                         color='primary'
122                         onClick={this.submit}>Ok</Button>
123                 </DialogActions>
124             </Dialog>;
125         }
126
127     });
128
129