[79a0317] | 1 | import React, { useState, useRef, useEffect } from "react";
|
---|
| 2 | import ReactDOM from "react-dom";
|
---|
| 3 | import { useNavigate } from "react-router-dom";
|
---|
[d565449] | 4 | import profile from "../../assets/person_icon.png";
|
---|
| 5 | import styles from "./Profile.module.css";
|
---|
[0c6b92a] | 6 | import { useAppContext } from "../AppContext/AppContext.jsx";
|
---|
[d565449] | 7 |
|
---|
[0c6b92a] | 8 | function Profile({ position = "fixed" }) {
|
---|
| 9 | const { username, isAuthenticated } = useAppContext();
|
---|
| 10 | const [open, setOpen] = useState(false);
|
---|
[79a0317] | 11 | const [menuPosition, setMenuPosition] = useState({ top: 0, left: 0 });
|
---|
[0c6b92a] | 12 | const imgRef = useRef(null);
|
---|
| 13 | const navigate = useNavigate();
|
---|
| 14 |
|
---|
| 15 | const menus = isAuthenticated ? ["My Maps", "Logout"] : ["Login"];
|
---|
| 16 |
|
---|
| 17 | useEffect(() => {
|
---|
| 18 | const handleClickOutside = (e) => {
|
---|
[79a0317] | 19 | if (imgRef.current && !imgRef.current.contains(e.target)) {
|
---|
| 20 | setOpen(false);
|
---|
[0c6b92a] | 21 | }
|
---|
| 22 | };
|
---|
[d565449] | 23 |
|
---|
[0c6b92a] | 24 | document.addEventListener("click", handleClickOutside);
|
---|
[d565449] | 25 |
|
---|
[0c6b92a] | 26 | return () => {
|
---|
| 27 | document.removeEventListener("click", handleClickOutside);
|
---|
| 28 | };
|
---|
| 29 | }, []);
|
---|
| 30 |
|
---|
[79a0317] | 31 | const toggleMenu = () => {
|
---|
| 32 | if (imgRef.current) {
|
---|
| 33 | const rect = imgRef.current.getBoundingClientRect();
|
---|
| 34 | setMenuPosition({
|
---|
| 35 | top: rect.bottom + window.scrollY,
|
---|
| 36 | left: rect.left + window.scrollX,
|
---|
| 37 | });
|
---|
| 38 | }
|
---|
| 39 | setOpen(!open);
|
---|
| 40 | };
|
---|
| 41 |
|
---|
[0c6b92a] | 42 | const handleMenuClick = (menu) => {
|
---|
| 43 | if (menu === "My Maps") {
|
---|
| 44 | navigate("/MyMaps");
|
---|
| 45 | } else if (menu === "Logout") {
|
---|
| 46 | localStorage.removeItem("token");
|
---|
| 47 | window.location.reload();
|
---|
[79a0317] | 48 | } else if (menu === "Login") {
|
---|
| 49 | navigate("/Login");
|
---|
[0c6b92a] | 50 | }
|
---|
| 51 | setOpen(false);
|
---|
[d565449] | 52 | };
|
---|
[0c6b92a] | 53 |
|
---|
[79a0317] | 54 | const renderDropdown = () => {
|
---|
| 55 | if (!open) return null;
|
---|
| 56 |
|
---|
| 57 | return ReactDOM.createPortal(
|
---|
| 58 | <div
|
---|
| 59 | className={styles.dropdownMenu}
|
---|
| 60 | style={{
|
---|
| 61 | position: "absolute",
|
---|
| 62 | top: menuPosition.top,
|
---|
| 63 | left: menuPosition.left,
|
---|
| 64 | }}
|
---|
| 65 | >
|
---|
| 66 | {isAuthenticated && <div className={styles.username}>{username}</div>}
|
---|
| 67 | <ul className={styles.menuList}>
|
---|
| 68 | {menus.map((menu) => (
|
---|
| 69 | <li
|
---|
| 70 | key={menu}
|
---|
| 71 | className={styles.menuItem}
|
---|
| 72 | onClick={() => handleMenuClick(menu)}
|
---|
| 73 | >
|
---|
| 74 | {menu}
|
---|
| 75 | </li>
|
---|
| 76 | ))}
|
---|
| 77 | </ul>
|
---|
| 78 | </div>,
|
---|
| 79 | document.body
|
---|
| 80 | );
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[0c6b92a] | 83 | return (
|
---|
[79a0317] | 84 | <div
|
---|
| 85 | className={
|
---|
| 86 | position === "fixed" ? styles.fixedProfileContainer : styles.inlineProfileContainer
|
---|
| 87 | }
|
---|
| 88 | >
|
---|
[0c6b92a] | 89 | <div className={styles.profileWrapper}>
|
---|
[79a0317] | 90 | <div
|
---|
| 91 | className={styles.profileIconContainer}
|
---|
| 92 | onClick={toggleMenu}
|
---|
| 93 | ref={imgRef}
|
---|
| 94 | >
|
---|
| 95 | <img src={profile} alt="profile" className={styles.profileImage} />
|
---|
[0c6b92a] | 96 | </div>
|
---|
[79a0317] | 97 | {renderDropdown()}
|
---|
[0c6b92a] | 98 | </div>
|
---|
| 99 | </div>
|
---|
| 100 | );
|
---|
[d565449] | 101 | }
|
---|
| 102 |
|
---|
| 103 | export default Profile;
|
---|