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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
// src/features/profile/hooks/useUpdateProfile.ts
import { useState } from 'react';
import { profileService } from '@/features/profile/services/profileService';
import { useAuthSession } from '@/providers/AuthProvider';
import { getErrorMessage } from '@/shared/lib/errors/getErrorMessage';
const MAX_BIO_LENGTH = 200;
export function useUpdateProfile(
initialDisplayName = '',
initialPhotoURL = '',
initialPhone = '',
initialCompany = '',
initialBio = '',
) {
const { user } = useAuthSession();
const [displayName, setDisplayName] = useState(initialDisplayName);
const [photoURL, setPhotoURL] = useState(initialPhotoURL);
const [phone, setPhone] = useState(initialPhone);
const [company, setCompany] = useState(initialCompany);
const [bio, setBio] = useState(initialBio);
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 false;
}
if (!displayName.trim()) {
setError('El nombre no puede estar vacio');
return false;
}
// validacion del campo bio antes de enviar los cambios
if (bio.trim().length > MAX_BIO_LENGTH) {
setError(`La bio no puede superar los ${MAX_BIO_LENGTH} caracteres`);
return false;
}
try {
setError(null);
setSuccessMessage(null);
setIsSubmitting(true);
await profileService.updateMyProfile({
uid: user.uid,
displayName: displayName.trim(),
photoURL: photoURL.trim(),
phone: phone.trim(),
company: company.trim(),
bio: bio.trim(),
});
setPhotoURL(photoURL.trim());
setPhone(phone.trim());
setCompany(company.trim());
setBio(bio.trim());
setSuccessMessage('Perfil actualizado correctamente');
return true;
} catch (err) {
setError(getErrorMessage(err, 'No se pudo actualizar el perfil'));
return false;
} finally {
setIsSubmitting(false);
}
};
return {
displayName,
photoURL,
phone,
company,
bio,
setDisplayName,
setPhotoURL,
setPhone,
setCompany,
setBio,
isSubmitting,
error,
successMessage,
maxBioLength: MAX_BIO_LENGTH,
submit,
};
}
|