Extract FileInput's format 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={(file: CollectionFile): File => ({
33             class: CWLType.FILE,
34             location: `keep:${file.id}`,
35             basename: file.name,
36         })}
37         validate={[
38             isRequiredInput(input)
39                 ? (file?: File) => file ? undefined : ERROR_MESSAGE
40                 : () => undefined,
41         ]} />;
42
43 const format = (value?: File) => value ? value.basename : '';
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                         disabled={props.commandInput.disabled}
96                         value={props.input.value}
97                         error={props.meta.touched && !!props.meta.error}
98                         onClick={!props.commandInput.disabled ? this.openDialog : undefined}
99                         onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
100                 {...this.props} />;
101         }
102
103         renderDialog() {
104             return <Dialog
105                 open={this.state.open}
106                 onClose={this.closeDialog}
107                 fullWidth
108                 maxWidth='md'>
109                 <DialogTitle>Choose a file</DialogTitle>
110                 <DialogContent>
111                     <ProjectsTreePicker
112                         pickerId={this.props.commandInput.id}
113                         includeCollections
114                         includeFiles
115                         toggleItemActive={this.setFile} />
116                 </DialogContent>
117                 <DialogActions>
118                     <Button onClick={this.closeDialog}>Cancel</Button>
119                     <Button
120                         disabled={!this.state.file}
121                         variant='contained'
122                         color='primary'
123                         onClick={this.submit}>Ok</Button>
124                 </DialogActions>
125             </Dialog>;
126         }
127
128     });
129
130