21224: added chevron, moved description section Arvados-DCO-1.1-Signed-off-by: Lisa...
[arvados.git] / services / workbench2 / src / views / run-process-panel / inputs / project-input.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { connect, DispatchProp } from 'react-redux';
7 import { Field } from 'redux-form';
8 import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button, withStyles, WithStyles, StyleRulesCallback } from '@material-ui/core';
9 import {
10     GenericCommandInputParameter
11 } from 'models/workflow';
12 import { GenericInput, GenericInputProps } from './generic-input';
13 import { ProjectsTreePicker } from 'views-components/projects-tree-picker/projects-tree-picker';
14 import { initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
15 import { TreeItem } from 'components/tree/tree';
16 import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
17 import { ProjectResource } from 'models/project';
18 import { ResourceKind } from 'models/resource';
19 import { RootState } from 'store/store';
20 import { getUserUuid } from 'common/getuser';
21
22 export type ProjectCommandInputParameter = GenericCommandInputParameter<ProjectResource, ProjectResource>;
23
24 const require: any = (value?: ProjectResource) => (value === undefined);
25
26 export interface ProjectInputProps {
27     required: boolean;
28     input: ProjectCommandInputParameter;
29     options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
30 }
31
32 type DialogContentCssRules = 'root' | 'pickerWrapper';
33
34 export const ProjectInput = ({ required, input, options }: ProjectInputProps) =>
35     <Field
36         name={input.id}
37         commandInput={input}
38         component={ProjectInputComponent as any}
39         format={format}
40         validate={required ? require : undefined}
41         {...{
42             options,
43             required
44         }} />;
45
46 const format = (value?: ProjectResource) => value ? value.name : '';
47
48 interface ProjectInputComponentState {
49     open: boolean;
50     project?: ProjectResource;
51 }
52
53 interface HasUserUuid {
54     userUuid: string;
55 };
56
57 const mapStateToProps = (state: RootState) => ({ userUuid: getUserUuid(state) });
58
59 export const ProjectInputComponent = connect(mapStateToProps)(
60     class ProjectInputComponent extends React.Component<GenericInputProps & DispatchProp & HasUserUuid & {
61         options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
62         required?: boolean;
63     }, ProjectInputComponentState> {
64         state: ProjectInputComponentState = {
65             open: false,
66         };
67
68         componentDidMount() {
69             this.props.dispatch<any>(
70                 initProjectsTreePicker(this.props.commandInput.id));
71         }
72
73         render() {
74             return <>
75                 {this.renderInput()}
76                 <this.dialog />
77             </>;
78         }
79
80         openDialog = () => {
81             this.componentDidMount();
82             this.setState({ open: true });
83         }
84
85         closeDialog = () => {
86             this.setState({ open: false });
87         }
88
89         submit = () => {
90             this.closeDialog();
91             this.props.input.onChange(this.state.project);
92         }
93
94         setProject = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
95             if ('kind' in data && data.kind === ResourceKind.PROJECT) {
96                 this.setState({ project: data });
97             } else {
98                 this.setState({ project: undefined });
99             }
100         }
101
102         invalid = () => (!this.state.project || !this.state.project.canWrite);
103
104         renderInput() {
105             return <GenericInput
106                 component={props =>
107                     <Input
108                         readOnly
109                         fullWidth
110                         value={props.input.value}
111                         error={props.meta.touched && !!props.meta.error}
112                         disabled={props.commandInput.disabled}
113                         onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
114                         onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
115                 {...this.props} />;
116         }
117
118         dialogContentStyles: StyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
119             root: {
120                 display: 'flex',
121                 flexDirection: 'column',
122             },
123             pickerWrapper: {
124                 flexBasis: `${spacing.unit * 8}vh`,
125                 flexShrink: 1,
126                 minHeight: 0,
127             },
128         });
129
130         dialog = withStyles(this.dialogContentStyles)(
131             ({ classes }: WithStyles<DialogContentCssRules>) =>
132                 this.state.open ? <Dialog
133                     open={this.state.open}
134                     onClose={this.closeDialog}
135                     fullWidth
136                     data-cy="choose-a-project-dialog"
137                     maxWidth='md'>
138                     <DialogTitle>Choose a project</DialogTitle>
139                     <DialogContent className={classes.root}>
140                         <div className={classes.pickerWrapper}>
141                             <ProjectsTreePicker
142                                 pickerId={this.props.commandInput.id}
143                                 cascadeSelection={false}
144                                 options={this.props.options}
145                                 toggleItemActive={this.setProject} />
146                         </div>
147                     </DialogContent>
148                     <DialogActions>
149                         <Button onClick={this.closeDialog}>Cancel</Button>
150                         <Button
151                             disabled={this.invalid()}
152                             variant='contained'
153                             color='primary'
154                             onClick={this.submit}>Ok</Button>
155                     </DialogActions>
156                 </Dialog> : null
157         );
158
159     });