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