@@ -5,21 +5,21 @@ import * as Haptics from 'expo-haptics';
55import { LinearGradient } from 'expo-linear-gradient' ;
66import { useContext , useEffect , useRef , useState } from "react" ;
77import {
8- Alert ,
9- Animated ,
10- Dimensions ,
11- FlatList ,
12- RefreshControl ,
13- ScrollView ,
14- StyleSheet ,
15- TouchableOpacity ,
16- View ,
8+ Alert ,
9+ Animated ,
10+ Dimensions ,
11+ FlatList ,
12+ RefreshControl ,
13+ ScrollView ,
14+ StyleSheet ,
15+ TouchableOpacity ,
16+ View ,
1717} from "react-native" ;
1818import {
19- ActivityIndicator ,
20- Modal ,
21- Portal ,
22- Text ,
19+ ActivityIndicator ,
20+ Modal ,
21+ Portal ,
22+ Text ,
2323} from "react-native-paper" ;
2424import { createGroup , getGroups , getOptimizedSettlements } from "../api/groups" ;
2525import { AuthContext } from "../context/AuthContext" ;
@@ -65,8 +65,13 @@ const HomeScreen = ({ navigation }) => {
6565 } ) ,
6666 ] ) . start ( ) ;
6767
68- fetchGroups ( ) ;
69- } , [ ] ) ;
68+ // Only fetch groups if user is authenticated
69+ if ( user && token ) {
70+ fetchGroups ( ) ;
71+ } else {
72+ setIsLoading ( false ) ; // Stop loading if not authenticated
73+ }
74+ } , [ user , token ] ) ; // Add dependencies
7075
7176 // Calculate settlement status for a group
7277 const calculateSettlementStatus = async ( groupId , userId ) => {
@@ -104,22 +109,38 @@ const HomeScreen = ({ navigation }) => {
104109
105110 const fetchGroups = async ( ) => {
106111 try {
107- const response = await getGroups ( token ) ;
108- const groupsData = response . data ;
112+ const response = await getGroups ( ) ; // Remove token parameter
113+ console . log ( 'Groups API Response:' , response ) ; // Debug log
114+
115+ // Handle different response structures
116+ let groupsData = response . data ;
117+ if ( ! groupsData ) {
118+ groupsData = response ; // Sometimes the response itself is the data
119+ }
120+ if ( ! Array . isArray ( groupsData ) ) {
121+ console . warn ( 'Groups data is not an array:' , groupsData ) ;
122+ groupsData = [ ] ; // Fallback to empty array
123+ }
124+
109125 setGroups ( groupsData ) ;
110126
111127 // Calculate settlement status for each group
112- const settlementPromises = groupsData . map ( async ( group ) => {
113- const status = await calculateSettlementStatus ( group . _id , user . _id ) ;
114- return { groupId : group . _id , status } ;
115- } ) ;
116-
117- const settlementsData = await Promise . all ( settlementPromises ) ;
118- const settlementsMap = { } ;
119- settlementsData . forEach ( ( { groupId, status } ) => {
120- settlementsMap [ groupId ] = status ;
121- } ) ;
122- setGroupSettlements ( settlementsMap ) ;
128+ if ( groupsData . length > 0 ) {
129+ const settlementPromises = groupsData . map ( async ( group ) => {
130+ const status = await calculateSettlementStatus ( group . _id , user . _id ) ;
131+ return { groupId : group . _id , status } ;
132+ } ) ;
133+
134+ const settlementsData = await Promise . all ( settlementPromises ) ;
135+ const settlementsMap = { } ;
136+ settlementsData . forEach ( ( { groupId, status } ) => {
137+ settlementsMap [ groupId ] = status ;
138+ } ) ;
139+ setGroupSettlements ( settlementsMap ) ;
140+ } else {
141+ // No groups, clear settlements
142+ setGroupSettlements ( { } ) ;
143+ }
123144
124145 // Animate balance numbers
125146 Animated . timing ( balanceAnim , {
@@ -161,7 +182,7 @@ const HomeScreen = ({ navigation }) => {
161182
162183 setIsCreatingGroup ( true ) ;
163184 try {
164- await createGroup ( newGroupName . trim ( ) , token ) ;
185+ await createGroup ( newGroupName . trim ( ) ) ; // Remove token parameter
165186 await Haptics . notificationAsync ( Haptics . NotificationFeedbackType . Success ) ;
166187 hideModal ( ) ;
167188 fetchGroups ( ) ;
0 commit comments