diff options
author | Ashm Walia <40498934+ashmgarv@users.noreply.github.com> | 2020-12-04 08:50:24 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-04 11:50:24 -0500 |
commit | 0fd892ad288f2e1eaaa4fdf5e1fd6f15dbd45860 (patch) | |
tree | d7d53d94c6c4026ac9b325508ebce4706d412ac4 /src/components/profile/ProfilePreview.tsx | |
parent | f620102190629e0b6f180d3ce056d850b1db5aaa (diff) |
[TMA - 398 AND TMA-430] Replace Providers with Redux Store (#125)
* First
* WIP
* Thunk
* Some more comments
* sc
* recent searches and follounfollow
* Edit profile dummy
* Block / unblock and some cleanup
* Replace auth provider
* Sc
* Delete AP after rebase
* Discover users
* Cleanup
* More cleanup
* Replace profile provider
* Fixed build failure
* Fixed a bug reported
* Prevent app crash when backend server is down
Diffstat (limited to 'src/components/profile/ProfilePreview.tsx')
-rw-r--r-- | src/components/profile/ProfilePreview.tsx | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/src/components/profile/ProfilePreview.tsx b/src/components/profile/ProfilePreview.tsx index af116dd3..5567fa5a 100644 --- a/src/components/profile/ProfilePreview.tsx +++ b/src/components/profile/ProfilePreview.tsx @@ -1,5 +1,5 @@ import React, {useEffect, useState, useContext} from 'react'; -import {ProfilePreviewType} from '../../types'; +import {ProfilePreviewType, ScreenType} from '../../types'; import { View, Text, @@ -14,8 +14,11 @@ import RNFetchBlob from 'rn-fetch-blob'; import AsyncStorage from '@react-native-community/async-storage'; import {AVATAR_PHOTO_ENDPOINT} from '../../constants'; import {UserType, PreviewType} from '../../types'; -import {AuthContext} from '../../routes/'; import {isUserBlocked} from '../../services'; +import {useSelector, useDispatch, useStore} from 'react-redux'; +import {RootState} from '../../store/rootreducer'; +import {loadUserX, logout} from '../../store/actions'; +import {userXInStore} from '../../utils'; const NO_USER: UserType = { userId: '', username: '', @@ -27,22 +30,24 @@ const NO_USER: UserType = { * If isComment is true then it means that we are not displaying this tile as a part of search results. * And hence we do not cache the search results. * On the other hand, if isComment is false, then we should update the search cache. (This cache needs to be revamped to clear outdated results.) - * In either case, we update the userBeingVisited in our AuthContext (Which can be used to make api calls later on to fetch user specific data). * Finally, We navigate to Profile. */ interface ProfilePreviewProps extends ViewProps { profilePreview: ProfilePreviewType; previewType: PreviewType; + screenType: ScreenType; } const ProfilePreview: React.FC<ProfilePreviewProps> = ({ profilePreview: {username, first_name, last_name, id}, previewType, + screenType, }) => { const navigation = useNavigation(); - const {user: loggedInUser, logout} = React.useContext(AuthContext); + const {user: loggedInUser} = useSelector((state: RootState) => state.user); const [avatarURI, setAvatarURI] = useState<string | null>(null); const [user, setUser] = useState<UserType>(NO_USER); + const dispatch = useDispatch(); useEffect(() => { let mounted = true; const loadAvatar = async () => { @@ -87,12 +92,14 @@ const ProfilePreview: React.FC<ProfilePreviewProps> = ({ const checkIfUserIsBlocked = async (userId: string) => { const token = await AsyncStorage.getItem('token'); if (!token) { - logout(); + dispatch(logout()); return false; } return await isUserBlocked(userId, loggedInUser.userId, token); }; + const state: RootState = useStore().getState(); + const addToRecentlyStoredAndNavigateToProfile = async () => { let user: ProfilePreviewType = { id, @@ -145,12 +152,19 @@ const ProfilePreview: React.FC<ProfilePreviewProps> = ({ } /** - * Navigate to profile of the user selected + * Dispatch an event to Fetch the user details + * If the user is already present in store, do not fetch again + * Finally, Navigate to profile of the user selected */ + if (!userXInStore(state, screenType, user.id)) { + dispatch( + loadUserX({userId: user.id, username: user.username}, screenType), + ); + } navigation.push('Profile', { - isProfileView: true, username: user.username, - userId: user.id, + userXId: user.id, + screenType: screenType, }); } catch (e) { console.log(e); |