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
import React, { useState, useContext, useEffect } from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
FlatList,
ActivityIndicator,
Alert,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { Ionicons } from '@expo/vector-icons';
import { GroupContext } from '../context/GroupContext';
import { LocationContext } from '../context/LocationContext';
const FindGroupsScreen = ({ navigation, route }) => {
const { findGroups, joinExistingGroup } = useContext(GroupContext);
const { currentLocation, calculateDistance } = useContext(LocationContext);
const [destination, setDestination] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [isJoining, setIsJoining] = useState(false);
const [matchingGroups, setMatchingGroups] = useState([]);
const [joiningGroupId, setJoiningGroupId] = useState(null);
useEffect(() => {
if (route.params?.destination) {
setDestination(route.params.destination);
searchGroups(route.params.destination);
} else {
navigation.navigate('SetDestination', { returnTo: 'FindGroups' });
}
}, [route.params]);
const searchGroups = async (dest) => {
if (!dest) {
return;
}
setIsLoading(true);
try {
const criteria = {
destination: dest,
destinationGeohash: dest.geohash,
};
const result = await findGroups(criteria);
if (result.success && result.groups) {
// Sort groups by proximity to user's current location
const sortedGroups = [...result.groups].sort((a, b) => {
const distanceA = calculateDistance(currentLocation, a.startingPoint);
const distanceB = calculateDistance(currentLocation, b.startingPoint);
return distanceA - distanceB;
});
setMatchingGroups(sortedGroups);
} else {
setMatchingGroups([]);
}
} catch (error) {
console.error('Search groups error:', error);
Alert.alert('Error', 'Failed to search for groups');
} finally {
setIsLoading(false);
}
};
const handleJoinGroup = async (groupId) => {
setIsJoining(true);
setJoiningGroupId(groupId);
try {
const result = await joinExistingGroup(groupId);
if (result.success) {
navigation.navigate('GroupDetails', { groupId });
} else {
Alert.alert('Error', result.error || 'Failed to join group');
}
} catch (error) {
console.error('Join group error:', error);
Alert.alert('Error', 'An unexpected error occurred');
} finally {
setIsJoining(false);
setJoiningGroupId(null);
}
};
const handleCreateGroup = () => {
if (destination) {
navigation.navigate('CreateGroup', { destination });
} else {
navigation.navigate('SetDestination', { returnTo: 'CreateGroup' });
}
};
const renderGroupItem = ({ item }) => {
const departureTime = new Date(item.departureTime);
const formattedTime = departureTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const formattedDate = departureTime.toLocaleDateString();
return (
<TouchableOpacity
style={styles.groupItem}
onPress={() => handleJoinGroup(item.$id)}
disabled={isJoining}
>
<View style={styles.groupHeader}>
<Text style={styles.groupName}>{item.name}</Text>
<View style={[
styles.statusBadge,
item.status === 'open' ? styles.openStatus : styles.fullStatus
]}>
<Text style={styles.statusText}>{item.status === 'open' ? 'Open' : 'Full'}</Text>
</View>
</View>
<View style={styles.groupDetails}>
<View style={styles.detailItem}>
<Ionicons name="calendar" size={16} color="#7c4dff" />
<Text style={styles.detailText}>{formattedDate} at {formattedTime}</Text>
</View>
<View style={styles.detailItem}>
<Ionicons name="people" size={16} color="#7c4dff" />
<Text style={styles.detailText}>{item.memberCount} / {item.maxSize} members</Text>
</View>
<View style={styles.detailItem}>
<Ionicons name={item.vehicleType === 'car' ? 'car' : 'bicycle'} size={16} color="#7c4dff" />
<Text style={styles.detailText}>
{item.vehicleType === 'car' ? 'Car' : item.vehicleType === 'bike' ? 'Bike' : 'Any vehicle'}
</Text>
</View>
</View>
<View style={styles.joinButtonContainer}>
{isJoining && joiningGroupId === item.$id ? (
<ActivityIndicator size="small" color="#7c4dff" />
) : (
<TouchableOpacity
style={styles.joinButton}
onPress={() => handleJoinGroup(item.$id)}
disabled={item.status !== 'open' || isJoining}
>
<Text style={styles.joinButtonText}>Join</Text>
</TouchableOpacity>
)}
</View>
</TouchableOpacity>
);
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.header}>
<TouchableOpacity
style={styles.backButton}
onPress={() => navigation.goBack()}
>
<Ionicons name="arrow-back" size={24} color="#1b1d28" />
</TouchableOpacity>
<Text style={styles.headerTitle}>Find Groups</Text>
<View style={styles.No Output
Run the code to generate an output.