summaryrefslogtreecommitdiffstats
path: root/src/features/profile/hooks/useProfile.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/profile/hooks/useProfile.ts')
-rw-r--r--src/features/profile/hooks/useProfile.ts45
1 files changed, 45 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,
+ };
+}