Add auth service, getting api token, login/logout actions
[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 } 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 { authService } from "../../services/services";
22 import IconButton from "@material-ui/core/IconButton/IconButton";
23 import Menu from "@material-ui/core/Menu/Menu";
24 import MenuItem from "@material-ui/core/MenuItem/MenuItem";
25 import { AccountCircle } from "@material-ui/icons";
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 }
62
63 interface WorkbenchActionProps {
64     login?: () => void;
65     logout?: () => void;
66 }
67
68 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & WithStyles<CssRules>;
69
70 interface WorkbenchState {
71     anchorEl: any;
72 }
73
74 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
75     constructor(props: WorkbenchProps) {
76         super(props);
77         this.state = {
78             anchorEl: null
79         }
80     }
81
82     login = () => {
83         this.props.login!();
84     };
85
86     logout = () => {
87         this.handleClose();
88         this.props.logout!();
89     };
90
91     handleOpenMenu = (event: React.MouseEvent<any>) => {
92         this.setState({
93             anchorEl: event.currentTarget
94         });
95     };
96
97     handleClose = () => {
98         this.setState({
99             anchorEl: null
100         });
101     };
102
103     render() {
104         const {classes} = this.props;
105         const userLoggedIn = authService.isUserLoggedIn();
106         return (
107             <div className={classes.root}>
108                 <AppBar position="absolute" className={classes.appBar}>
109                     <Toolbar>
110                         <Typography variant="title" color="inherit" noWrap style={{flexGrow: 1}}>
111                             <span>Arvados</span><br/><span style={{fontSize: 12}}>Workbench 2</span>
112                         </Typography>
113                         {userLoggedIn ?
114                             <div>
115                                 <IconButton
116                                       aria-owns={this.state.anchorEl ? 'menu-appbar' : undefined}
117                                       aria-haspopup="true"
118                                       onClick={this.handleOpenMenu}
119                                       color="inherit">
120                                   <AccountCircle/>
121                                 </IconButton>
122                                 <Menu
123                                   id="menu-appbar"
124                                   anchorEl={this.state.anchorEl}
125                                   anchorOrigin={{
126                                     vertical: 'top',
127                                     horizontal: 'right',
128                                   }}
129                                   transformOrigin={{
130                                     vertical: 'top',
131                                     horizontal: 'right',
132                                   }}
133                                   open={!!this.state.anchorEl}
134                                   onClose={this.handleClose}>
135                                   <MenuItem onClick={this.logout}>Logout</MenuItem>
136                                   <MenuItem onClick={this.handleClose}>My account</MenuItem>
137                                 </Menu>
138                             </div>
139                             :
140                             <Button color="inherit" onClick={this.login}>Login</Button>
141                         }
142                     </Toolbar>
143                 </AppBar>
144                 {userLoggedIn &&
145                 <Drawer
146                     variant="permanent"
147                     classes={{
148                         paper: classes.drawerPaper,
149                     }}>
150                     <div className={classes.toolbar}/>
151                     <Tree items={this.props.projects} render={(p: Project) =>
152                         <Link to={`/project/${p.name}`}>{p.name}</Link>
153                     }/>
154                 </Drawer>}
155                 <main className={classes.content}>
156                     <div className={classes.toolbar}/>
157                     <Switch>
158                         <Route path="/project/:name" component={ProjectList}/>
159                     </Switch>
160                 </main>
161             </div>
162         );
163     }
164 }
165
166 export default connect<WorkbenchDataProps>(
167     (state: RootState) => ({
168         projects: state.projects
169     }), {
170         login: authActions.login,
171         logout: authActions.logout
172     }
173 )(
174     withStyles(styles)(Workbench)
175 );