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