import { useState } from 'react' import UserNav from '../components/UserNav' import { changePasswordApi} from '../services/auth' import { useHandleAuthError } from '../hooks/useHandleAuthError' export default function Profile() { const user = JSON.parse(localStorage.getItem('user') || '{}') const initials = `${(user.firstname || '?')[0]}${(user.lastname || '?')[0]}`.toUpperCase() const handleAuthError = useHandleAuthError() const [pwForm, setPwForm] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' }) const [showPw, setShowPw] = useState({ current: false, new: false, confirm: false }) const [pwLoading, setPwLoading] = useState(false) const [pwError, setPwError] = useState('') const [pwSuccess, setPwSuccess] = useState(false) const handlePwChange = (e) => setPwForm({ ...pwForm, [e.target.name]: e.target.value }) const handlePwSubmit = async (e) => { e.preventDefault() setPwError('') setPwSuccess(false) if (pwForm.newPassword !== pwForm.confirmPassword) { setPwError('Les nouveaux mots de passe ne correspondent pas.') return } if (pwForm.newPassword.length < 8) { setPwError('Le nouveau mot de passe doit faire au moins 8 caractères.') return } setPwLoading(true) try { await changePasswordApi({ currentPassword: pwForm.currentPassword, newPassword: pwForm.newPassword }) setPwSuccess(true) setPwForm({ currentPassword: '', newPassword: '', confirmPassword: '' }) } catch (err) { if (!handleAuthError(err)) setPwError(err.message) } finally { setPwLoading(false) } } const EyeIcon = ({ visible }) => ( {visible ? <> : } ) return (
{/* Avatar */}
{initials}
{/* Infos du compte (lecture seule) */}
{user.email || '—'}
{user.lastname || '—'}
{user.firstname || '—'}
{/* Séparateur */}
{/* Section changement de mot de passe */}

Changer mon mot de passe

{pwSuccess && (
Mot de passe modifié avec succès.
)} {pwError && (
{pwError}
)}
{[ { name: 'currentPassword', label: 'Mot de passe actuel', key: 'current' }, { name: 'newPassword', label: 'Nouveau mot de passe', key: 'new' }, { name: 'confirmPassword', label: 'Confirmer le nouveau mot de passe', key: 'confirm' }, ].map(({ name, label, key }) => (
))}
) }