Created
June 14, 2018 11:57
-
-
Save OpakAlex/524ac8bfbb323b209603085a063a8b81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { withStyles } from 'material-ui/styles'; | |
import { browserHistory } from 'react-router'; | |
import { compose } from 'recompose'; | |
import Grid from 'material-ui/Grid'; | |
import Menu, { MenuItem } from 'material-ui/Menu'; | |
import User from './User'; | |
import { withState, withAuth } from '../helpers'; | |
const styles = theme => ({ | |
wrapper: { | |
marginTop: theme.spacing.unit | |
}, | |
right: { | |
display: 'flex', | |
justifyContent: 'flex-end', | |
marginRight: theme.spacing.unit * 2 | |
} | |
}) | |
const enhance = compose(withStyles(styles), withState('menu'), withAuth) | |
const Auth = enhance(({ classes, menu, handleChangeMenu, session, logout, children }) => { | |
const handleClick = event => handleChangeMenu('anchorEl', event.currentTarget) | |
const handleClose = () => handleChangeMenu('anchorEl', null) | |
const handleLogout = () => { | |
logout() | |
handleClose() | |
window.location.reload() | |
} | |
const handleAdmin = () => { | |
handleClose() | |
browserHistory.push('/admin') | |
} | |
const handleMain = () => { | |
handleClose() | |
browserHistory.push('/') | |
} | |
if (!session) { | |
return null | |
} | |
const mapped = React.Children.map(children, child => React.cloneElement(child, { session, logout })) | |
return ( | |
<div> | |
<Grid container className={classes.wrapper} spacing={0}> | |
<Grid item xs> | |
</Grid> | |
<Grid item xs={3} className={classes.right}> | |
<User picture={session.picture} name={session.nickname} onClick={handleClick} /> | |
<Menu | |
id="simple-menu" | |
anchorEl={menu.anchorEl} | |
open={Boolean(menu.anchorEl)} | |
onClose={handleClose} | |
> | |
<MenuItem onClick={handleMain}>Main</MenuItem> | |
<MenuItem onClick={handleAdmin}>Admin</MenuItem> | |
<MenuItem onClick={handleLogout}>Logout</MenuItem> | |
</Menu> | |
</Grid> | |
</Grid> | |
{mapped} | |
</div> | |
) | |
}) | |
export default Auth |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment