-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathUserMenu.jsx
86 lines (80 loc) · 2.43 KB
/
UserMenu.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { useState, Fragment } from 'react';
import { makeStyles } from '@material-ui/styles';
import Avatar from '@material-ui/core/Avatar';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import IconButton from '@material-ui/core/IconButton';
import LogoutVariantIcon from 'mdi-react/LogoutVariantIcon';
import ContentCopyIcon from 'mdi-react/ContentCopyIcon';
import copy from 'clipboard-copy';
import Button from '../Button';
import { USER_SESSION } from '../../utils/constants';
import { withUser } from '../../utils/AuthContext';
const useStyles = makeStyles(theme => ({
avatar: {
height: theme.spacing(6),
width: theme.spacing(6),
padding: 0,
},
}));
function UserMenu(props) {
const { user, onAuthorize, onUnauthorize } = props;
const classes = useStyles();
const [anchorEl, setAnchorEl] = useState(null);
const handleMenuOpen = e => setAnchorEl(e.currentTarget);
const handleMenuClose = () => setAnchorEl(null);
const handleLogoutClick = () => {
handleMenuClose();
onUnauthorize();
};
const handleCopyAccessToken = () => {
const { accessToken } = JSON.parse(
localStorage.getItem(USER_SESSION)
).authResult;
copy(accessToken);
handleMenuClose();
};
return (
<Fragment>
{user ? (
<IconButton
className={classes.avatar}
aria-haspopup="true"
aria-controls="user-menu"
aria-label="user menu"
onClick={handleMenuOpen}>
{user.picture ? (
<Avatar alt={user.nickname} src={user.picture} />
) : (
<Avatar alt={user.name}>{user.name[0]}</Avatar>
)}
</IconButton>
) : (
<Button
onClick={onAuthorize}
size="small"
variant="contained"
color="secondary">
Login
</Button>
)}
<Menu
id="user-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
getContentAnchorEl={null}
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
onClose={handleMenuClose}>
<MenuItem title="Copy Access Token" onClick={handleCopyAccessToken}>
<ContentCopyIcon />
Copy Access Token
</MenuItem>
<MenuItem title="Logout" onClick={handleLogoutClick}>
<LogoutVariantIcon />
Logout
</MenuItem>
</Menu>
</Fragment>
);
}
export default withUser(UserMenu);