import { useEffect, useState } from 'react'; import { Navigate } from 'react-router-dom'; import { api, formatCredits } from '../api'; import { useAuth } from '../AuthContext'; export default function ProfilePage() { const { user, loading, refresh } = useAuth(); const [profile, setProfile] = useState(null); const [bio, setBio] = useState(''); const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [message, setMessage] = useState(''); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); useEffect(() => { if (!user || user.role === 'admin') return; let cancelled = false; (async () => { try { const data = await api.profile(); if (!cancelled) { setProfile(data.profile); setBio(data.user.bio || ''); } } catch (err) { if (!cancelled) setError(err.message); } })(); return () => { cancelled = true; }; }, [user]); const saveBio = async (e) => { e.preventDefault(); setBusy(true); setError(''); setMessage(''); try { await api.updateProfile({ bio }); await refresh(); setMessage('Profile updated.'); } catch (err) { setError(err.message); } finally { setBusy(false); } }; const changePassword = async (e) => { e.preventDefault(); setBusy(true); setError(''); setMessage(''); try { await api.changePassword(currentPassword, newPassword); setCurrentPassword(''); setNewPassword(''); setMessage('Password changed.'); } catch (err) { setError(err.message); } finally { setBusy(false); } }; const onAvatar = async (e) => { const file = e.target.files?.[0]; if (!file) return; setBusy(true); setError(''); setMessage(''); try { await api.uploadAvatar(file); await refresh(); setMessage('Avatar updated.'); } catch (err) { setError(err.message); } finally { setBusy(false); e.target.value = ''; } }; if (loading) return
Loading…
; if (!user) return@{user.username}
Loading stats…
)}