Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added reload to search page #116

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 49 additions & 36 deletions project/app/(tabs)/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
FlatList,
Image,
TouchableOpacity,
RefreshControl,
} from "react-native";
import Ionicons from "@expo/vector-icons/Ionicons";
import {
Expand Down Expand Up @@ -35,46 +36,50 @@ const Search = () => {
const [searchQuery, setSearchQuery] = useState("");
const [users, setUsers] = useState<User[]>([]);
const [filteredData, setFilteredData] = useState<User[]>([]);

const [refreshing, setRefreshing] = useState(false);
// Fetch all users from the database using databaseService
const fetchUsers = async () => {
try {
const userIds = await getAllUsernames(); // Includes firstName and lastName
console.log(`Fetched user IDs count: ${userIds.length}`); // Debugging

const currentUserId = await getUserId();
const allFollowingRequests = await getAllFollowingRequests(currentUserId)
const allFollowing = await getAllFollowing(currentUserId)
const userProfiles: User[] = [];

const profilePromises = userIds.map(async (userBasic) => {
const profile = await getUserProfile(userBasic.userId);
if (profile) {
userProfiles.push({
userId: profile.userId || userBasic.userId,
username: profile.username || "Unknown",
firstName: userBasic.firstName || "",
lastName: userBasic.lastName || "",
profilePic: profile.profilePic || "",
isFollowingRequest: allFollowingRequests.some(request => request.id === userBasic.userId),
isFollowing: allFollowing.some(following => following.id === userBasic.userId),
} as User);
}
});

await Promise.all(profilePromises);

console.log(`Fetched user profiles count: ${userProfiles.length}`); // Debugging
setUsers(userProfiles);
setFilteredData(userProfiles.slice(0, 10));
} catch (error) {
console.error("Error fetching users:", error);
}
};
useEffect(() => {
const fetchUsers = async () => {
try {
const userIds = await getAllUsernames(); // Includes firstName and lastName
console.log(`Fetched user IDs count: ${userIds.length}`); // Debugging

const currentUserId = await getUserId();
const allFollowingRequests = await getAllFollowingRequests(currentUserId)
const allFollowing = await getAllFollowing(currentUserId)
const userProfiles: User[] = [];

const profilePromises = userIds.map(async (userBasic) => {
const profile = await getUserProfile(userBasic.userId);
if (profile) {
userProfiles.push({
userId: profile.userId || userBasic.userId,
username: profile.username || "Unknown",
firstName: userBasic.firstName || "",
lastName: userBasic.lastName || "",
profilePic: profile.profilePic || "",
isFollowingRequest: allFollowingRequests.some(request => request.id === userBasic.userId),
isFollowing: allFollowing.some(following => following.id === userBasic.userId),
} as User);
}
});

await Promise.all(profilePromises);

console.log(`Fetched user profiles count: ${userProfiles.length}`); // Debugging
setUsers(userProfiles);
setFilteredData(userProfiles.slice(0, 10));
} catch (error) {
console.error("Error fetching users:", error);
}
};

fetchUsers();
}, []);
const handleRefresh = async () => {
setRefreshing(true); // Start the refreshing animation
await fetchUsers()
setRefreshing(false); // Stop the refreshing animation
};

const handleAddFriend = async (userId: string) => {
const currentUserId = await getUserId()
Expand Down Expand Up @@ -134,6 +139,14 @@ const renderItem = ({ item }: { item: User }) => (
data={filteredData}
renderItem={renderItem}
keyExtractor={(item, index) => uuid()}
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={handleRefresh}
colors={["#ffd33d"]} // Customize spinner color (Android)
tintColor="#ffd33d" // Customize spinner color (iOS)
/>
}
/>
</View>
);
Expand Down