forked from G-Research-Forks/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboardNavbarLinks.tsx
More file actions
120 lines (110 loc) · 3.96 KB
/
DashboardNavbarLinks.tsx
File metadata and controls
120 lines (110 loc) · 3.96 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, { useEffect, useState } from 'react';
import clsx from 'clsx';
import { makeStyles } from '@material-ui/core/styles';
import MenuItem from '@material-ui/core/MenuItem';
import MenuList from '@material-ui/core/MenuList';
import Grow from '@material-ui/core/Grow';
import Paper from '@material-ui/core/Paper';
import ClickAwayListener from '@material-ui/core/ClickAwayListener';
import Hidden from '@material-ui/core/Hidden';
import Popper from '@material-ui/core/Popper';
import Divider from '@material-ui/core/Divider';
import Button from '../CustomButtons/Button';
import styles from '../../assets/jss/material-dashboard-react/components/headerLinksStyle';
import { useNavigate } from 'react-router-dom';
import { AccountCircle } from '@material-ui/icons';
import { getUser } from '../../services/user';
import axios from 'axios';
import { getAxiosConfig } from '../../services/auth';
import { PublicUser } from '../../../db/types';
import { getBaseUrl } from '../../services/apiConfig';
const useStyles = makeStyles(styles);
const DashboardNavbarLinks: React.FC = () => {
const classes = useStyles();
const navigate = useNavigate();
const [openProfile, setOpenProfile] = useState<HTMLElement | null>(null);
const [, setAuth] = useState<boolean>(true);
const [, setIsLoading] = useState<boolean>(true);
const [user, setUser] = useState<PublicUser | null>(null);
useEffect(() => {
getUser(setIsLoading, setUser, setAuth);
}, []);
const handleClickProfile = (event: React.MouseEvent<HTMLElement>) => {
if (openProfile && openProfile.contains(event.target as Node)) {
setOpenProfile(null);
} else {
setOpenProfile(event.currentTarget);
}
};
const handleCloseProfile = () => {
setOpenProfile(null);
};
const showProfile = () => {
navigate('/dashboard/profile', { replace: true });
};
const logout = async () => {
try {
const baseUrl = await getBaseUrl();
const { data } = await axios.post(`${baseUrl}/api/auth/logout`, {}, getAxiosConfig());
if (!data.isAuth && !data.user) {
setAuth(false);
navigate(0);
}
} catch (error) {
console.error('Logout failed:', error);
}
};
return (
<div>
<div className={classes.manager}>
<Button
color={window.innerWidth > 959 ? 'transparent' : 'white'}
justIcon={window.innerWidth > 959}
simple={window.innerWidth <= 959}
aria-owns={openProfile ? 'profile-menu-list-grow' : undefined}
aria-haspopup='true'
onClick={handleClickProfile}
className={classes.buttonLink}
>
<AccountCircle />
<Hidden mdUp implementation='css'>
<p className={classes.linkText}>Profile</p>
</Hidden>
</Button>
<Popper
open={Boolean(openProfile)}
anchorEl={openProfile}
transition
disablePortal
className={clsx(classes.popperNav, { [classes.popperClose]: !openProfile })}
>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{
transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom',
}}
>
<Paper>
<ClickAwayListener onClickAway={handleCloseProfile}>
<MenuList role='menu'>
<MenuItem onClick={showProfile} className={classes.dropdownItem}>
{user ? 'My Account' : 'Login'}
</MenuItem>
{!!user && <Divider light />}
{!!user && (
<MenuItem onClick={logout} className={classes.dropdownItem}>
Logout
</MenuItem>
)}
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</div>
</div>
);
};
export default DashboardNavbarLinks;