Fetch user profile - abhiram-shaji/Langroove GitHub Wiki
-
Component Structure:
FeedScreen
: The main screen that displays a list of topics. It usesuseFeed
to get topics anduseHeader
to fetch and display user information (like the logged-in user's name). When a topic is clicked, it navigates to a profile screen with the topic owner’s ID as a parameter.TopicCard
: A card component for each topic that shows the owner’s name, avatar, and description. It usesuseUserInfo
to fetch the profile of the topic owner by their ID.
-
Fetching User Info with
useHeader
anduseUserInfo
:useHeader
: This hook is likely used to fetch the logged-in user's information and status (isUserLoading
,userInfoLoading
) so it can display "Welcome" or the user's name.useUserInfo
: This hook fetches the specific user information (like avatar) for each topic owner. When aTopicCard
renders,useUserInfo
retrieves the owner's profile based on theirownerId
. If loading, it displays a default avatar; otherwise, it displays the user’s actual avatar.
-
Caching and Fetching Topics:
useFeed
Hook: Manages topic fetching from Firestore. It also handles offline support with caching:- Load Cached Topics:
useFeed
first tries to load cached topics fromAsyncStorage
for faster initial load. - Fetch from Firestore: It sets up a real-time listener on the 'topics' collection in Firestore using
onSnapshot
. When topics are fetched, it stores them inAsyncStorage
to update the cache and provides real-time updates when topics change.
- Load Cached Topics:
-
Navigating to Profile:
- When a user clicks on a
TopicCard
, theFeedScreen
navigates to the profile screen (Profile
) withownerId
as a parameter, enabling theProfile
screen to fetch that specific user’s details.
- When a user clicks on a
-
Unsubscribe Logic:
- To manage the real-time listener efficiently,
useFeed
stores theunsubscribe
function fromonSnapshot
. This function is called when the component unmounts to avoid memory leaks and unnecessary data fetching.
- To manage the real-time listener efficiently,
-
Activity Indicators:
- Top Bar Loading: Displays an
ActivityIndicator
ifisUserLoading
oruserInfoLoading
are true, showing a loading spinner until user data is fetched. - Default Avatar: In
TopicCard
, ifuseUserInfo
is loading, a default avatar is displayed until the actual avatar is fetched.
- Top Bar Loading: Displays an
In summary, this approach efficiently handles profile fetching with real-time Firestore updates and caching for offline support. It also cleans up listeners on unmount, ensuring optimal performance.