'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { useNotification } from './NotificationProvider'; import useSWR from 'swr'; interface UserProfile { id: string; email: string; name: string | null; photoUrl: string | null; role: { id: string; name: string; description: string | null; } | null; } const fetcher = (url: string) => fetch(url).then((res) => res.json()); export default function ModifierCompteContent() { const router = useRouter(); const { showNotification } = useNotification(); const { data: userProfile, mutate } = useSWR('/api/user/profile', fetcher); const [loading, setLoading] = useState(false); const [formData, setFormData] = useState({ name: '', email: '', photoUrl: '', currentPassword: '', newPassword: '', confirmPassword: '', }); useEffect(() => { if (userProfile) { setFormData({ name: userProfile.name || '', email: userProfile.email || '', photoUrl: userProfile.photoUrl || '', currentPassword: '', newPassword: '', confirmPassword: '', }); } }, [userProfile]); const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value })); }; const handleImageUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { // Vérifier la taille du fichier (max 5MB) if (file.size > 5 * 1024 * 1024) { showNotification('L\'image est trop grande (max 5MB)', 'error'); return; } // Vérifier le type de fichier if (!file.type.startsWith('image/')) { showNotification('Veuillez sélectionner une image', 'error'); return; } const reader = new FileReader(); reader.onloadend = () => { setFormData((prev) => ({ ...prev, photoUrl: reader.result as string })); }; reader.readAsDataURL(file); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { // Vérifier que les mots de passe correspondent si un nouveau mot de passe est fourni if (formData.newPassword) { if (formData.newPassword !== formData.confirmPassword) { showNotification('Les mots de passe ne correspondent pas', 'error'); setLoading(false); return; } if (formData.newPassword.length < 6) { showNotification('Le mot de passe doit contenir au moins 6 caractères', 'error'); setLoading(false); return; } } const updateData: any = { name: formData.name.trim() || null, email: formData.email.trim(), photoUrl: formData.photoUrl || null, }; if (formData.newPassword) { updateData.currentPassword = formData.currentPassword; updateData.newPassword = formData.newPassword; } const response = await fetch('/api/user/profile', { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(updateData), }); const data = await response.json(); if (response.ok) { showNotification('Profil mis à jour avec succès', 'success'); mutate(); // Réinitialiser les champs de mot de passe setFormData((prev) => ({ ...prev, currentPassword: '', newPassword: '', confirmPassword: '', })); } else { showNotification(data.error || 'Erreur lors de la mise à jour', 'error'); } } catch (error) { console.error('Erreur:', error); showNotification('Erreur lors de la mise à jour', 'error'); } finally { setLoading(false); } }; const getUserInitials = () => { if (userProfile?.name) { const names = userProfile.name.split(' '); if (names.length >= 2) { return `${names[0].charAt(0)}${names[1].charAt(0)}`.toUpperCase(); } return userProfile.name.charAt(0).toUpperCase(); } return userProfile?.email?.charAt(0).toUpperCase() || 'U'; }; if (!userProfile) { return (
Chargement...
); } return (

Modifier mon compte

Mettez à jour vos informations personnelles et votre mot de passe

{/* Photo de profil */}
{formData.photoUrl ? ( Photo de profil ) : (
{getUserInitials()}
)}
{formData.photoUrl && ( )}

JPG, PNG ou GIF (max 5MB)

{/* Nom et prénom */}
{/* Email */}
{/* Section changement de mot de passe */}

Changer le mot de passe

Laissez ces champs vides si vous ne souhaitez pas changer votre mot de passe

{/* Boutons d'action */}
); }