source: frontend/src/components/Navbar.js@ e1f74f6

Last change on this file since e1f74f6 was 20d16ca, checked in by MBK <marija.karapandzova@…>, 3 weeks ago

Implement advanced database queries

  • Property mode set to 100644
File size: 7.1 KB
Line 
1import React, { useState, useEffect } from 'react';
2import { Link, useNavigate, useLocation } from 'react-router-dom';
3
4function Navbar() {
5 const [user, setUser] = useState(null);
6 const [showMenu, setShowMenu] = useState(false);
7 const navigate = useNavigate();
8 const location = useLocation();
9
10 useEffect(() => {
11 const loadUser = () => {
12 const userStr = localStorage.getItem('user');
13 if (userStr) {
14 setUser(JSON.parse(userStr));
15 } else {
16 setUser(null);
17 }
18 };
19
20 loadUser();
21
22 window.addEventListener('storage', loadUser);
23 return () => window.removeEventListener('storage', loadUser);
24 }, []);
25
26 const handleLogout = () => {
27 localStorage.removeItem('token');
28 localStorage.removeItem('user');
29 setUser(null);
30
31
32 window.dispatchEvent(new CustomEvent('userStorageChange', {
33 detail: { userData: null }
34 }));
35
36 navigate('/login');
37 };
38
39 const isActive = (path) => location.pathname === path;
40
41 const getInitials = () => {
42 if (!user) return '';
43 return `${user.firstName?.[0]}${user.lastName?.[0]}`.toUpperCase();
44 };
45
46 return (
47 <nav className="navbar">
48 <div className="navbar-brand">
49 <img src="/logo.png" alt="Medora Logo" style={{ height: '40px', width: 'auto' }} />
50 </div>
51
52 <div className="navbar-links">
53 <Link to="/" className={`navbar-link ${isActive('/') ? 'active' : ''}`}>
54 Overview
55 </Link>
56 {user?.role !== 'PATIENT' && user?.role !== 'LAB_TECHNICIAN' && user?.role !== 'BILLING_ADMIN' && (
57 <Link to="/patients" className={`navbar-link ${isActive('/patients') ? 'active' : ''}`}>
58 Patients
59 </Link>
60 )}
61 <Link to="/doctors" className={`navbar-link ${isActive('/doctors') ? 'active' : ''}`}>
62 Doctors
63 </Link>
64 <Link to="/departments" className={`navbar-link ${isActive('/departments') ? 'active' : ''}`}>
65 Departments
66 </Link>
67 {user?.role !== 'LAB_TECHNICIAN' && user?.role !== 'BILLING_ADMIN' && (
68 <>
69 <Link to="/appointments" className={`navbar-link ${isActive('/appointments') ? 'active' : ''}`}>
70 Appointments
71 </Link>
72 {user?.role === 'PATIENT' && (
73 <>
74 <Link to="/medical-records" className={`navbar-link ${isActive('/medical-records') ? 'active' : ''}`}>
75 Medical Records
76 </Link>
77 <Link to="/billing" className={`navbar-link ${isActive('/billing') ? 'active' : ''}`}>
78 Billing
79 </Link>
80 </>
81 )}
82 </>
83 )}
84 {(user?.role === 'ADMIN' || user?.role === 'LAB_TECHNICIAN' || user?.role === 'DOCTOR') && (
85 <Link to="/lab-tests" className={`navbar-link ${isActive('/lab-tests') ? 'active' : ''}`}>
86 Lab Tests
87 </Link>
88 )}
89 {!user?.role || user?.role === 'ADMIN' || user?.role === 'DOCTOR' ? (
90 <>
91 {user?.role !== 'LAB_TECHNICIAN' && user?.role !== 'BILLING_ADMIN' && (
92 <Link to="/medical-records" className={`navbar-link ${isActive('/medical-records') ? 'active' : ''}`}>
93 Medical Records
94 </Link>
95 )}
96 {user?.role !== 'PATIENT' && user?.role !== 'LAB_TECHNICIAN' && user?.role !== 'BILLING_ADMIN' && (
97 <>
98 <Link to="/medical-reports" className={`navbar-link ${isActive('/medical-reports') ? 'active' : ''}`}>
99 Medical Reports
100 </Link>
101 <Link to="/procedures" className={`navbar-link ${isActive('/procedures') ? 'active' : ''}`}>
102 Procedures
103 </Link>
104 <Link to="/referrals" className={`navbar-link ${isActive('/referrals') ? 'active' : ''}`}>
105 Referrals
106 </Link>
107 </>
108 )}
109 </>
110 ) : null}
111 {(user?.role === 'ADMIN' || user?.role === 'BILLING_ADMIN') && (
112 <Link to="/billing" className={`navbar-link ${isActive('/billing') ? 'active' : ''}`}>
113 Billing
114 </Link>
115 )}
116 </div>
117
118 <div className="navbar-right">
119 {user && (
120 <div className="navbar-avatar" style={{ position: 'relative' }}>
121 <div className="navbar-avatar-circle">{getInitials()}</div>
122 <div className="navbar-avatar-name">{user.firstName}</div>
123 <button
124 onClick={() => setShowMenu(!showMenu)}
125 style={{
126 background: 'none',
127 border: 'none',
128 cursor: 'pointer',
129 marginLeft: '4px',
130 color: 'var(--color-neutral-600)',
131 fontSize: '12px'
132 }}
133 >
134
135 </button>
136 {showMenu && (
137 <div style={{
138 position: 'absolute',
139 top: '100%',
140 right: 0,
141 background: 'white',
142 border: '1px solid var(--color-neutral-400)',
143 borderRadius: '6px',
144 minWidth: '150px',
145 marginTop: '4px',
146 boxShadow: '0 4px 6px rgba(0,0,0,0.1)',
147 zIndex: 1000
148 }}>
149 <div style={{
150 padding: '8px 0',
151 fontSize: '11px',
152 color: 'var(--color-neutral-600)',
153 borderBottom: '1px solid var(--color-neutral-400)',
154 paddingLeft: '12px',
155 paddingRight: '12px',
156 paddingTop: '8px',
157 paddingBottom: '8px'
158 }}>
159 {user.role}
160 </div>
161 <button
162 onClick={handleLogout}
163 style={{
164 width: '100%',
165 padding: '8px 12px',
166 background: 'none',
167 border: 'none',
168 textAlign: 'left',
169 cursor: 'pointer',
170 fontSize: '12px',
171 color: 'var(--color-status-red)',
172 transition: 'background 0.2s'
173 }}
174 onMouseEnter={(e) => e.target.style.background = 'var(--color-neutral-200)'}
175 onMouseLeave={(e) => e.target.style.background = 'none'}
176 >
177 Logout
178 </button>
179 </div>
180 )}
181 </div>
182 )}
183 </div>
184 </nav>
185 );
186}
187
188export default Navbar;
Note: See TracBrowser for help on using the repository browser.