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