-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSideBar.tsx
145 lines (134 loc) · 4.67 KB
/
SideBar.tsx
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { useEffect, useState } from 'react';
import { XCircle } from 'react-bootstrap-icons';
import { useCookies } from 'react-cookie';
import { NavLink } from 'react-router-dom';
import { CSSTransition } from 'react-transition-group';
import { Icon } from 'semantic-ui-react';
import Logo from '../../asset/peterportal-banner-logo.svg';
import './Sidebar.scss';
import axios, { AxiosResponse } from 'axios';
import { useAppDispatch, useAppSelector } from '../..//store/hooks';
import { setSidebarStatus } from '../../store/slices/uiSlice';
import Footer from '../Footer/Footer';
const SideBar = () => {
const dispatch = useAppDispatch();
const showSidebar = useAppSelector((state) => state.ui.sidebarOpen);
const [cookies] = useCookies(['user']);
const [isAdmin, setIsAdmin] = useState<boolean>(false);
const isLoggedIn = cookies.user !== undefined;
interface AdminResponse {
admin: boolean;
}
useEffect(() => {
// if the user is logged in
if (isLoggedIn) {
// useEffect's function is not allowed to be async, create async checkAdmin function within
const checkAdmin = async () => {
const res: AxiosResponse<AdminResponse> = await axios.get('/api/users/isAdmin');
const admin = res.data.admin;
setIsAdmin(admin);
};
checkAdmin();
}
}, [cookies.user, isLoggedIn]);
const closeSidebar = () => dispatch(setSidebarStatus(false));
const links = (
<div className="sidebar-links">
<ul>
<li>
<NavLink
to="/"
className={({ isActive }) => (isActive || location.pathname === '/search/courses' ? 'sidebar-active' : '')}
onClick={closeSidebar}
>
<div>
<Icon name="list alternate outline" size="large" />
</div>
<span className="full-name">Course Catalog</span>
<span>Courses</span>
</NavLink>
</li>
<li>
<NavLink
to="/search/professors"
className={({ isActive }) => (isActive ? 'sidebar-active' : '')}
onClick={closeSidebar}
>
<div>
<Icon name="users" size="large" />
</div>
<span className="full-name">Professors</span>
<span>Professors</span>
</NavLink>
</li>
<li>
<NavLink
to="/roadmap"
className={({ isActive }) => (isActive ? 'sidebar-active' : '')}
onClick={closeSidebar}
>
<div>
<Icon name="map outline" size="large" />
</div>
<span className="full-name">Peter's Roadmap</span>
<span>Roadmap</span>
</NavLink>
</li>
{isAdmin && (
<>
<li>
<NavLink
to="/admin/verify"
className={({ isActive }) => (isActive ? 'sidebar-active' : '')}
onClick={closeSidebar}
>
<div>
<Icon name="check" size="large" />
</div>
<span className="full-name">Verify Reviews</span>
<span>Verification</span>
</NavLink>
</li>
<li>
<NavLink
to="/admin/reports"
className={({ isActive }) => (isActive ? 'sidebar-active' : '')}
onClick={closeSidebar}
>
<div>
<Icon name="exclamation triangle" size="large" />
</div>
<span className="full-name">View Reports</span>
<span>Reports</span>
</NavLink>
</li>{' '}
</>
)}
</ul>
</div>
);
return (
<>
<div className="sidebar mini">{links}</div>
<CSSTransition in={showSidebar} timeout={500} unmountOnExit>
{/* Clicking this is only an alternative action to something that is already accessible */}
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions,jsx-a11y/click-events-have-key-events */}
<div className="sidebar-overlay" onClick={closeSidebar}></div>
</CSSTransition>
<CSSTransition in={showSidebar} timeout={500} unmountOnExit>
<div className="sidebar">
{/* Close Button */}
<div className="button-container">
<img alt="PeterPortal" src={Logo} height="32" />
<button className="sidebar-close" onClick={closeSidebar} id="close-sidebar-btn">
<XCircle className="sidebar-close-icon" size="28px" />
</button>
</div>
{links}
<Footer />
</div>
</CSSTransition>
</>
);
};
export default SideBar;