refs #13610 Merge branch '13610-projects-hierarchy'
[arvados-workbench2.git] / src / views / workbench / workbench.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
7 import { StyleRulesCallback, Theme, WithStyles, withStyles } from '@material-ui/core/styles';
8 import Drawer from '@material-ui/core/Drawer';
9 import AppBar from '@material-ui/core/AppBar';
10 import Toolbar from '@material-ui/core/Toolbar';
11 import Typography from '@material-ui/core/Typography';
12 import { connect, DispatchProp } from "react-redux";
13 import ProjectList from "../../components/project-list/project-list";
14 import { Route, Switch } from "react-router";
15 import { Link } from "react-router-dom";
16 import Button from "@material-ui/core/Button/Button";
17 import authActions from "../../store/auth/auth-action";
18 import IconButton from "@material-ui/core/IconButton/IconButton";
19 import Menu from "@material-ui/core/Menu/Menu";
20 import MenuItem from "@material-ui/core/MenuItem/MenuItem";
21 import { AccountCircle } from "@material-ui/icons";
22 import { User } from "../../models/user";
23 import Grid from "@material-ui/core/Grid/Grid";
24 import { RootState } from "../../store/store";
25 import projectActions from "../../store/project/project-action"
26
27 import ProjectTree from '../../components/project-tree/project-tree';
28 import { TreeItem } from "../../components/tree/tree";
29 import { Project } from "../../models/project";
30 import { projectService } from '../../services/services';
31
32 const drawerWidth = 240;
33
34 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'toolbar';
35
36 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
37     root: {
38         flexGrow: 1,
39         zIndex: 1,
40         overflow: 'hidden',
41         position: 'relative',
42         display: 'flex',
43         width: '100vw',
44         height: '100vh'
45     },
46     appBar: {
47         zIndex: theme.zIndex.drawer + 1,
48         backgroundColor: '#692498'
49     },
50     drawerPaper: {
51         position: 'relative',
52         width: drawerWidth,
53     },
54     content: {
55         flexGrow: 1,
56         backgroundColor: theme.palette.background.default,
57         padding: theme.spacing.unit * 3,
58         height: '100%',
59         minWidth: 0,
60     },
61     toolbar: theme.mixins.toolbar
62 });
63
64 interface WorkbenchDataProps {
65     projects: Array<TreeItem<Project>>;
66     user?: User;
67 }
68
69 interface WorkbenchActionProps {
70 }
71
72 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
73
74 interface WorkbenchState {
75     anchorEl: any;
76 }
77
78 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
79     constructor(props: WorkbenchProps) {
80         super(props);
81         this.state = {
82             anchorEl: null
83         }
84     }
85
86     login = () => {
87         this.props.dispatch(authActions.LOGIN());
88     };
89
90     logout = () => {
91         this.handleClose();
92         this.props.dispatch(authActions.LOGOUT());
93     };
94
95     handleOpenMenu = (event: React.MouseEvent<any>) => {
96         this.setState({
97             anchorEl: event.currentTarget
98         });
99     };
100
101     handleClose = () => {
102         this.setState({
103             anchorEl: null
104         });
105     };
106
107     toggleProjectTreeItem = (itemId: string) => {
108         this.props.dispatch<any>(projectService.getProjectList(itemId)).then(() => {
109             this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(itemId));
110         });
111     };
112
113     render() {
114         const {classes, user} = this.props;
115         return (
116             <div className={classes.root}>
117                 <AppBar position="absolute" className={classes.appBar}>
118                     <Toolbar>
119                         <Typography variant="title" color="inherit" noWrap style={{flexGrow: 1}}>
120                             <span>Arvados</span><br/><span style={{fontSize: 12}}>Workbench 2</span>
121                         </Typography>
122                         {user ?
123                             <Grid container style={{width: 'auto'}}>
124                                 <Grid container style={{width: 'auto'}} alignItems='center'>
125                                     <Typography variant="title" color="inherit" noWrap>
126                                         {user.firstName} {user.lastName}
127                                     </Typography>
128                                 </Grid>
129                                 <Grid item>
130                                     <IconButton
131                                           aria-owns={this.state.anchorEl ? 'menu-appbar' : undefined}
132                                           aria-haspopup="true"
133                                           onClick={this.handleOpenMenu}
134                                           color="inherit">
135                                       <AccountCircle/>
136                                     </IconButton>
137                                 </Grid>
138                                 <Menu
139                                   id="menu-appbar"
140                                   anchorEl={this.state.anchorEl}
141                                   anchorOrigin={{
142                                     vertical: 'top',
143                                     horizontal: 'right',
144                                   }}
145                                   transformOrigin={{
146                                     vertical: 'top',
147                                     horizontal: 'right',
148                                   }}
149                                   open={!!this.state.anchorEl}
150                                   onClose={this.handleClose}>
151                                   <MenuItem onClick={this.logout}>Logout</MenuItem>
152                                   <MenuItem onClick={this.handleClose}>My account</MenuItem>
153                                 </Menu>
154                             </Grid>
155                             :
156                             <Button color="inherit" onClick={this.login}>Login</Button>
157                         }
158                     </Toolbar>
159                 </AppBar>
160                 {user &&
161                 <Drawer
162                     variant="permanent"
163                     classes={{
164                         paper: classes.drawerPaper,
165                     }}>
166                     <div className={classes.toolbar}/>
167                     <ProjectTree
168                         projects={this.props.projects}
169                         toggleProjectTreeItem={this.toggleProjectTreeItem}/>
170                 </Drawer>}
171                 <main className={classes.content}>
172                     <div className={classes.toolbar}/>
173                     <Switch>
174                         <Route path="/project/:name" component={ProjectList}/>
175                     </Switch>
176                 </main>
177             </div>
178         );
179     }
180 }
181
182 export default connect<WorkbenchDataProps>(
183     (state: RootState) => ({
184         projects: state.projects,
185         user: state.auth.user
186     })
187 )(
188     withStyles(styles)(Workbench)
189 );