diff options
| author | Alberto Duarte Delgado <alberto@albertoduarte.com> | 2026-03-30 18:12:23 +0100 |
|---|---|---|
| committer | Alberto Duarte Delgado <alberto@albertoduarte.com> | 2026-03-30 18:12:23 +0100 |
| commit | 7a196010214db6a1789760958eee5228374a7745 (patch) | |
| tree | e8d936b04bdf72acbe9c5910fee95add0bef7e94 /src/features | |
| parent | 174f1acd46c4ffbde88c04465e8fa6b6810ce113 (diff) | |
logica perfil de usuario
Diffstat (limited to 'src/features')
| -rw-r--r-- | src/features/profile/hooks/useProfile.ts | 45 | ||||
| -rw-r--r-- | src/features/profile/hooks/useProfile.ts~ | 0 | ||||
| -rw-r--r-- | src/features/profile/hooks/useUpdateProfile.ts | 52 | ||||
| -rw-r--r-- | src/features/profile/hooks/useUpdateProfile.ts~ | 0 | ||||
| -rw-r--r-- | src/features/profile/services/profileService.ts | 45 | ||||
| -rw-r--r-- | src/features/profile/services/profileService.ts~ | 0 | ||||
| -rw-r--r-- | src/features/profile/types.ts | 8 | ||||
| -rw-r--r-- | src/features/profile/types.ts~ | 0 |
8 files changed, 150 insertions, 0 deletions
diff --git a/src/features/profile/hooks/useProfile.ts b/src/features/profile/hooks/useProfile.ts new file mode 100644 index 0000000..82068f4 --- /dev/null +++ b/src/features/profile/hooks/useProfile.ts @@ -0,0 +1,45 @@ +// src/features/profile/hooks/useProfile.ts + +import { useEffect, useState } from 'react'; +import { useAuthSession } from '@/providers/AuthProvider'; +import { profileService } from '@/features/profile/services/profileService'; +import { UserProfile } from '@/features/profile/types'; + +export function useProfile() { + const { user } = useAuthSession(); + + const [profile, setProfile] = useState<UserProfile | null>(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState<string | null>(null); + + const loadProfile = async () => { + if (!user?.uid) { + setProfile(null); + setLoading(false); + return; + } + + try { + setError(null); + setLoading(true); + + const data = await profileService.getMyProfile(user.uid); + setProfile(data); + } catch (err: any) { + setError(err?.message ?? 'No se pudo cargar el perfil'); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + loadProfile(); + }, [user?.uid]); + + return { + profile, + loading, + error, + reload: loadProfile, + }; +} diff --git a/src/features/profile/hooks/useProfile.ts~ b/src/features/profile/hooks/useProfile.ts~ new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/features/profile/hooks/useProfile.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<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, + }; +} diff --git a/src/features/profile/hooks/useUpdateProfile.ts~ b/src/features/profile/hooks/useUpdateProfile.ts~ new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/features/profile/hooks/useUpdateProfile.ts~ diff --git a/src/features/profile/services/profileService.ts b/src/features/profile/services/profileService.ts new file mode 100644 index 0000000..566c3fa --- /dev/null +++ b/src/features/profile/services/profileService.ts @@ -0,0 +1,45 @@ +// src/features/profile/services/profileService.ts + +import { doc, getDoc, updateDoc } from 'firebase/firestore'; +import { updateProfile } from 'firebase/auth'; +import { db, auth } from '@/shared/lib/firebase/config'; +import { UserProfile } from '@/features/profile/types'; + +export const profileService = { + async getMyProfile(uid: string): Promise<UserProfile | null> { + const snapshot = await getDoc(doc(db, 'users', uid)); + + if (!snapshot.exists()) { + return null; + } + + const data = snapshot.data(); + + return { + uid, + email: data.email ?? '', + displayName: data.displayName ?? '', + photoURL: data.photoURL ?? '', + }; + }, + + async updateMyProfile(params: { + uid: string; + displayName: string; + photoURL?: string; + }): Promise<void> { + const { uid, displayName, photoURL } = params; + + await updateDoc(doc(db, 'users', uid), { + displayName, + ...(photoURL !== undefined ? { photoURL } : {}), + }); + + if (auth.currentUser) { + await updateProfile(auth.currentUser, { + displayName, + ...(photoURL !== undefined ? { photoURL } : {}), + }); + } + }, +}; diff --git a/src/features/profile/services/profileService.ts~ b/src/features/profile/services/profileService.ts~ new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/features/profile/services/profileService.ts~ diff --git a/src/features/profile/types.ts b/src/features/profile/types.ts new file mode 100644 index 0000000..1c6d219 --- /dev/null +++ b/src/features/profile/types.ts @@ -0,0 +1,8 @@ +// src/features/profile/types.ts + +export type UserProfile = { + uid: string; + email: string; + displayName: string; + photoURL?: string; +}; diff --git a/src/features/profile/types.ts~ b/src/features/profile/types.ts~ new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/features/profile/types.ts~ |