From 0fd892ad288f2e1eaaa4fdf5e1fd6f15dbd45860 Mon Sep 17 00:00:00 2001 From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com> Date: Fri, 4 Dec 2020 08:50:24 -0800 Subject: [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 --- src/screens/onboarding/Checkpoint.tsx | 1 - src/screens/onboarding/Login.tsx | 15 ++-- src/screens/onboarding/SocialMedia.tsx | 14 ++-- src/screens/profile/CaptionScreen.tsx | 26 ++++--- src/screens/profile/EditProfile.tsx | 13 ++-- src/screens/profile/FollowersListScreen.tsx | 21 +++++- src/screens/profile/IndividualMoment.tsx | 25 +++++-- src/screens/profile/MomentCommentsScreen.tsx | 15 ++-- src/screens/profile/ProfileScreen.tsx | 74 ++++++++++---------- src/screens/profile/SocialMediaTaggs.tsx | 100 +++++++++++++++++---------- src/screens/search/SearchScreen.tsx | 37 ++++++++-- 11 files changed, 220 insertions(+), 121 deletions(-) (limited to 'src/screens') diff --git a/src/screens/onboarding/Checkpoint.tsx b/src/screens/onboarding/Checkpoint.tsx index 0be1e831..83a8a2bc 100644 --- a/src/screens/onboarding/Checkpoint.tsx +++ b/src/screens/onboarding/Checkpoint.tsx @@ -11,7 +11,6 @@ import { } from 'react-native'; import {OnboardingStackParams} from '../../routes'; -import {AuthContext} from '../../routes/authentication'; import {RegistrationWizard, Background} from '../../components'; type CheckpointRouteProp = RouteProp; diff --git a/src/screens/onboarding/Login.tsx b/src/screens/onboarding/Login.tsx index 8f19ec3d..2ddae403 100644 --- a/src/screens/onboarding/Login.tsx +++ b/src/screens/onboarding/Login.tsx @@ -14,11 +14,12 @@ import { } from 'react-native'; import {OnboardingStackParams} from '../../routes/onboarding'; -import {AuthContext} from '../../routes/authentication'; import {Background, TaggInput, SubmitButton} from '../../components'; import {usernameRegex, LOGIN_ENDPOINT} from '../../constants'; import AsyncStorage from '@react-native-community/async-storage'; import {UserType} from '../../types'; +import {useDispatch} from 'react-redux'; +import {userLogin} from '../../utils'; type VerificationScreenRouteProp = RouteProp; type VerificationScreenNavigationProp = StackNavigationProp< @@ -51,9 +52,15 @@ const Login: React.FC = ({navigation}: LoginProps) => { attemptedSubmit: false, token: '', }); - // determines if user is logged in - const {login} = React.useContext(AuthContext); const [user, setUser] = useState(NO_USER); + + /** + * Redux Store stuff + * Get the dispatch reference + */ + + const dispatch = useDispatch(); + /** * Updates the state of username. Also verifies the input of the username field by ensuring proper length and appropriate characters. */ @@ -140,7 +147,7 @@ const Login: React.FC = ({navigation}: LoginProps) => { await AsyncStorage.setItem('token', data.token); await AsyncStorage.setItem('userId', data.UserID); await AsyncStorage.setItem('username', username); - login(data.UserID, username); + userLogin(dispatch, {userId: data.UserID, username}); } catch (err) { setUser(NO_USER); console.log(data); diff --git a/src/screens/onboarding/SocialMedia.tsx b/src/screens/onboarding/SocialMedia.tsx index 57270e4e..ee2bed10 100644 --- a/src/screens/onboarding/SocialMedia.tsx +++ b/src/screens/onboarding/SocialMedia.tsx @@ -10,6 +10,7 @@ import { TouchableOpacity, View, } from 'react-native'; +import {useDispatch} from 'react-redux'; import {LinkerType} from 'src/types'; import { Background, @@ -17,7 +18,8 @@ import { RegistrationWizard, } from '../../components'; import {SOCIAL_LIST} from '../../constants/'; -import {AuthContext, OnboardingStackParams} from '../../routes'; +import {OnboardingStackParams} from '../../routes'; +import {userLogin} from '../../utils'; /** * Social Media Screen for displaying social media linkers @@ -33,12 +35,6 @@ const SocialMedia: React.FC = ({route}) => { const {userId, username} = route.params; const linkers: Array = []; - /** - * login: determines if user successully created an account to - * navigate to home and display main tab navigation bar - */ - const {login} = React.useContext(AuthContext); - // let numSocials: Number = state.showMore ? 9 : 3; for (let i = 0; i < SOCIAL_LIST.length; i++) { @@ -48,6 +44,8 @@ const SocialMedia: React.FC = ({route}) => { linkers.push(linker); } + const dispatch = useDispatch(); + /** * Just commenting this out, in case we need it in the future */ @@ -60,7 +58,7 @@ const SocialMedia: React.FC = ({route}) => { const handleLogin = () => { try { - login(userId, username); + userLogin(dispatch, {userId: userId, username: username}); } catch (error) { console.log(error); Alert.alert('There was a problem logging you in'); diff --git a/src/screens/profile/CaptionScreen.tsx b/src/screens/profile/CaptionScreen.tsx index 08dd8e5b..e9eed668 100644 --- a/src/screens/profile/CaptionScreen.tsx +++ b/src/screens/profile/CaptionScreen.tsx @@ -18,7 +18,9 @@ import {ProfileStackParams} from 'src/routes'; import {StackNavigationProp} from '@react-navigation/stack'; import {CaptionScreenHeader} from '../../components/'; import {MOMENTS_ENDPOINT} from '../../constants'; -import {AuthContext} from '../../routes/authentication'; +import {useDispatch, useSelector} from 'react-redux'; +import {loadUserMoments} from '../../store/actions'; +import {RootState} from '../../store/rootReducer'; /** * Upload Screen to allow users to upload posts to Tagg @@ -34,11 +36,11 @@ interface CaptionScreenProps { } const CaptionScreen: React.FC = ({route, navigation}) => { - const {title, image} = route.params; + const {title, image, screenType} = route.params; const { user: {userId}, - updateMoments, - } = React.useContext(AuthContext); + } = useSelector((state: RootState) => state.user); + const dispatch = useDispatch(); const [caption, setCaption] = React.useState(''); const handleCaptionUpdate = (caption: string) => { @@ -54,6 +56,14 @@ const CaptionScreen: React.FC = ({route, navigation}) => { return true; }; + const navigateToProfile = () => { + //Since the logged In User is navigating to own profile, useXId is not required + navigation.navigate('Profile', { + screenType, + userXId: undefined, + }); + }; + const handleShare = async () => { try { const request = new FormData(); @@ -86,8 +96,8 @@ const CaptionScreen: React.FC = ({route, navigation}) => { let data = await response.json(); if (statusCode === 200 && checkImageUploadStatus(data)) { Alert.alert('The picture was uploaded successfully!'); - updateMoments(true); - navigation.navigate('Profile', {isProfileView: false}); + dispatch(loadUserMoments(userId)); + navigateToProfile(); } else { Alert.alert('An error occured while uploading. Please try again!'); } @@ -107,9 +117,7 @@ const CaptionScreen: React.FC = ({route, navigation}) => {