blob: 505bfbbe5c40e044e3f4b636788aa15913e3a73b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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,
};
}
|