diff --git a/src/components/Profile/ProfilePage.js b/src/components/Profile/ProfilePage.js index c5f7d89..7aca076 100644 --- a/src/components/Profile/ProfilePage.js +++ b/src/components/Profile/ProfilePage.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import '../../styles/components.css'; const ProfileRow = ({ label, value, icon }) => ( @@ -15,14 +15,103 @@ const ProfileRow = ({ label, value, icon }) => ( ); +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 = () => { - // En esta versión demo, la información del usuario es estática. - // En una integración real, estos datos vendrían del backend o del contexto de autenticación. - const user = { - initials: 'JP', - name: 'Juan Pérez', - role: 'Administrador', + 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({ + username: profile.username, + firstName: profile.firstName, + lastName: profile.lastName, + password: '', + }); + + useEffect(() => { + if (showModal) { + setForm({ + username: profile.username, + 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.username.trim() || !form.firstName.trim() || !form.lastName.trim()) { + alert('Por favor, completa los campos: usuario, 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, + username: form.username.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 ( @@ -33,12 +122,17 @@ const ProfilePage = () => { Perfil de usuario +