ef6d08f40086dbeb4aa2d0596cdb412885c841f9
[arvados-workbench2.git] / 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     input: ProjectCommandInputParameter;
28     options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
29 }
30
31 type DialogContentCssRules = 'root' | 'pickerWrapper';
32
33 export const ProjectInput = ({ input, options }: ProjectInputProps) =>
34     <Field
35         name={input.id}
36         commandInput={input}
37         component={ProjectInputComponent as any}
38         format={format}
39         validate={require}
40         {...{
41             options
42         }} />;
43
44 const format = (value?: ProjectResource) => value ? value.name : '';
45
46 interface ProjectInputComponentState {
47     open: boolean;
48     project?: ProjectResource;
49 }
50
51 interface HasUserUuid {
52     userUuid: string;
53 };
54
55 const mapStateToProps = (state: RootState) => ({ userUuid: getUserUuid(state) });
56
57 export const ProjectInputComponent = connect(mapStateToProps)(
58     class ProjectInputComponent extends React.Component<GenericInputProps & DispatchProp & HasUserUuid & {
59         options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
60     }, ProjectInputComponentState> {
61         state: ProjectInputComponentState = {
62             open: false,
63         };
64
65         componentDidMount() {
66             this.props.dispatch<any>(
67                 initProjectsTreePicker(this.props.commandInput.id));
68         }
69
70         render() {
71             return <>
72                 {this.renderInput()}
73                 <this.dialog />
74             </>;
75         }
76
77         openDialog = () => {
78             this.componentDidMount();
79             this.setState({ open: true });
80         }
81
82         closeDialog = () => {
83             this.setState({ open: false });
84         }
85
86         submit = () => {
87             this.closeDialog();
88             this.props.input.onChange(this.state.project);
89         }
90
91         setProject = (_: {}, { data }: TreeItem<ProjectsTreePickerItem>) => {
92             if ('kind' in data && data.kind === ResourceKind.PROJECT) {
93                 this.setState({ project: data });
94             } else {
95                 this.setState({ project: undefined });
96             }
97         }
98
99         invalid = () => (!this.state.project || this.state.project.writableBy.indexOf(this.props.userUuid) === -1);
100
101         renderInput() {
102             return <GenericInput
103                 component={props =>
104                     <Input
105                         readOnly
106                         fullWidth
107                         value={props.input.value}
108                         error={props.meta.touched && !!props.meta.error}
109                         disabled={props.commandInput.disabled}
110                         onClick={!this.props.commandInput.disabled ? this.openDialog : undefined}
111                         onKeyPress={!this.props.commandInput.disabled ? this.openDialog : undefined} />}
112                 {...this.props} />;
113         }
114
115         dialogContentStyles: StyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
116             root: {
117                 display: 'flex',
118                 flexDirection: 'column',
119             },
120             pickerWrapper: {
121                 flexBasis: `${spacing.unit * 8}vh`,
122                 flexShrink: 1,
123                 minHeight: 0,
124             },
125         });
126
127         dialog = withStyles(this.dialogContentStyles)(
128             ({ classes }: WithStyles<DialogContentCssRules>) =>
129                 this.state.open ? <Dialog
130                     open={this.state.open}
131                     onClose={this.closeDialog}
132                     fullWidth
133                     data-cy="choose-a-project-dialog"
134                     maxWidth='md'>
135                     <DialogTitle>Choose a project</DialogTitle>
136                     <DialogContent className={classes.root}>
137                         <div className={classes.pickerWrapper}>
138                             <ProjectsTreePicker
139                                 pickerId={this.props.commandInput.id}
140                                 options={this.props.options}
141                                 toggleItemActive={this.setProject} />
142                         </div>
143                     </DialogContent>
144                     <DialogActions>
145                         <Button onClick={this.closeDialog}>Cancel</Button>
146                         <Button
147                             disabled={this.invalid()}
148                             variant='contained'
149                             color='primary'
150                             onClick={this.submit}>Ok</Button>
151                     </DialogActions>
152                 </Dialog> : null
153         );
154
155     });