11import { useState , useEffect } from 'react' ;
22import { Alert } from 'react-native' ;
3- import { useAuth } from '@/context/AuthContext' ;
43import { profileService } from '@/services/profileService' ;
54import { ProfileData } from '@/types/api/profile' ;
65import { Event } from '@/types/api/event' ;
76import { Volunteer } from '@/types/api/volunteer' ;
87import { useCurrentVolunteer } from '@/hooks/useCurrentVolunteer' ;
9- import { useUpcomingEvents } from '@/hooks/useUpcomingEvents' ;
108
119// TODO: abstract to backend asw
1210function calculateTotalHours ( completedEvents : Event [ ] ) : number {
@@ -20,49 +18,44 @@ function calculateTotalHours(completedEvents: Event[]): number {
2018}
2119
2220export function useProfile ( ) {
23- const { user } = useAuth ( ) ;
2421 const {
2522 data : qVolunteer ,
2623 isLoading : volunteerLoading ,
2724 refetch : refetchVolunteer ,
2825 } = useCurrentVolunteer ( ) ;
29- const { data : qUpcomingEvents , refetch : refetchUpcomingEvents } =
30- useUpcomingEvents ( user ?. entityId ) ;
3126 const [ loading , setLoading ] = useState ( true ) ;
3227 const [ refreshing , setRefreshing ] = useState ( false ) ;
3328 const [ volunteer , setVolunteer ] = useState < Volunteer | null > ( null ) ;
3429 const [ upcomingEvents , setUpcomingEvents ] = useState < Event [ ] > ( [ ] ) ;
3530 const [ profileData , setProfileData ] = useState < ProfileData | null > ( null ) ;
3631
3732 const loadProfileData = async ( isRefresh = false ) => {
38- if ( ! user ?. entityId ) {
39- setLoading ( false ) ;
40- return ;
41- }
4233 try {
4334 if ( isRefresh ) {
4435 setRefreshing ( true ) ;
4536 } else {
4637 setLoading ( true ) ;
4738 }
48- // Ensure we have latest volunteer and upcoming events (React Query)
49- const [ vResult , uResult , pastEventsData ] = await Promise . all ( [
50- refetchVolunteer ( ) ,
51- refetchUpcomingEvents ( ) ,
52- profileService . getPastEvents ( user . entityId ) ,
53- ] ) ;
39+ // Ensure we have latest volunteer
40+ const vResult = await refetchVolunteer ( ) ;
5441
55- const v = vResult . data ?? qVolunteer ?? null ;
42+ const v : Volunteer | null = vResult . data ?? qVolunteer ?? null ;
5643 setVolunteer ( v ) ;
57- const upcoming = uResult . data ?? qUpcomingEvents ?? [ ] ;
58- setUpcomingEvents ( upcoming ) ;
5944
6045 if ( ! v ) {
46+ setUpcomingEvents ( [ ] ) ;
6147 setProfileData ( null ) ;
6248 return ;
6349 }
6450
65- const levelProgress = await profileService . getLevelProgress ( ) ;
51+ // Fetch upcoming and past events using the resolved volunteer id
52+ const [ upcoming , pastEventsData , levelProgress ] = await Promise . all ( [
53+ profileService . getUpcomingEvents ( v . id ) ,
54+ profileService . getPastEvents ( v . id ) ,
55+ profileService . getLevelProgress ( ) ,
56+ ] ) ;
57+
58+ setUpcomingEvents ( upcoming ) ;
6659 const totalHours = calculateTotalHours ( pastEventsData ) ;
6760
6861 const stats = {
@@ -92,22 +85,16 @@ export function useProfile() {
9285
9386 useEffect ( ( ) => {
9487 // When user or volunteer changes, rebuild profile
95- if ( ! user ?. entityId ) {
96- setLoading ( false ) ;
97- return ;
98- }
9988 // If volunteer query is still loading, wait
10089 if ( volunteerLoading ) return ;
10190 // Build with current data (events will be fetched)
10291 loadProfileData ( false ) ;
10392 // eslint-disable-next-line react-hooks/exhaustive-deps
10493 } , [
105- user ?. entityId ,
10694 qVolunteer ?. id ,
10795 qVolunteer ?. firstName ,
10896 qVolunteer ?. lastName ,
10997 qVolunteer ?. experience ,
110- qUpcomingEvents ?. length ,
11198 ] ) ;
11299
113100 return {
0 commit comments