Fetch user profile - abhiram-shaji/Langroove GitHub Wiki
-
Component Structure:
FeedScreen: The main screen that displays a list of topics. It usesuseFeedto get topics anduseHeaderto 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 usesuseUserInfoto fetch the profile of the topic owner by their ID.
-
Fetching User Info with
useHeaderanduseUserInfo: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 aTopicCardrenders,useUserInforetrieves 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:
useFeedHook: Manages topic fetching from Firestore. It also handles offline support with caching:- Load Cached Topics:
useFeedfirst tries to load cached topics fromAsyncStoragefor 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 inAsyncStorageto update the cache and provides real-time updates when topics change.
- Load Cached Topics:
-
Navigating to Profile:
- When a user clicks on a
TopicCard, theFeedScreennavigates to the profile screen (Profile) withownerIdas a parameter, enabling theProfilescreen to fetch that specific user’s details.
- When a user clicks on a
-
Unsubscribe Logic:
- To manage the real-time listener efficiently,
useFeedstores theunsubscribefunction 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
ActivityIndicatorifisUserLoadingoruserInfoLoadingare true, showing a loading spinner until user data is fetched. - Default Avatar: In
TopicCard, ifuseUserInfois 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.