Merge branch '16680-expired-token-handling'
[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 { memoize } from 'lodash/fp';
7 import {
8     isRequiredInput,
9     FileCommandInputParameter,
10     File,
11     CWLType
12 } from '~/models/workflow';
13 import { Field } from 'redux-form';
14 import { ERROR_MESSAGE } from '~/validators/require';
15 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button } from '@material-ui/core';
16 import { GenericInputProps, GenericInput } from './generic-input';
17 import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
18 import { connect, DispatchProp } from 'react-redux';
19 import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
20 import { TreeItem } from '~/components/tree/tree';
21 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
22 import { CollectionFile, CollectionFileType } from '~/models/collection-file';
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={format}
33         parse={parse}
34         validate={getValidation(input)} />;
35
36 const format = (value?: File) => value ? value.basename : '';
37
38 const parse = (file: CollectionFile): File => ({
39     class: CWLType.FILE,
40     location: `keep:${file.id}`,
41     basename: file.name,
42 });
43
44 const getValidation = memoize(
45     (input: FileCommandInputParameter) => ([
46         isRequiredInput(input)
47             ? (file?: File) => file ? undefined : ERROR_MESSAGE
48             : () => undefined,
49     ]));
50
51 interface FileInputComponentState {
52     open: boolean;
53     file?: CollectionFile;
54 }
55
56 const FileInputComponent = connect()(
57     class FileInputComponent extends React.Component<GenericInputProps & DispatchProp, FileInputComponentState> {
58         state: FileInputComponentState = {
59             open: false,
60         };
61
62         componentDidMount() {
63             this.props.dispatch<any>(
64                 initProjectsTreePicker(this.props.commandInput.id));
65         }
66
67         render() {
68             return <>
69                 {this.renderInput()}
70                 {this.renderDialog()}
71             </>;
72         }
73
74         openDialog = () => {
75             this.setState({ open: true });
76         }
77
78         closeDialog = () => {
79             this.setState({ open: false });
80         }
81
82         submit = () => {
83             this.closeDialog();
84             this.props.input.onChange(this.state.file);
85         }
86
87         setFile = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
88             if ('type' in data && data.type === CollectionFileType.FILE) {
89                 this.setState({ file: data });
90             } else {
91                 this.setState({ file: undefined });
92             }
93         }
94
95         renderInput() {
96             return <GenericInput
97                 component={props =>
98                     <Input
99                         readOnly
100                         fullWidth
101                         disabled={props.commandInput.disabled}
102                         value={props.input.value}
103                         error={props.meta.touched && !!props.meta.error}
104                         onClick={!props.commandInput.disabled ? this.openDialog : undefined}
105                         onKeyPress={!props.commandInput.disabled ? this.openDialog : undefined} />}
106                 {...this.props} />;
107         }
108
109         renderDialog() {
110             return <Dialog
111                 open={this.state.open}
112                 onClose={this.closeDialog}
113                 fullWidth
114                 maxWidth='md'>
115                 <DialogTitle>Choose a file</DialogTitle>
116                 <DialogContent>
117                     <ProjectsTreePicker
118                         pickerId={this.props.commandInput.id}
119                         includeCollections
120                         includeFiles
121                         toggleItemActive={this.setFile} />
122                 </DialogContent>
123                 <DialogActions>
124                     <Button onClick={this.closeDialog}>Cancel</Button>
125                     <Button
126                         disabled={!this.state.file}
127                         variant='contained'
128                         color='primary'
129                         onClick={this.submit}>Ok</Button>
130                 </DialogActions>
131             </Dialog>;
132         }
133
134     });
135
136