diff options
Diffstat (limited to 'src/features/profile/hooks/useUpdateProfile.ts')
| -rw-r--r-- | src/features/profile/hooks/useUpdateProfile.ts | 52 |
1 files changed, 52 insertions, 0 deletions
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<string | null>(null); + const [successMessage, setSuccessMessage] = useState<string | null>(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, + }; +} |