Create directory-array-input
[arvados.git] / src / views / run-process-panel / inputs / directory-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     DirectoryCommandInputParameter,
9     CWLType,
10     Directory
11 } from '~/models/workflow';
12 import { Field } from 'redux-form';
13 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button } from '@material-ui/core';
14 import { GenericInputProps, GenericInput } from './generic-input';
15 import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
16 import { connect, DispatchProp } from 'react-redux';
17 import { initProjectsTreePicker } from '~/store/tree-picker/tree-picker-actions';
18 import { TreeItem } from '~/components/tree/tree';
19 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
20 import { CollectionResource } from '~/models/collection';
21 import { ResourceKind } from '~/models/resource';
22 import { ERROR_MESSAGE } from '../../../validators/require';
23
24 export interface DirectoryInputProps {
25     input: DirectoryCommandInputParameter;
26 }
27 export const DirectoryInput = ({ input }: DirectoryInputProps) =>
28     <Field
29         name={input.id}
30         commandInput={input}
31         component={DirectoryInputComponent}
32         format={(value?: Directory) => value ? value.basename : ''}
33         parse={(directory: CollectionResource): Directory => ({
34             class: CWLType.DIRECTORY,
35             location: `keep:${directory.portableDataHash}`,
36             basename: directory.name,
37         })}
38         validate={[
39             isRequiredInput(input)
40                 ? (directory?: Directory) => directory ? undefined : ERROR_MESSAGE
41                 : () => undefined,
42         ]} />;
43
44
45 interface DirectoryInputComponentState {
46     open: boolean;
47     directory?: CollectionResource;
48 }
49
50 const DirectoryInputComponent = connect()(
51     class FileInputComponent extends React.Component<GenericInputProps & DispatchProp, DirectoryInputComponentState> {
52         state: DirectoryInputComponentState = {
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.directory);
79         }
80
81         setDirectory = (event: React.MouseEvent<HTMLElement>, { data }: TreeItem<ProjectsTreePickerItem>, pickerId: string) => {
82             if ('kind' in data && data.kind === ResourceKind.COLLECTION) {
83                 this.setState({ directory: data });
84             } else {
85                 this.setState({ directory: 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 directory</DialogTitle>
109                 <DialogContent>
110                     <ProjectsTreePicker
111                         pickerId={this.props.commandInput.id}
112                         includeCollections
113                         toggleItemActive={this.setDirectory} />
114                 </DialogContent>
115                 <DialogActions>
116                     <Button onClick={this.closeDialog}>Cancel</Button>
117                     <Button
118                         disabled={!this.state.directory}
119                         variant='contained'
120                         color='primary'
121                         onClick={this.submit}>Ok</Button>
122                 </DialogActions>
123             </Dialog>;
124         }
125
126     });
127
128