import React, { useState, useRef, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { authService } from '../services/membershipService'; interface ProfileMenuProps { userName: string; } const ProfileMenu: React.FC = ({ userName }) => { const [isOpen, setIsOpen] = useState(false); const [showChangePassword, setShowChangePassword] = useState(false); const menuRef = useRef(null); const navigate = useNavigate(); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (menuRef.current && !menuRef.current.contains(event.target as Node)) { setIsOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, []); const handleLogout = () => { authService.logout(); navigate('/login'); }; const handleChangePassword = () => { setShowChangePassword(true); setIsOpen(false); }; const handleCloseChangePassword = () => { setShowChangePassword(false); }; const dropdownStyle: React.CSSProperties = { position: 'absolute', top: '100%', right: 0, background: 'white', border: '1px solid #ddd', borderRadius: '4px', boxShadow: '0 2px 8px rgba(0,0,0,0.1)', minWidth: '160px', zIndex: 1000, }; const menuItemStyle: React.CSSProperties = { display: 'block', width: '100%', padding: '12px 16px', background: 'none', border: 'none', textAlign: 'left', cursor: 'pointer', color: '#333', fontSize: '14px', }; return ( <>
{isOpen && (
)}
{showChangePassword && ( )} ); }; interface ChangePasswordModalProps { onClose: () => void; } const ChangePasswordModal: React.FC = ({ onClose }) => { const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (newPassword !== confirmPassword) { setError('New passwords do not match'); return; } if (newPassword.length < 8) { setError('New password must be at least 8 characters long'); return; } setLoading(true); setError(''); try { await authService.changePassword({ current_password: currentPassword, new_password: newPassword }); alert('Password changed successfully!'); onClose(); } catch (error: any) { setError(error.response?.data?.detail || 'Failed to change password'); } finally { setLoading(false); } }; return (

Change Password

setCurrentPassword(e.target.value)} required />
setNewPassword(e.target.value)} required minLength={8} />
setConfirmPassword(e.target.value)} required minLength={8} />
{error && (
{error}
)}
); }; export default ProfileMenu;