[0c6b92a] | 1 | import React, { useState, useRef, useEffect, useContext } from "react";
|
---|
| 2 | import { useNavigate, Link } from "react-router-dom";
|
---|
[d565449] | 3 | import profile from "../../assets/person_icon.png";
|
---|
| 4 | import styles from "./Profile.module.css";
|
---|
[0c6b92a] | 5 | import { useAppContext } from "../AppContext/AppContext.jsx";
|
---|
[d565449] | 6 |
|
---|
[0c6b92a] | 7 | function Profile({ position = "fixed" }) {
|
---|
| 8 | const { username, isAuthenticated } = useAppContext();
|
---|
| 9 | const [open, setOpen] = useState(false);
|
---|
| 10 | const menuRef = useRef(null);
|
---|
| 11 | const imgRef = useRef(null);
|
---|
| 12 | const navigate = useNavigate();
|
---|
| 13 |
|
---|
| 14 | const menus = isAuthenticated ? ["My Maps", "Logout"] : ["Login"];
|
---|
| 15 |
|
---|
| 16 | useEffect(() => {
|
---|
| 17 | const handleClickOutside = (e) => {
|
---|
| 18 | if (menuRef.current && imgRef.current) {
|
---|
| 19 | if (!menuRef.current.contains(e.target) && !imgRef.current.contains(e.target)) {
|
---|
| 20 | setOpen(false);
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 | };
|
---|
[d565449] | 24 |
|
---|
[0c6b92a] | 25 | document.addEventListener("click", handleClickOutside);
|
---|
[d565449] | 26 |
|
---|
[0c6b92a] | 27 | return () => {
|
---|
| 28 | document.removeEventListener("click", handleClickOutside);
|
---|
| 29 | };
|
---|
| 30 | }, []);
|
---|
| 31 |
|
---|
| 32 | const handleMenuClick = (menu) => {
|
---|
| 33 | if (menu === "My Maps") {
|
---|
| 34 | navigate("/MyMaps");
|
---|
| 35 | } else if (menu === "Logout") {
|
---|
| 36 | localStorage.removeItem("token");
|
---|
| 37 | window.location.reload();
|
---|
| 38 | }
|
---|
| 39 | setOpen(false);
|
---|
[d565449] | 40 | };
|
---|
[0c6b92a] | 41 |
|
---|
| 42 | return (
|
---|
| 43 | <div className={position === "fixed" ? styles.fixedProfileContainer : styles.inlineProfileContainer}>
|
---|
| 44 | <div className={styles.profileWrapper}>
|
---|
| 45 | <div className={styles.profileIconContainer} onClick={() => setOpen(!open)}>
|
---|
| 46 | <img src={profile} alt="profile" className={styles.profileImage} ref={imgRef} />
|
---|
| 47 | </div>
|
---|
| 48 | {open && (
|
---|
| 49 | <div ref={menuRef} className={styles.dropdownMenu}>
|
---|
| 50 | {isAuthenticated && <div className={styles.username}>{username}</div>}
|
---|
| 51 | <ul className={styles.menuList}>
|
---|
| 52 | {menus.map((menu) =>
|
---|
| 53 | menu === "Login" ? (
|
---|
| 54 | <li key={menu} className={styles.menuItem}>
|
---|
| 55 | <Link to="/login" className={styles.linkStyle}>{menu}</Link>
|
---|
| 56 | </li>
|
---|
| 57 | ) : (
|
---|
| 58 | <li key={menu} onClick={() => handleMenuClick(menu)} className={styles.menuItem}>
|
---|
| 59 | {menu}
|
---|
| 60 | </li>
|
---|
| 61 | )
|
---|
| 62 | )}
|
---|
| 63 | </ul>
|
---|
| 64 | </div>
|
---|
| 65 | )}
|
---|
| 66 | </div>
|
---|
| 67 | </div>
|
---|
| 68 | );
|
---|
[d565449] | 69 | }
|
---|
| 70 |
|
---|
| 71 | export default Profile;
|
---|