From 7a196010214db6a1789760958eee5228374a7745 Mon Sep 17 00:00:00 2001 From: Alberto Duarte Delgado Date: Mon, 30 Mar 2026 18:12:23 +0100 Subject: logica perfil de usuario --- src/features/profile/hooks/useUpdateProfile.ts | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/features/profile/hooks/useUpdateProfile.ts (limited to 'src/features/profile/hooks/useUpdateProfile.ts') diff --git a/src/features/profile/hooks/useUpdateProfile.ts b/src/features/profile/hooks/useUpdateProfile.ts new file mode 100644 index 0000000..505bfbb --- /dev/null +++ b/src/features/profile/hooks/useUpdateProfile.ts @@ -0,0 +1,52 @@ +// src/features/profile/hooks/useUpdateProfile.ts + +import { useState } from 'react'; +import { profileService } from '@/features/profile/services/profileService'; +import { useAuthSession } from '@/providers/AuthProvider'; + +export function useUpdateProfile(initialDisplayName = '') { + const { user } = useAuthSession(); + + const [displayName, setDisplayName] = useState(initialDisplayName); + const [isSubmitting, setIsSubmitting] = useState(false); + const [error, setError] = useState(null); + const [successMessage, setSuccessMessage] = useState(null); + + const submit = async () => { + if (!user?.uid) { + setError('No hay usuario autenticado'); + return; + } + + if (!displayName.trim()) { + setError('El nombre no puede estar vacĂ­o'); + return; + } + + try { + setError(null); + setSuccessMessage(null); + setIsSubmitting(true); + + await profileService.updateMyProfile({ + uid: user.uid, + displayName: displayName.trim(), + }); + + setSuccessMessage('Perfil actualizado correctamente'); + } catch (err: any) { + setError(err?.message ?? 'No se pudo actualizar el perfil'); + } finally { + setIsSubmitting(false); + } + }; + + return { + displayName, + setDisplayName, + isSubmitting, + error, + successMessage, + submit, + }; +} -- cgit v1.2.3-54-g00ecf