Extract FileInput's parse function
[arvados-workbench2.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
23 export interface FileInputProps {
24     input: FileCommandInputParameter;
25 }
26 export const FileInput = ({ input }: FileInputProps) =>
27     <Field
28         name={input.id}
29         commandInput={input}
30         component={FileInputComponent}
31         format={format}
32         parse={parse}
33         validate={[
34             isRequiredInput(input)
35                 ? (file?: File) => file ? undefined : ERROR_MESSAGE
36                 : () => undefined,
37         ]} />;
38
39 const format = (value?: File) => value ? value.basename : '';
40
41 const parse = (file: CollectionFile): File => ({
42     class: CWLType.FILE,
43     location: `keep:${file.id}`,
44     basename: file.name,
45 });
46
47 interface FileInputComponentState {
48     open: boolean;
49     file?: CollectionFile;
50 }
51
52 const FileInputComponent = connect()(
53     class FileInputComponent extends React.Component<GenericInputProps & DispatchProp, FileInputComponentState> {
54         state: FileInputComponentState = {
55             open: false,
56         };
57
58         componentDidMount() {
59             this.props.dispatch<any>(
60                 initProjectsTreePicker(this.props.commandInput.id));
61         }
62
63         render() {
64             return <>
65                 {this.renderInput()}
66                 {this.renderDialog()}
67             </>;
68         }
69
70         openDialog = () => {
71             this.setState({ open: true });
72         }
73
74         closeDialog = () => {
75             this.setState({ open: false });
76         }
77
78         submit = () => {
79             this.closeDialog();
80             this.props.input.onChange(this.state.file);
81         }
82
83         setFile = (event: React.MouseEvent<HTMLElement>, { data }: TreeItem<ProjectsTreePickerItem>, pickerId: string) => {
84             if ('type' in data && data.type === CollectionFileType.FILE) {
85                 this.setState({ file: data });
86             } else {
87                 this.setState({ file: undefined });
88             }
89         }
90
91         renderInput() {
92             return <GenericInput
93                 component={props =>
94                     <Input
95                         readOnly
96                         fullWidth
97                         disabled={props.commandInput.disabled}
98                         value={props.input.value}
99                         error={props.meta.touched && !!props.meta.error}
100                         onClick={!props.commandInput.disabled ? this.openDialog : undefined}
101                         onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
102                 {...this.props} />;
103         }
104
105         renderDialog() {
106             return <Dialog
107                 open={this.state.open}
108                 onClose={this.closeDialog}
109                 fullWidth
110                 maxWidth='md'>
111                 <DialogTitle>Choose a file</DialogTitle>
112                 <DialogContent>
113                     <ProjectsTreePicker
114                         pickerId={this.props.commandInput.id}
115                         includeCollections
116                         includeFiles
117                         toggleItemActive={this.setFile} />
118                 </DialogContent>
119                 <DialogActions>
120                     <Button onClick={this.closeDialog}>Cancel</Button>
121                     <Button
122                         disabled={!this.state.file}
123                         variant='contained'
124                         color='primary'
125                         onClick={this.submit}>Ok</Button>
126                 </DialogActions>
127             </Dialog>;
128         }
129
130     });
131
132