Add user icon to app bar

This commit is contained in:
Rick Watson 2019-05-26 16:19:16 +01:00
parent ab03ddaf4d
commit 353b46c675
3 changed files with 104 additions and 72 deletions

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { withStyles } from '@material-ui/core/styles'; import { withStyles } from '@material-ui/core/styles';
import Drawer from '@material-ui/core/Drawer'; import Drawer from '@material-ui/core/Drawer';
@ -10,20 +10,28 @@ import Typography from '@material-ui/core/Typography';
import IconButton from '@material-ui/core/IconButton'; import IconButton from '@material-ui/core/IconButton';
import Hidden from '@material-ui/core/Hidden'; import Hidden from '@material-ui/core/Hidden';
import Divider from '@material-ui/core/Divider'; import Divider from '@material-ui/core/Divider';
import Grow from '@material-ui/core/Grow';
import MenuItem from '@material-ui/core/MenuItem';
import MenuList from '@material-ui/core/MenuList';
import List from '@material-ui/core/List'; import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem'; import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon'; import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText'; import ListItemText from '@material-ui/core/ListItemText';
import Popper from '@material-ui/core/Popper';
import MenuIcon from '@material-ui/icons/Menu'; import MenuIcon from '@material-ui/icons/Menu';
import WifiIcon from '@material-ui/icons/Wifi'; import WifiIcon from '@material-ui/icons/Wifi';
import SystemUpdateIcon from '@material-ui/icons/SystemUpdate'; import SystemUpdateIcon from '@material-ui/icons/SystemUpdate';
import AccessTimeIcon from '@material-ui/icons/AccessTime'; import AccessTimeIcon from '@material-ui/icons/AccessTime';
import AccountCircleIcon from '@material-ui/icons/AccountCircle'; import AccountCircleIcon from '@material-ui/icons/AccountCircle';
import SettingsInputAntennaIcon from '@material-ui/icons/SettingsInputAntenna'; import SettingsInputAntennaIcon from '@material-ui/icons/SettingsInputAntenna';
import LockIcon from '@material-ui/icons/Lock'; import LockIcon from '@material-ui/icons/Lock';
import ClickAwayListener from '@material-ui/core/ClickAwayListener';
import Paper from '@material-ui/core/Paper';
import { APP_NAME } from '../constants/App'; import { APP_NAME } from '../constants/App';
import { withAuthenticationContext } from '../authentication/Context.js'; import { withAuthenticationContext } from '../authentication/Context.js';
@ -31,79 +39,76 @@ const drawerWidth = 290;
const styles = theme => ({ const styles = theme => ({
root: { root: {
zIndex: 1,
width: '100%',
height: '100%',
},
toolbar: {
paddingLeft: theme.spacing.unit,
paddingRight: theme.spacing.unit,
[theme.breakpoints.up('md')]: {
paddingLeft: theme.spacing.unit * 3,
paddingRight: theme.spacing.unit * 3,
}
},
appFrame: {
position: 'relative',
display: 'flex', display: 'flex',
width: '100%', },
height: '100%', drawer: {
[theme.breakpoints.up('md')]: {
width: drawerWidth,
flexShrink: 0,
},
},
title: {
flexGrow: 1
}, },
appBar: { appBar: {
position: 'absolute',
marginLeft: drawerWidth, marginLeft: drawerWidth,
[theme.breakpoints.up('md')]: { [theme.breakpoints.up('md')]: {
width: `calc(100% - ${drawerWidth}px)`, width: `calc(100% - ${drawerWidth}px)`,
}, },
}, },
navIconHide: { menuButton: {
marginRight: theme.spacing(2),
[theme.breakpoints.up('md')]: { [theme.breakpoints.up('md')]: {
display: 'none', display: 'none',
}, },
}, },
toolbar: theme.mixins.toolbar,
drawerPaper: { drawerPaper: {
width: drawerWidth, width: drawerWidth,
height: '100%',
[theme.breakpoints.up('md')]: {
width: drawerWidth,
position:'fixed',
left:0,
top:0,
overflow:'auto'
},
}, },
content: { content: {
backgroundColor: theme.palette.background.default, flexGrow: 1,
width:"100%", padding: theme.spacing(),
marginTop: 56,
[theme.breakpoints.up('md')]: {
paddingLeft: drawerWidth
},
[theme.breakpoints.up('sm')]: {
height: 'calc(100% - 64px)',
marginTop: 64,
},
}, },
popper: {
zIndex: 3300
}
}); });
class MenuAppBar extends React.Component { class MenuAppBar extends React.Component {
state = { state = {
mobileOpen: false, mobileOpen: false,
authMenuOpen: false
}; };
anchorRef = React.createRef();
handleToggle = () => {
this.setState({ authMenuOpen: !this.state.authMenuOpen });
}
handleClose = (event) => {
if (this.anchorRef.current && this.anchorRef.current.contains(event.target)) {
return;
}
this.setState({ authMenuOpen: false });
}
handleDrawerToggle = () => { handleDrawerToggle = () => {
this.setState({ mobileOpen: !this.state.mobileOpen }); this.setState({ mobileOpen: !this.state.mobileOpen });
}; };
render() { render() {
const { classes, theme, children, sectionTitle, authenticationContext } = this.props; const { classes, theme, children, sectionTitle, authenticationContext } = this.props;
const { mobileOpen, authMenuOpen } = this.state;
const drawer = ( const drawer = (
<div> <div>
<Toolbar> <Toolbar>
<Typography variant="h6" color="primary"> <Typography variant="h6" color="primary">
{APP_NAME} {APP_NAME}
</Typography> </Typography>
<Divider absolute /> <Divider absolute />
</Toolbar> </Toolbar>
<Divider /> <Divider />
@ -138,46 +143,72 @@ class MenuAppBar extends React.Component {
</ListItemIcon> </ListItemIcon>
<ListItemText primary="Security" /> <ListItemText primary="Security" />
</ListItem> </ListItem>
<Divider />
<ListItem button onClick={authenticationContext.signOut}>
<ListItemIcon>
<AccountCircleIcon />
</ListItemIcon>
<ListItemText primary="Sign Out" secondary={"Signed in as: "+ authenticationContext.jwt.username} />
</ListItem>
</List> </List>
</div> </div>
); );
return ( return (
<div className={classes.root}> <div className={classes.root}>
<div className={classes.appFrame}> <AppBar position="fixed" className={classes.appBar}>
<AppBar className={classes.appBar}> <Toolbar>
<Toolbar className={classes.toolbar} disableGutters={true}> <IconButton
color="inherit"
aria-label="Open drawer"
edge="start"
onClick={this.handleDrawerToggle}
className={classes.menuButton}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" noWrap className={classes.title}>
{sectionTitle}
</Typography>
<div>
<IconButton <IconButton
ref={this.anchorRef}
aria-owns={authMenuOpen ? 'menu-list-grow' : undefined}
aria-haspopup="true"
onClick={this.handleToggle}
color="inherit" color="inherit"
aria-label="open drawer"
onClick={this.handleDrawerToggle}
className={classes.navIconHide}
> >
<MenuIcon /> <AccountCircleIcon />
</IconButton> </IconButton>
<Typography variant="h6" color="inherit" noWrap> <Popper open={authMenuOpen} anchorEl={this.anchorRef.current} transition disablePortal>
{sectionTitle} {({ TransitionProps, placement }) => (
</Typography> <Grow
</Toolbar> {...TransitionProps}
</AppBar> style={{ transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom' }}
<Hidden mdUp> >
<Paper id="menu-list-grow">
<ClickAwayListener onClickAway={this.handleClose}>
<MenuList>
<MenuItem button onClick={authenticationContext.signOut}>
<ListItemIcon>
<AccountCircleIcon />
</ListItemIcon>
<ListItemText primary="Sign Out" secondary={"Signed in as: " + authenticationContext.jwt.username} />
</MenuItem>
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</div>
</Toolbar>
</AppBar>
<nav className={classes.drawer}>
<Hidden mdUp implementation="css">
<Drawer <Drawer
variant="temporary" variant="temporary"
anchor={theme.direction === 'rtl' ? 'right' : 'left'} anchor={theme.direction === 'rtl' ? 'right' : 'left'}
open={this.state.mobileOpen} open={mobileOpen}
onClose={this.handleDrawerToggle}
classes={{ classes={{
paper: classes.drawerPaper, paper: classes.drawerPaper,
}} }}
onClose={this.handleDrawerToggle}
ModalProps={{ ModalProps={{
keepMounted: true, // Better open performance on mobile. keepMounted: true,
}} }}
> >
{drawer} {drawer}
@ -185,19 +216,20 @@ class MenuAppBar extends React.Component {
</Hidden> </Hidden>
<Hidden smDown implementation="css"> <Hidden smDown implementation="css">
<Drawer <Drawer
variant="permanent"
open
classes={{ classes={{
paper: classes.drawerPaper, paper: classes.drawerPaper,
}} }}
variant="permanent"
open
> >
{drawer} {drawer}
</Drawer> </Drawer>
</Hidden> </Hidden>
<main className={classes.content}> </nav>
{children} <main className={classes.content}>
</main> <div className={classes.toolbar} />
</div> {children}
</main>
</div> </div>
); );
} }

View File

@ -239,7 +239,7 @@ class ManageUsersForm extends React.Component {
} }
ManageUsersForm.propTypes = { ManageUsersForm.propTypes = {
authenticationContex: PropTypes.object.isRequired, authenticationContext: PropTypes.object.isRequired,
classes: PropTypes.object.isRequired, classes: PropTypes.object.isRequired,
userData: PropTypes.object, userData: PropTypes.object,
userDataFetched: PropTypes.bool.isRequired, userDataFetched: PropTypes.bool.isRequired,

View File

@ -45,7 +45,7 @@ class UserForm extends React.Component {
<ValidatorForm onSubmit={onDoneEditing} ref={this.formRef}> <ValidatorForm onSubmit={onDoneEditing} ref={this.formRef}>
<Dialog onClose={onCancelEditing} aria-labelledby="user-form-dialog-title" open={true} scroll="paper"> <Dialog onClose={onCancelEditing} aria-labelledby="user-form-dialog-title" open={true} scroll="paper">
<DialogTitle id="user-form-dialog-title">{creating ? 'Create' : 'Modify'} User</DialogTitle> <DialogTitle id="user-form-dialog-title">{creating ? 'Create' : 'Modify'} User</DialogTitle>
<DialogContent> <DialogContent dividers={true}>
<TextValidator <TextValidator
validators={creating ? ['required', 'uniqueUsername', 'matchRegexp:^[a-zA-Z0-9_\\.]{1,24}$'] : []} validators={creating ? ['required', 'uniqueUsername', 'matchRegexp:^[a-zA-Z0-9_\\.]{1,24}$'] : []}
errorMessages={creating ? ['Username is required', "That username already exists", "Must be 1-24 characters: alpha numberic, '_' or '.'"] : []} errorMessages={creating ? ['Username is required', "That username already exists", "Must be 1-24 characters: alpha numberic, '_' or '.'"] : []}