Tree component adjsustments for dynamic contents
[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.projects ? 
109             this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(itemId)) : ( 
110                 this.props.dispatch<any>(projectService.getProjectList(itemId)).then(() => {
111                     this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(itemId));
112                 }))
113     };
114
115     render() {
116         const {classes, user} = this.props;
117         return (
118             <div className={classes.root}>
119                 <AppBar position="absolute" className={classes.appBar}>
120                     <Toolbar>
121                         <Typography variant="title" color="inherit" noWrap style={{flexGrow: 1}}>
122                             <span>Arvados</span><br/><span style={{fontSize: 12}}>Workbench 2</span>
123                         </Typography>
124                         {user ?
125                             <Grid container style={{width: 'auto'}}>
126                                 <Grid container style={{width: 'auto'}} alignItems='center'>
127                                     <Typography variant="title" color="inherit" noWrap>
128                                         {user.firstName} {user.lastName}
129                                     </Typography>
130                                 </Grid>
131                                 <Grid item>
132                                     <IconButton
133                                           aria-owns={this.state.anchorEl ? 'menu-appbar' : undefined}
134                                           aria-haspopup="true"
135                                           onClick={this.handleOpenMenu}
136                                           color="inherit">
137                                       <AccountCircle/>
138                                     </IconButton>
139                                 </Grid>
140                                 <Menu
141                                   id="menu-appbar"
142                                   anchorEl={this.state.anchorEl}
143                                   anchorOrigin={{
144                                     vertical: 'top',
145                                     horizontal: 'right',
146                                   }}
147                                   transformOrigin={{
148                                     vertical: 'top',
149                                     horizontal: 'right',
150                                   }}
151                                   open={!!this.state.anchorEl}
152                                   onClose={this.handleClose}>
153                                   <MenuItem onClick={this.logout}>Logout</MenuItem>
154                                   <MenuItem onClick={this.handleClose}>My account</MenuItem>
155                                 </Menu>
156                             </Grid>
157                             :
158                             <Button color="inherit" onClick={this.login}>Login</Button>
159                         }
160                     </Toolbar>
161                 </AppBar>
162                 {user &&
163                 <Drawer
164                     variant="permanent"
165                     classes={{
166                         paper: classes.drawerPaper,
167                     }}>
168                     <div className={classes.toolbar}/>
169                     <ProjectTree
170                         projects={this.props.projects}
171                         toggleProjectTreeItem={this.toggleProjectTreeItem}/>
172                 </Drawer>}
173                 <main className={classes.content}>
174                     <div className={classes.toolbar}/>
175                     <Switch>
176                         <Route path="/project/:name" component={ProjectList}/>
177                     </Switch>
178                 </main>
179             </div>
180         );
181     }
182 }
183
184 export default connect<WorkbenchDataProps>(
185     (state: RootState) => ({
186         projects: state.projects,
187         user: state.auth.user
188     })
189 )(
190     withStyles(styles)(Workbench)
191 );