Changeset 0c6b92a for imaps-frontend/src/components/Profile/Profile.jsx
- Timestamp:
- 12/12/24 17:06:06 (5 weeks ago)
- Branches:
- main
- Parents:
- d565449
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/src/components/Profile/Profile.jsx
rd565449 r0c6b92a 1 import React, { useState, useRef, useEffect } from "react"; 1 import React, { useState, useRef, useEffect, useContext } from "react"; 2 import { useNavigate, Link } from "react-router-dom"; 2 3 import profile from "../../assets/person_icon.png"; 3 4 import styles from "./Profile.module.css"; 5 import { useAppContext } from "../AppContext/AppContext.jsx"; 4 6 5 function Profile() { 6 const menus = ["Profile", "Settings", "Support", "Logout"]; 7 const [open, setOpen] = useState(false); 8 const menuRef = useRef(null); 9 const imgRef = useRef(null); 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(); 10 13 11 useEffect(() => { 12 const handleClickOutside = (e) => { 13 if (menuRef.current && imgRef.current) { 14 if (!menuRef.current.contains(e.target) && !imgRef.current.contains(e.target)) { 15 setOpen(false); 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 }; 24 25 document.addEventListener("click", handleClickOutside); 26 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(); 16 38 } 17 }39 setOpen(false); 18 40 }; 19 41 20 document.addEventListener("click", handleClickOutside); 21 22 return () => { 23 document.removeEventListener("click", handleClickOutside); 24 }; 25 }, []); 26 27 return ( 28 <div className={styles.profileContainer}> 29 <div className={styles.profileWrapper}> 30 <img 31 onClick={() => setOpen(!open)} 32 src={profile} 33 alt="profile" 34 className={styles.profileImage} 35 ref={imgRef} 36 /> 37 {open && ( 38 <div ref={menuRef} className={styles.dropdownMenu}> 39 <ul className={styles.menuList}> 40 {menus.map((menu) => ( 41 <li key={menu} onClick={() => setOpen(false)} className={styles.menuItem}> 42 {menu} 43 </li> 44 ))} 45 </ul> 46 </div> 47 )} 48 </div> 49 </div> 50 ); 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 ); 51 69 } 52 70
Note:
See TracChangeset
for help on using the changeset viewer.