import React, { useState, useEffect } from 'react'; import { User, Mail, Phone, Lock, Save, Heart, LogOut, Camera } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; import Button from '../components/common/Button'; import Card, { CardContent, CardHeader } from '../components/common/Card'; import { useAuth } from '../contexts/AuthContext'; import { api } from '../services/api'; import { usePageTitle } from '../hooks/usePageTitle'; import { useToast } from '../contexts/ToastContext'; import { useI18n } from '../contexts/I18nContext'; const Profile: React.FC = () => { const { t } = useI18n(); usePageTitle('My Account'); const { user, logout, refreshUser } = useAuth(); const { showToast } = useToast(); const navigate = useNavigate(); const [activeTab, setActiveTab] = useState<'profile' | 'password'>('profile'); const [isLoading, setIsLoading] = useState(false); const [success, setSuccess] = useState(''); const [error, setError] = useState(''); const [avatarUrl, setAvatarUrl] = useState(user?.avatarUrl); const [uploadingAvatar, setUploadingAvatar] = useState(false); // Keep avatarUrl in sync when user context refreshes useEffect(() => { setAvatarUrl(user?.avatarUrl); }, [user?.avatarUrl]); const [profile, setProfile] = useState({ name: user?.name || '', email: user?.email || '', phone: user?.phone || '', }); const [passwords, setPasswords] = useState({ currentPassword: '', newPassword: '', confirmPassword: '', }); // Fetch latest user data on mount (to get phone and any other fields) useEffect(() => { api.users.me() .then((data: any) => { setProfile({ name: data.name || '', email: data.email || '', phone: data.phone || '', }); }) .catch(() => {}); }, []); const handleProfileChange = (e: React.ChangeEvent) => { setProfile((prev) => ({ ...prev, [e.target.name]: e.target.value })); }; const handleAvatarUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setUploadingAvatar(true); try { const result = await api.uploads.image(file); const url = result.url || result.urls?.[0]; if (url) { await api.users.updateMe({ avatar_url: url }); await refreshUser(); setAvatarUrl(url); showToast(t.profile.avatarUpdated, 'success'); } } catch (err: any) { showToast(err.message || 'Failed to upload avatar', 'error'); } finally { setUploadingAvatar(false); } }; const handlePasswordChange = (e: React.ChangeEvent) => { setPasswords((prev) => ({ ...prev, [e.target.name]: e.target.value })); }; const handleProfileSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); setError(''); setSuccess(''); try { await api.users.updateMe({ name: profile.name, email: profile.email, phone: profile.phone || undefined }); await refreshUser(); setSuccess(t.profile.profileUpdated); showToast(t.profile.profileUpdated, 'success'); } catch (err: any) { setError(err.message || 'Failed to update profile'); showToast(err.message || 'Failed to update profile', 'error'); } finally { setIsLoading(false); } }; const handlePasswordSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setSuccess(''); if (passwords.newPassword !== passwords.confirmPassword) { setError(t.profile.passwordsMismatch); return; } if (passwords.newPassword.length < 8) { setError(t.profile.passwordTooShort); return; } setIsLoading(true); try { await api.auth.changePassword(passwords.currentPassword, passwords.newPassword); setSuccess(t.profile.passwordChanged); showToast(t.profile.passwordChanged, 'success'); setPasswords({ currentPassword: '', newPassword: '', confirmPassword: '' }); } catch (err: any) { setError(err.message || 'Failed to change password'); showToast(err.message || 'Failed to change password', 'error'); } finally { setIsLoading(false); } }; const handleLogout = () => { logout(); navigate('/login'); }; if (!user) { navigate('/login'); return null; } return (

{t.profile.title}

{/* Profile Header */}
); }; export default Profile;