From b3c0cad0528615d21b79660fa3eb6ab3d30d7ac2 Mon Sep 17 00:00:00 2001 From: ChrisWoolson Date: Wed, 4 Dec 2024 14:49:29 -0800 Subject: [PATCH] Added reload to search page --- project/app/(tabs)/Search.tsx | 85 ++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/project/app/(tabs)/Search.tsx b/project/app/(tabs)/Search.tsx index a000d19..d62a56a 100644 --- a/project/app/(tabs)/Search.tsx +++ b/project/app/(tabs)/Search.tsx @@ -8,6 +8,7 @@ import { FlatList, Image, TouchableOpacity, + RefreshControl, } from "react-native"; import Ionicons from "@expo/vector-icons/Ionicons"; import { @@ -35,46 +36,50 @@ const Search = () => { const [searchQuery, setSearchQuery] = useState(""); const [users, setUsers] = useState([]); const [filteredData, setFilteredData] = useState([]); - + 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() @@ -134,6 +139,14 @@ const renderItem = ({ item }: { item: User }) => ( data={filteredData} renderItem={renderItem} keyExtractor={(item, index) => uuid()} + refreshControl={ + + } /> );