Merge branch '16159-logout-request-with-token'
[arvados-workbench2.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     options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
28 }
29 export const DirectoryInput = ({ input, options }: DirectoryInputProps) =>
30     <Field
31         name={input.id}
32         commandInput={input}
33         component={DirectoryInputComponent}
34         format={format}
35         parse={parse}
36         {...{
37             options
38         }}
39         validate={getValidation(input)} />;
40
41 const format = (value?: Directory) => value ? value.basename : '';
42
43 const parse = (directory: CollectionResource): Directory => ({
44     class: CWLType.DIRECTORY,
45     location: `keep:${directory.portableDataHash}`,
46     basename: directory.name,
47 });
48
49 const getValidation = memoize(
50     (input: DirectoryCommandInputParameter) => ([
51         isRequiredInput(input)
52             ? (directory?: Directory) => directory ? undefined : ERROR_MESSAGE
53             : () => undefined,
54     ])
55 );
56
57 interface DirectoryInputComponentState {
58     open: boolean;
59     directory?: CollectionResource;
60 }
61
62 const DirectoryInputComponent = connect()(
63     class FileInputComponent extends React.Component<GenericInputProps & DispatchProp & {
64         options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
65     }, DirectoryInputComponentState> {
66         state: DirectoryInputComponentState = {
67             open: false,
68         };
69
70         componentDidMount() {
71             this.props.dispatch<any>(
72                 initProjectsTreePicker(this.props.commandInput.id));
73         }
74
75         render() {
76             return <>
77                 {this.renderInput()}
78                 {this.renderDialog()}
79             </>;
80         }
81
82         openDialog = () => {
83             this.setState({ open: true });
84         }
85
86         closeDialog = () => {
87             this.setState({ open: false });
88         }
89
90         submit = () => {
91             this.closeDialog();
92             this.props.input.onChange(this.state.directory);
93         }
94
95         setDirectory = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
96             if ('kind' in data && data.kind === ResourceKind.COLLECTION) {
97                 this.setState({ directory: data });
98             } else {
99                 this.setState({ directory: undefined });
100             }
101         }
102
103         renderInput() {
104             return <GenericInput
105                 component={props =>
106                     <Input
107                         readOnly
108                         fullWidth
109                         value={props.input.value}
110                         error={props.meta.touched && !!props.meta.error}
111                         disabled={props.commandInput.disabled}
112                         onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
113                         onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
114                 {...this.props} />;
115         }
116
117         renderDialog() {
118             return <Dialog
119                 open={this.state.open}
120                 onClose={this.closeDialog}
121                 fullWidth
122                 data-cy="choose-a-directory-dialog"
123                 maxWidth='md'>
124                 <DialogTitle>Choose a directory</DialogTitle>
125                 <DialogContent>
126                     <ProjectsTreePicker
127                         pickerId={this.props.commandInput.id}
128                         includeCollections
129                         options={this.props.options}
130                         toggleItemActive={this.setDirectory} />
131                 </DialogContent>
132                 <DialogActions>
133                     <Button onClick={this.closeDialog}>Cancel</Button>
134                     <Button
135                         disabled={!this.state.directory}
136                         variant='contained'
137                         color='primary'
138                         onClick={this.submit}>Ok</Button>
139                 </DialogActions>
140             </Dialog>;
141         }
142
143     });
144
145