-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathKIHeader.jsx
More file actions
456 lines (429 loc) · 16.6 KB
/
KIHeader.jsx
File metadata and controls
456 lines (429 loc) · 16.6 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import { useState, useEffect, useMemo, useRef } from 'react';
// import { getUserProfile } from '../../actions/userProfile'
import { Link } from 'react-router-dom';
import { connect, useDispatch } from 'react-redux';
import {
Collapse,
Navbar,
NavbarToggler,
Nav,
NavItem,
NavLink,
UncontrolledDropdown,
DropdownToggle,
DropdownMenu,
DropdownItem,
} from 'reactstrap';
import DarkModeButton from '../../Header/DarkModeButton';
import { fetchTaskEditSuggestions } from '~/components/TaskEditSuggestions/thunks';
import BellNotification from '~/components/Header/BellNotification';
import { getHeaderData } from '../../../actions/authActions';
import { getAllRoles } from '../../../actions/role';
import Timer from '../../Timer/Timer';
import OwnerMessage from '../../OwnerMessage/OwnerMessage';
import {
// LOGO,
DASHBOARD,
// BM_DASHBOARD,
// CP_DASHBOARD,
REPORTS,
OTHER_LINKS,
USER_MANAGEMENT,
BADGE_MANAGEMENT,
PROJECTS,
TEAMS,
WELCOME,
VIEW_PROFILE,
UPDATE_PASSWORD,
LOGOUT,
// PERMISSIONS_MANAGEMENT,
// ACTIVITY,
// PARTICIPATION,
// RESOURCE_USAGE,
// CALENDAR,
// EVENT_PERSONALIZATION,
// ACTIVITIES,
// REGISTRATION,
// SEND_EMAILS,
// ACTIVITY_RESOURCE_MANGEMENT,
// ACTIVITY_RESOURCE_USAGE,
PRODUCTION,
KI_PROCESSING,
GARDEN_MANAGEMENT,
ORCHARD_MANAGEMENT,
ANIMAL_MANAGEMENT,
ANIMAL_MANAGEMENT_TRACKING,
RECIPES,
KI_INVENTORY,
ORDERS,
KI_REPORTS,
FOOD_BARS,
KI_CALENDAR,
} from '../../../languages/en/ui';
import Logout from '../../Logout/Logout';
// import './CPHeader.css';
import styles from '../../Header/Header.module.css';
import hasPermission, { cantUpdateDevAdminDetails } from '../../../utils/permissions';
export function Header(props) {
const [isOpen, setIsOpen] = useState(false);
const [logoutPopup, setLogoutPopup] = useState(false);
const { isAuthenticated, user } = props.auth;
const [firstName, setFirstName] = useState(props.auth.firstName);
const [profilePic, setProfilePic] = useState(props.auth.profilePic);
const [isAuthUser, setIsAuthUser] = useState(true);
const [displayUserId, setDisplayUserId] = useState(user.userid);
const collapseRef = useRef(null);
const toggleRef = useRef(null);
const ALLOWED_ROLES_TO_INTERACT = useMemo(() => ['Owner', 'Administrator'], []);
const canInteractWithViewingUser = useMemo(
() => ALLOWED_ROLES_TO_INTERACT.includes(props.auth.user.role),
[ALLOWED_ROLES_TO_INTERACT, props.auth.user.role],
);
// Users
const canAccessUserManagement =
props.hasPermission('postUserProfile', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('deleteUserProfile', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('changeUserStatus', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('getUserProfiles', !isAuthUser && canInteractWithViewingUser);
// Badges
const canAccessBadgeManagement =
props.hasPermission('seeBadges', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('createBadges', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('updateBadges', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('deleteBadges', !isAuthUser && canInteractWithViewingUser);
// Projects
const canAccessProjects =
props.hasPermission('postProject', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('deleteProject', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('putProject', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('getProjectMembers', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('assignProjectToUsers', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('postWbs', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('deleteWbs', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('postTask', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('updateTask', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('deleteTask', !isAuthUser && canInteractWithViewingUser);
// Tasks
const canUpdateTask = props.hasPermission(
'updateTask',
!isAuthUser && canInteractWithViewingUser,
);
// Teams
const canAccessTeams =
props.hasPermission('postTeam', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('putTeam', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('deleteTeam', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('assignTeamToUsers', !isAuthUser && canInteractWithViewingUser);
// Popups
const canAccessPopups =
props.hasPermission('createPopup', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('updatePopup', !isAuthUser && canInteractWithViewingUser);
// SendEmails
const canAccessSendEmails = props.hasPermission('sendEmails', !isAuthUser);
// Permissions
const canAccessPermissionsManagement =
props.hasPermission('postRole', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('putRole', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('deleteRole', !isAuthUser && canInteractWithViewingUser) ||
props.hasPermission('putUserProfilePermissions', !isAuthUser && canInteractWithViewingUser);
const dispatch = useDispatch();
const { darkMode } = props;
useEffect(() => {
const handleStorageEvent = () => {
const sessionStorageData = JSON.parse(window.sessionStorage.getItem('viewingUser'));
if (sessionStorageData) {
setDisplayUserId(sessionStorageData.userId);
setFirstName(sessionStorageData.firstName);
setProfilePic(sessionStorageData.profilePic);
setIsAuthUser(false);
} else {
setDisplayUserId(user.userid);
setFirstName(props.auth.firstName);
setProfilePic(props.auth.profilePic);
setIsAuthUser(true);
}
};
// Set the initial state when the component mounts
handleStorageEvent();
// Add the event listener
window.addEventListener('storage', handleStorageEvent);
// Clean up the event listener when the component unmounts
return () => {
window.removeEventListener('storage', handleStorageEvent);
};
}, [user.userid, props.auth.firstName]);
useEffect(() => {
if (props.auth.isAuthenticated) {
props.getHeaderData(props.auth.user.userid);
if (props.auth.user.role === 'Administrator') {
dispatch(fetchTaskEditSuggestions());
}
}
}, [props.auth.isAuthenticated]);
const roles = props.role?.roles;
useEffect(() => {
if (roles.length === 0) {
props.getAllRoles();
}
}, []);
const openModal = () => {
setLogoutPopup(true);
};
const toggle = () => {
setIsOpen(prevIsOpen => !prevIsOpen);
};
useEffect(() => {
const handleClickOutside = event => {
if (
collapseRef.current &&
!collapseRef.current.contains(event.target) &&
!toggleRef.current?.contains(event.target)
) {
setIsOpen(false);
}
};
if (isOpen) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [isOpen]);
const fontColor = darkMode
? `${styles.darkDropdownText} ${styles.darkDropdownItem}`
: `${styles.mobileDropdownText} ${styles.mobileDropdownItem}`;
return (
<div className={styles.headerWrapper}>
{isAuthenticated && (
<div className={`${styles.navbarOwnerMessage} ${styles.showInTablet} bg-dark pt-3 `}>
<OwnerMessage />
</div>
)}
<Navbar className={`py-3 ${styles.navbar}`} color="dark" dark expand="xl">
{logoutPopup && <Logout open={logoutPopup} setLogoutPopup={setLogoutPopup} />}
{isAuthenticated && <Timer />}
{/* ITEM SHOWS OUTSIDE OF THE DROPDOWN IN MOBILE */}
<div className={`${styles.showInMobile} ml-auto mr-3`}>
<BellNotification userId={displayUserId} />
</div>
{/* --------------------------------------------- */}
<div ref={toggleRef}>
<NavbarToggler onClick={toggle} className="mr-3" />
</div>
{isAuthenticated && (
<Collapse isOpen={isOpen} navbar innerRef={collapseRef}>
{isAuthenticated && (
<div className={`${styles.hideInTablet} ${styles.navbarOwnerMessage}`}>
<OwnerMessage />
</div>
)}
<Nav className={`ml-auto ${styles.menuContainer} mr-3`} navbar>
{/* --PROFILE SHOWS ON TOP IN MOBILE VIEW */}
<NavItem className={styles.showInMobile}>
<NavLink tag={Link} to={`/userprofile/${displayUserId}`}>
<img
alt=""
src={`${profilePic || '/pfp-default-header.png'}`}
style={{ maxWidth: '60px', maxHeight: '60px' }}
className="dashboardimg"
/>
</NavLink>
</NavItem>
<UncontrolledDropdown inNavbar nav className={styles.showInMobile}>
<DropdownToggle nav caret>
<span>
{WELCOME}, {firstName}
</span>
</DropdownToggle>
<DropdownMenu
className={` ${darkMode ? styles.darkMenuDropdown : styles.mobileMenuDropdown} ${
styles.noMaxHeight
}`}
>
<DropdownItem
tag={Link}
to={`/userprofile/${displayUserId}`}
className={fontColor}
>
{VIEW_PROFILE}
</DropdownItem>
{!cantUpdateDevAdminDetails(props.userProfile.email, props.userProfile.email) && (
<DropdownItem
tag={Link}
to={`/updatepassword/${displayUserId}`}
className={fontColor}
>
{UPDATE_PASSWORD}
</DropdownItem>
)}
<DropdownItem className={fontColor}>
<DarkModeButton />
</DropdownItem>
<DropdownItem onClick={openModal} className={fontColor}>
{LOGOUT}
</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
{/* ------------------------------------- */}
{canUpdateTask && (
<NavItem>
<NavLink tag={Link} to="/taskeditsuggestions">
<div className={`${styles.redBackGroupHeader} ${styles.hideInMobile}`}>
<span>{props.taskEditSuggestionCount}</span>
</div>
{/* --- MOBILE VIEW ONLY --- */}
<span className={styles.showInMobile}>
Task Edit Suggestion ({props.taskEditSuggestionCount})
</span>
{/* ------------------- */}
</NavLink>
</NavItem>
)}
<NavItem>
<NavLink tag={Link} to="/kitchenandinventory">
<span>{DASHBOARD}</span>
</NavLink>
</NavItem>
<UncontrolledDropdown nav inNavbar>
<DropdownToggle nav caret>
<span>{PRODUCTION}</span>
</DropdownToggle>
<DropdownMenu
className={`${styles.noMaxHeight} ${
darkMode ? styles.darkMenuDropdown : styles.mobileMenuDropdown
}`}
>
<DropdownItem
tag={Link}
to="/kitchenandinventory/gardenmanagement"
className={fontColor}
>
{GARDEN_MANAGEMENT}
</DropdownItem>
<DropdownItem
tag={Link}
to="/kitchenandinventory/orchardmanagement"
className={fontColor}
>
{ORCHARD_MANAGEMENT}
</DropdownItem>
<DropdownItem
tag={Link}
to="/kitchenandinventory/animalmanagement"
className={fontColor}
>
{ANIMAL_MANAGEMENT}
</DropdownItem>
<DropdownItem
tag={Link}
to="/kitchenandinventory/animalmanagementtracking"
className={fontColor}
>
{ANIMAL_MANAGEMENT_TRACKING}
</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
<NavItem>
<NavLink tag={Link} to="/kitchenandinventory/processing">
<span>{KI_PROCESSING}</span>
</NavLink>
</NavItem>
<NavItem>
<NavLink tag={Link} to="/kitchenandinventory/recipes">
<span>{RECIPES}</span>
</NavLink>
</NavItem>
<NavItem>
<NavLink tag={Link} to="/kitchenandinventory/inventory">
<span>{KI_INVENTORY}</span>
</NavLink>
</NavItem>
<NavItem>
<NavLink tag={Link} to="/kitchenandinventory/orders">
<span>{ORDERS}</span>
</NavLink>
</NavItem>
<NavItem>
<NavLink tag={Link} to="/kitchenandinventory/reports">
<span>{KI_REPORTS}</span>
</NavLink>
</NavItem>
<NavItem>
<NavLink tag={Link} to="/kitchenandinventory/foodbars">
<span>{FOOD_BARS}</span>
</NavLink>
</NavItem>
<NavItem>
<NavLink tag={Link} to="/kitchenandinventory/calendar">
<span>{KI_CALENDAR}</span>
</NavLink>
</NavItem>
<NavItem className={styles.hideInMobile}>
<BellNotification userId={displayUserId} />
</NavItem>
<NavItem className={styles.hideInMobile}>
<NavLink tag={Link} to={`/userprofile/${displayUserId}`}>
<img
src={`${profilePic || '/pfp-default-header.png'}`}
alt=""
style={{ maxWidth: '60px', maxHeight: '60px' }}
className="dashboardimg"
/>
</NavLink>
</NavItem>
<UncontrolledDropdown nav className={styles.hideInMobile}>
<DropdownToggle nav caret>
<span>
{WELCOME}, {firstName}
</span>
</DropdownToggle>
<DropdownMenu
className={`${styles.noMaxHeight} ${
darkMode ? styles.darkMenuDropdown : styles.mobileMenuDropdown
}`}
>
<DropdownItem header className={fontColor}>
Hello {firstName}
</DropdownItem>
<DropdownItem divider />
<DropdownItem tag={Link} to={`/userprofile/${user.userid}`} className={fontColor}>
{VIEW_PROFILE}
</DropdownItem>
{!cantUpdateDevAdminDetails(props.userProfile.email, props.userProfile.email) && (
<DropdownItem
tag={Link}
to={`/updatepassword/${user.userid}`}
className={fontColor}
>
{UPDATE_PASSWORD}
</DropdownItem>
)}
<DropdownItem className={fontColor}>
<DarkModeButton />
</DropdownItem>
<DropdownItem divider />
<DropdownItem onClick={openModal} className={fontColor}>
{LOGOUT}
</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
</Nav>
</Collapse>
)}
</Navbar>
</div>
);
}
const mapStateToProps = state => ({
auth: state.auth,
userProfile: state.userProfile,
taskEditSuggestionCount: state.taskEditSuggestions.count,
role: state.role,
notification: state.notification,
darkMode: state.theme.darkMode,
});
export default connect(mapStateToProps, {
getHeaderData,
getAllRoles,
hasPermission,
})(Header);