import React, { useState } from 'react'; import { User, Mail, Phone, Lock, Globe, Clock, Camera, Save, X } from 'lucide-react'; import { Button, Card, Avatar, Input, Select } from '../../../../components/ui'; import { PageHeader } from '../../../../components/layout'; import { useAuthUser } from '../../../../stores/auth.store'; import { useToast } from '../../../../hooks/ui/useToast'; import { useAuthProfile, useUpdateProfile, useChangePassword } from '../../../../api/hooks/auth'; interface ProfileFormData { first_name: string; last_name: string; email: string; phone: string; language: string; timezone: string; } interface PasswordData { currentPassword: string; newPassword: string; confirmPassword: string; } const ProfilePage: React.FC = () => { const user = useAuthUser(); const { addToast } = useToast(); const { data: profile, isLoading: profileLoading, error: profileError } = useAuthProfile(); const updateProfileMutation = useUpdateProfile(); const changePasswordMutation = useChangePassword(); const [isEditing, setIsEditing] = useState(false); const [isLoading, setIsLoading] = useState(false); const [showPasswordForm, setShowPasswordForm] = useState(false); const [profileData, setProfileData] = useState({ first_name: '', last_name: '', email: '', phone: '', language: 'es', timezone: 'Europe/Madrid' }); // Update profile data when profile is loaded React.useEffect(() => { if (profile) { setProfileData({ first_name: profile.first_name || '', last_name: profile.last_name || '', email: profile.email || '', phone: profile.phone || '', language: profile.language || 'es', timezone: profile.timezone || 'Europe/Madrid' }); } }, [profile]); const [passwordData, setPasswordData] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' }); const [errors, setErrors] = useState>({}); const languageOptions = [ { value: 'es', label: 'Español' }, { value: 'ca', label: 'Català' }, { value: 'en', label: 'English' } ]; const timezoneOptions = [ { value: 'Europe/Madrid', label: 'Madrid (CET/CEST)' }, { value: 'Atlantic/Canary', label: 'Canarias (WET/WEST)' }, { value: 'Europe/London', label: 'Londres (GMT/BST)' } ]; const validateProfile = (): boolean => { const newErrors: Record = {}; if (!profileData.first_name.trim()) { newErrors.first_name = 'El nombre es requerido'; } if (!profileData.last_name.trim()) { newErrors.last_name = 'Los apellidos son requeridos'; } if (!profileData.email.trim()) { newErrors.email = 'El email es requerido'; } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(profileData.email)) { newErrors.email = 'Email inválido'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const validatePassword = (): boolean => { const newErrors: Record = {}; if (!passwordData.currentPassword) { newErrors.currentPassword = 'Contraseña actual requerida'; } if (!passwordData.newPassword) { newErrors.newPassword = 'Nueva contraseña requerida'; } else if (passwordData.newPassword.length < 8) { newErrors.newPassword = 'Mínimo 8 caracteres'; } if (passwordData.newPassword !== passwordData.confirmPassword) { newErrors.confirmPassword = 'Las contraseñas no coinciden'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSaveProfile = async () => { if (!validateProfile()) return; setIsLoading(true); try { await updateProfileMutation.mutateAsync(profileData); setIsEditing(false); addToast('Perfil actualizado correctamente', 'success'); } catch (error) { addToast('No se pudo actualizar tu perfil', 'error'); } finally { setIsLoading(false); } }; const handleChangePasswordSubmit = async () => { if (!validatePassword()) return; setIsLoading(true); try { await changePasswordMutation.mutateAsync({ current_password: passwordData.currentPassword, new_password: passwordData.newPassword, confirm_password: passwordData.confirmPassword }); setShowPasswordForm(false); setPasswordData({ currentPassword: '', newPassword: '', confirmPassword: '' }); addToast('Contraseña actualizada correctamente', 'success'); } catch (error) { addToast('No se pudo cambiar tu contraseña', 'error'); } finally { setIsLoading(false); } }; const handleInputChange = (field: keyof ProfileFormData) => (e: React.ChangeEvent) => { setProfileData(prev => ({ ...prev, [field]: e.target.value })); if (errors[field]) { setErrors(prev => ({ ...prev, [field]: '' })); } }; const handleSelectChange = (field: keyof ProfileFormData) => (value: string) => { setProfileData(prev => ({ ...prev, [field]: value })); }; const handlePasswordChange = (field: keyof PasswordData) => (e: React.ChangeEvent) => { setPasswordData(prev => ({ ...prev, [field]: e.target.value })); if (errors[field]) { setErrors(prev => ({ ...prev, [field]: '' })); } }; return (
{/* Profile Header */}

{profileData.first_name} {profileData.last_name}

{profileData.email}

En línea
{!isEditing && ( )}
{/* Profile Form */}

Información Personal

} /> } /> } /> } />
{isEditing && (
)}
{/* Password Change Form */} {showPasswordForm && (

Cambiar Contraseña

} /> } /> } />
)}
); }; export default ProfilePage;