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