import React, { useEffect, useMemo, useState } from 'react';
import '../../styles/components.css';
const ProfileRow = ({ label, value, icon }) => (
{value}
);
const storageKey = 'user.profile.v1';
const loadProfile = () => {
try {
const raw = localStorage.getItem(storageKey);
if (!raw) return null;
return JSON.parse(raw);
} catch (_e) {
return null;
}
};
const saveProfile = (profile) => {
try {
localStorage.setItem(storageKey, JSON.stringify(profile));
} catch (_e) {}
};
const computeInitials = (firstName = '', lastName = '') => {
const a = (firstName || '').trim().charAt(0).toUpperCase();
const b = (lastName || '').trim().charAt(0).toUpperCase();
return `${a}${b || ''}` || 'U';
};
const ProfilePage = () => {
const defaultProfile = useMemo(() => ({
username: 'jperez',
firstName: 'Juan',
lastName: 'Pérez',
email: 'juan.perez@empresa.com',
role: 'Administrador',
}), []);
const [profile, setProfile] = useState(() => loadProfile() || defaultProfile);
const [showModal, setShowModal] = useState(false);
useEffect(() => {
saveProfile(profile);
}, [profile]);
const initials = computeInitials(profile.firstName, profile.lastName);
const [form, setForm] = useState({
email: profile.email,
firstName: profile.firstName,
lastName: profile.lastName,
password: '',
});
useEffect(() => {
if (showModal) {
setForm({
email: profile.email,
firstName: profile.firstName,
lastName: profile.lastName,
password: '',
});
}
}, [showModal, profile]);
const openModal = () => setShowModal(true);
const closeModal = () => setShowModal(false);
const handleChange = (e) => {
const { name, value } = e.target;
setForm((prev) => ({ ...prev, [name]: value }));
};
const handleSave = (e) => {
e.preventDefault();
// Validaciones básicas
if (!form.email.trim() || !form.firstName.trim() || !form.lastName.trim()) {
alert('Por favor, completa los campos: correo, nombre y apellido.');
return;
}
if (form.password && form.password.length < 6) {
alert('La contraseña debe tener al menos 6 caracteres.');
return;
}
// En una app real, aquí llamarías a la API para actualizar perfil y contraseña.
setProfile((prev) => ({
...prev,
email: form.email.trim(),
firstName: form.firstName.trim(),
lastName: form.lastName.trim(),
// La contraseña NO se guarda en localStorage; solo se simula el flujo.
}));
if (form.password) {
// Simulación de actualización de contraseña
console.log('Contraseña actualizada (simulado).');
}
setShowModal(false);
};
return (
Perfil de usuario
{initials}
{profile.firstName} {profile.lastName}
{profile.role}
{showModal && (
e.stopPropagation()}>
Actualizar perfil
)}
);
};
export default ProfilePage;