-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathHomeScreen.js
More file actions
326 lines (298 loc) · 9.13 KB
/
HomeScreen.js
File metadata and controls
326 lines (298 loc) · 9.13 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { useContext, useEffect, useState } from "react";
import { Alert, FlatList, RefreshControl, StyleSheet, View } from "react-native";
import {
ActivityIndicator,
Appbar,
Avatar,
Modal,
Portal,
Text,
TextInput,
useTheme,
} from "react-native-paper";
import HapticButton from '../components/ui/HapticButton';
import HapticCard from '../components/ui/HapticCard';
import { HapticAppbarAction } from '../components/ui/HapticAppbar';
import GroupListSkeleton from '../components/skeletons/GroupListSkeleton';
import * as Haptics from "expo-haptics";
import { createGroup, getGroups, getOptimizedSettlements } from "../api/groups";
import { AuthContext } from "../context/AuthContext";
import { formatCurrency, getCurrencySymbol } from "../utils/currency";
const HomeScreen = ({ navigation }) => {
const { token, logout, user } = useContext(AuthContext);
const theme = useTheme();
const [groups, setGroups] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [isRefreshing, setIsRefreshing] = useState(false);
const [groupSettlements, setGroupSettlements] = useState({}); // Track settlement status for each group
// State for the Create Group modal
const [modalVisible, setModalVisible] = useState(false);
const [newGroupName, setNewGroupName] = useState("");
const [isCreatingGroup, setIsCreatingGroup] = useState(false);
const showModal = () => setModalVisible(true);
const hideModal = () => setModalVisible(false);
// Calculate settlement status for a group
const calculateSettlementStatus = async (groupId, userId) => {
try {
const response = await getOptimizedSettlements(groupId);
const settlements = response.data.optimizedSettlements || [];
// Check if user has any pending settlements
const userOwes = settlements.filter((s) => s.fromUserId === userId);
const userIsOwed = settlements.filter((s) => s.toUserId === userId);
const totalOwed = userOwes.reduce((sum, s) => sum + (s.amount || 0), 0);
const totalToReceive = userIsOwed.reduce(
(sum, s) => sum + (s.amount || 0),
0
);
return {
isSettled: totalOwed === 0 && totalToReceive === 0,
owesAmount: totalOwed,
owedAmount: totalToReceive,
netBalance: totalToReceive - totalOwed,
};
} catch (error) {
console.error(
"Failed to fetch settlement status for group:",
groupId,
error
);
return {
isSettled: true,
owesAmount: 0,
owedAmount: 0,
netBalance: 0,
};
}
};
const fetchGroups = async (showLoading = true) => {
try {
if (showLoading) setIsLoading(true);
const response = await getGroups();
const groupsList = response.data.groups;
setGroups(groupsList);
// Fetch settlement status for each group
if (user?._id) {
const settlementPromises = groupsList.map(async (group) => {
const status = await calculateSettlementStatus(group._id, user._id);
return { groupId: group._id, status };
});
const settlementResults = await Promise.all(settlementPromises);
const settlementMap = {};
settlementResults.forEach(({ groupId, status }) => {
settlementMap[groupId] = status;
});
setGroupSettlements(settlementMap);
}
} catch (error) {
console.error("Failed to fetch groups:", error);
Alert.alert("Error", "Failed to fetch groups.");
} finally {
if (showLoading) setIsLoading(false);
}
};
const onRefresh = async () => {
setIsRefreshing(true);
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
await fetchGroups(false);
setIsRefreshing(false);
};
useEffect(() => {
if (token) {
fetchGroups();
}
}, [token]);
const handleCreateGroup = async () => {
if (!newGroupName) {
Alert.alert("Error", "Please enter a group name.");
return;
}
setIsCreatingGroup(true);
try {
await createGroup(newGroupName);
hideModal();
setNewGroupName("");
await fetchGroups(); // Refresh the groups list
} catch (error) {
console.error("Failed to create group:", error);
Alert.alert("Error", "Failed to create group.");
} finally {
setIsCreatingGroup(false);
}
};
const currencySymbol = getCurrencySymbol();
const renderGroup = ({ item }) => {
const settlementStatus = groupSettlements[item._id];
// Generate settlement status text
const getSettlementStatusText = () => {
if (!settlementStatus) {
return "Calculating balances...";
}
if (settlementStatus.isSettled) {
return "✓ You are settled up.";
}
if (settlementStatus.netBalance > 0) {
return `You are owed ${formatCurrency(settlementStatus.netBalance)}.`;
} else if (settlementStatus.netBalance < 0) {
return `You owe ${formatCurrency(
Math.abs(settlementStatus.netBalance)
)}.`;
}
return "You are settled up.";
};
// Get text color based on settlement status
const getStatusColor = () => {
if (!settlementStatus || settlementStatus.isSettled) {
return "#4CAF50"; // Green for settled
}
if (settlementStatus.netBalance > 0) {
return "#4CAF50"; // Green for being owed money
} else if (settlementStatus.netBalance < 0) {
return "#F44336"; // Red for owing money
}
return "#4CAF50"; // Default green
};
const isImage =
item.imageUrl && /^(https?:|data:image)/.test(item.imageUrl);
const groupIcon = item.imageUrl || item.name?.charAt(0) || "?";
return (
<HapticCard
style={styles.card}
onPress={() =>
navigation.navigate("GroupDetails", {
groupId: item._id,
groupName: item.name,
groupIcon,
})
}
accessibilityRole="button"
accessibilityLabel={`Group ${item.name}. ${getSettlementStatusText()}`}
accessibilityHint="Double tap to view group details"
>
<HapticCard.Title
title={item.name}
left={(props) =>
isImage ? (
<Avatar.Image {...props} source={{ uri: item.imageUrl }} />
) : (
<Avatar.Text {...props} label={groupIcon} />
)
}
/>
<HapticCard.Content>
<Text style={[styles.settlementStatus, { color: getStatusColor() }]}>
{getSettlementStatusText()}
</Text>
</HapticCard.Content>
</HapticCard>
);
};
return (
<View style={styles.container}>
<Portal>
<Modal
visible={modalVisible}
onDismiss={hideModal}
contentContainerStyle={styles.modalContainer}
>
<Text style={styles.modalTitle}>Create a New Group</Text>
<TextInput
label="Group Name"
value={newGroupName}
onChangeText={setNewGroupName}
style={styles.input}
accessibilityLabel="New group name"
/>
<HapticButton
mode="contained"
onPress={handleCreateGroup}
loading={isCreatingGroup}
disabled={isCreatingGroup}
accessibilityLabel="Create Group"
accessibilityRole="button"
>
Create
</HapticButton>
</Modal>
</Portal>
<Appbar.Header>
<Appbar.Content title="Your Groups" />
<HapticAppbarAction
icon="plus"
onPress={showModal}
accessibilityLabel="Create new group"
accessibilityRole="button"
/>
<HapticAppbarAction
icon="account-plus"
onPress={() =>
navigation.navigate("JoinGroup", { onGroupJoined: fetchGroups })
}
accessibilityLabel="Join a group"
accessibilityRole="button"
/>
</Appbar.Header>
{isLoading ? (
<GroupListSkeleton />
) : (
<FlatList
data={groups}
renderItem={renderGroup}
keyExtractor={(item) => item._id}
contentContainerStyle={styles.list}
ListEmptyComponent={
<Text style={styles.emptyText}>
No groups found. Create or join one!
</Text>
}
refreshControl={
<RefreshControl
refreshing={isRefreshing}
onRefresh={onRefresh}
colors={[theme.colors.primary]}
tintColor={theme.colors.primary}
/>
}
/>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
loaderContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
list: {
padding: 16,
},
card: {
marginBottom: 16,
},
settlementStatus: {
fontWeight: "500",
marginTop: 4,
},
emptyText: {
textAlign: "center",
marginTop: 20,
},
modalContainer: {
backgroundColor: "white",
padding: 20,
margin: 20,
borderRadius: 8,
},
modalTitle: {
fontSize: 20,
marginBottom: 20,
textAlign: "center",
},
input: {
marginBottom: 20,
},
});
export default HomeScreen;