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 }) => ( ) return (