import { useState, useEffect } from "react"; import { ProfileService } from "../services/profile.service"; import { useAuth } from "../context/AuthContext"; import { AuthService } from "../services/auth.service"; import { NotificationPreferencesService, type NotificationPreferences, } from "../services/notification-preferences.service"; export default function PerfilPage() { const { user, refreshSession } = useAuth(); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [showEmailModal, setShowEmailModal] = useState(false); const [newEmail, setNewEmail] = useState(""); const [authCode, setAuthCode] = useState(""); // Código Google Authenticator const [loadingEmail, setLoadingEmail] = useState(false); const [formData, setFormData] = useState({ firstName: "", lastName: "", phoneNumber: "", }); // Estado de preferencias de notificación const [notifPrefs, setNotifPrefs] = useState({ sistema: true, marketing: true, rendimiento: true, mensajes: true, }); const [savingPrefs, setSavingPrefs] = useState(false); const [prefsSaved, setPrefsSaved] = useState(false); const [loadingPrefs, setLoadingPrefs] = useState(true); useEffect(() => { loadProfile(); loadNotifPrefs(); }, []); const loadNotifPrefs = async () => { try { const data = await NotificationPreferencesService.getPreferences(); setNotifPrefs(data); } catch (err) { console.error("Error cargando preferencias de notificación", err); } finally { setLoadingPrefs(false); } }; const handleSaveNotifPrefs = async () => { setSavingPrefs(true); try { await NotificationPreferencesService.updatePreferences(notifPrefs); setPrefsSaved(true); setTimeout(() => setPrefsSaved(false), 3000); } catch (err) { alert("Error al guardar las preferencias de notificación."); } finally { setSavingPrefs(false); } }; const loadProfile = async () => { try { const data = await ProfileService.getProfile(); setFormData({ firstName: data.firstName || "", lastName: data.lastName || "", phoneNumber: data.phoneNumber || "", }); } catch (err) { console.error("Error loading profile", err); } finally { setLoading(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); try { await ProfileService.updateProfile(formData); alert("Perfil actualizado con éxito"); if (refreshSession) refreshSession(); } catch (err) { alert("Error al actualizar el perfil"); } finally { setSaving(false); } }; if (loading) { return (
); } return (

Mi Perfil

Gestiona tu información personal

{/* Sidebar Info */}
{user?.username?.[0].toUpperCase()}

{user?.username}

{user?.email}

{user?.userType === 3 ? "🛡️ Administrador" : "👤 Usuario Particular"}
{/* Formulario de datos personales */}
setFormData({ ...formData, firstName: e.target.value }) } className="w-full bg-white/5 border border-white/10 rounded-2xl px-5 py-4 text-sm text-white outline-none focus:border-blue-500 transition-all font-medium" placeholder="Tu nombre" />
setFormData({ ...formData, lastName: e.target.value }) } className="w-full bg-white/5 border border-white/10 rounded-2xl px-5 py-4 text-sm text-white outline-none focus:border-blue-500 transition-all font-medium" placeholder="Tu apellido" />
setFormData({ ...formData, phoneNumber: e.target.value }) } className="w-full bg-white/5 border border-white/10 rounded-2xl px-5 py-4 text-sm text-white outline-none focus:border-blue-500 transition-all font-medium" placeholder="Ej: +54 9 11 1234 5678" />
{/* ─── Preferencias de Notificación ─── */}
🔔

Preferencias de Notificación

Elegir qué correos querés recibir

{loadingPrefs ? (
Cargando preferencias...
) : (
{([ { key: "sistema" as const, label: "Avisos del Sistema", desc: "Vencimiento de avisos, renovaciones y alertas importantes.", icon: "⚙️", }, { key: "marketing" as const, label: "Promociones y Marketing", desc: "Ofertas especiales, recordatorio de carrito abandonado.", icon: "🎁", }, { key: "rendimiento" as const, label: "Resumen Semanal", desc: "Visitas y favoritos de tus avisos publicados.", icon: "📊", }, { key: "mensajes" as const, label: "Recordatorio de Mensajes", desc: "Aviso cuando tenés mensajes sin leer por más de 4 horas.", icon: "💬", }, ] as const).map(({ key, label, desc, icon }) => (
{showEmailModal && (
📧

Cambiar Email

Ingresa tu nueva dirección y valida con tu autenticador.

setNewEmail(e.target.value)} className="w-full bg-black/40 border border-white/10 rounded-xl px-4 py-3 text-white text-sm outline-none focus:border-blue-500 transition-all placeholder:text-gray-700" />
setAuthCode(e.target.value.replace(/\D/g, "")) } className="w-full bg-black/40 border border-white/10 rounded-xl px-4 py-3 text-white text-center font-mono text-xl tracking-[0.3em] outline-none focus:border-blue-500 transition-all placeholder:text-gray-700 placeholder:tracking-normal" />
)}
); }