diff options
| author | Ivan Chen <ivan@tagg.id> | 2021-03-26 20:01:26 -0400 |
|---|---|---|
| committer | Ivan Chen <ivan@tagg.id> | 2021-03-26 20:01:26 -0400 |
| commit | ed26661062a2e47df2662254eaddfcfa1de62d04 (patch) | |
| tree | 78e1683c60ba65a1d71631838be500e1f54607f6 /src/screens | |
| parent | 5b975a354f0707c0671571387d5bda501fbe75ae (diff) | |
| parent | bef5728b24a71d1bf327a72e425346020a997037 (diff) | |
Merge branch 'master' into tma-722-cornell-fe
# Conflicts:
# src/types/types.ts
Diffstat (limited to 'src/screens')
| -rw-r--r-- | src/screens/onboarding/Login.tsx | 29 | ||||
| -rw-r--r-- | src/screens/onboarding/OnboardingStepThree.tsx | 100 | ||||
| -rw-r--r-- | src/screens/profile/EditProfile.tsx | 38 |
3 files changed, 76 insertions, 91 deletions
diff --git a/src/screens/onboarding/Login.tsx b/src/screens/onboarding/Login.tsx index 6d9d3a97..97f4fe87 100644 --- a/src/screens/onboarding/Login.tsx +++ b/src/screens/onboarding/Login.tsx @@ -27,7 +27,7 @@ import { import {OnboardingStackParams} from '../../routes/onboarding'; import {fcmService} from '../../services'; import {RootState} from '../../store/rootReducer'; -import {BackgroundGradientType} from '../../types'; +import {BackgroundGradientType, UniversityType} from '../../types'; import {normalize, userLogin} from '../../utils'; import UpdateRequired from './UpdateRequired'; @@ -156,18 +156,39 @@ const Login: React.FC<LoginProps> = ({navigation}: LoginProps) => { let statusCode = response.status; let data = await response.json(); + if (statusCode === 200) { + await AsyncStorage.setItem('token', data.token); + await AsyncStorage.setItem('userId', data.UserID); + await AsyncStorage.setItem('username', username); + } + if (statusCode === 200 && data.isOnboarded) { //Stores token received in the response into client's AsynStorage try { - await AsyncStorage.setItem('token', data.token); - await AsyncStorage.setItem('userId', data.UserID); - await AsyncStorage.setItem('username', username); userLogin(dispatch, {userId: data.UserID, username}); fcmService.sendFcmTokenToServer(); } catch (err) { Alert.alert(ERROR_INVALID_LOGIN); } + } else if ( + statusCode === 200 && + data.university === UniversityType.Empty + ) { + /** + * A user account was created during onboarding step 2 but user didn't + * finish step 3, thus does not have a universtiy. + * Redirecting user back to onboarding to finish the process + */ + navigation.navigate('OnboardingStepThree', { + userId: data.UserID, + username: username, + }); } else if (statusCode === 200 && !data.isOnboarded) { + /** + * A user account was created and finished the onboarding process but + * did not have an invitation code at the time so the user's account + * is not activated (isOnboarded) yet. + */ navigation.navigate('InvitationCodeVerification', { userId: data.UserID, username: username, diff --git a/src/screens/onboarding/OnboardingStepThree.tsx b/src/screens/onboarding/OnboardingStepThree.tsx index f22d720f..29028421 100644 --- a/src/screens/onboarding/OnboardingStepThree.tsx +++ b/src/screens/onboarding/OnboardingStepThree.tsx @@ -1,4 +1,3 @@ -import AsyncStorage from '@react-native-community/async-storage'; import {RouteProp} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; import moment from 'moment'; @@ -20,24 +19,19 @@ import { RegistrationWizard, TaggDropDown, TaggInput, + UniversitySelection, } from '../../components'; +import {CLASS_YEAR_LIST, genderRegex, TAGG_PURPLE} from '../../constants'; import { - CLASS_YEAR_LIST, - EDIT_PROFILE_ENDPOINT, - genderRegex, - TAGG_PURPLE, -} from '../../constants'; -import { - ERROR_DOUBLE_CHECK_CONNECTION, - ERROR_PROFILE_CREATION_SHORT, ERROR_SELECT_BIRTHDAY, ERROR_SELECT_CLASS_YEAR, ERROR_SELECT_GENDER, - ERROR_SOMETHING_WENT_WRONG_REFRESH, + ERROR_SELECT_UNIVERSITY, ERROR_UPLOAD_SMALL_PROFILE_PIC, } from '../../constants/strings'; import {OnboardingStackParams} from '../../routes/onboarding'; -import {BackgroundGradientType} from '../../types'; +import {patchEditProfile} from '../../services'; +import {BackgroundGradientType, UniversityType} from '../../types'; import {normalize, SCREEN_HEIGHT, SCREEN_WIDTH} from '../../utils'; type OnboardingStepThreeRouteProp = RouteProp< @@ -53,15 +47,25 @@ interface OnboardingStepThreeProps { navigation: OnboardingStepThreeNavigationProp; } +type FormType = { + smallPic: string; + university: UniversityType; + birthdate: string | undefined; + gender: string; + isValidGender: boolean; + classYear: number; + attemptedSubmit: boolean; +}; + const OnboardingStepThree: React.FC<OnboardingStepThreeProps> = ({ route, navigation, }) => { const {userId, username} = route.params; - let emptyDate: string | undefined; - const [form, setForm] = React.useState({ + const [form, setForm] = React.useState<FormType>({ smallPic: '', - birthdate: emptyDate, + university: UniversityType.Empty, + birthdate: undefined, gender: '', isValidGender: true, classYear: -1, @@ -164,7 +168,11 @@ const OnboardingStepThree: React.FC<OnboardingStepThreeProps> = ({ Alert.alert(ERROR_SELECT_CLASS_YEAR); return; } - if (form.birthdate === emptyDate) { + if (form.university === UniversityType.Empty) { + Alert.alert(ERROR_SELECT_UNIVERSITY); + return; + } + if (!form.birthdate) { Alert.alert(ERROR_SELECT_BIRTHDAY); return; } @@ -178,7 +186,6 @@ const OnboardingStepThree: React.FC<OnboardingStepThreeProps> = ({ attemptedSubmit: true, }); } - let invalidFields: boolean = false; const request = new FormData(); if (form.smallPic) { request.append('smallProfilePicture', { @@ -188,16 +195,13 @@ const OnboardingStepThree: React.FC<OnboardingStepThreeProps> = ({ }); } - if (form.birthdate) { - request.append('birthday', form.birthdate); - } if (customGender) { if (form.isValidGender) { request.append('gender', form.gender); } else { setForm({...form, attemptedSubmit: false}); setTimeout(() => setForm({...form, attemptedSubmit: true})); - invalidFields = true; + return; } } else { if (form.isValidGender) { @@ -205,47 +209,20 @@ const OnboardingStepThree: React.FC<OnboardingStepThreeProps> = ({ } } - if (form.classYear !== -1) { - request.append('university_class', form.classYear); - } - - if (invalidFields) { - return; - } + request.append('birthday', form.birthdate); + request.append('university_class', form.classYear); + request.append('university', form.university); - const endpoint = EDIT_PROFILE_ENDPOINT + `${userId}/`; - try { - const token = await AsyncStorage.getItem('token'); - let response = await fetch(endpoint, { - method: 'PATCH', - headers: { - 'Content-Type': 'multipart/form-data', - Authorization: 'Token ' + token, - }, - body: request, - }); - let statusCode = response.status; - let data = await response.json(); - if (statusCode === 200) { + patchEditProfile(request, userId) + .then((_) => navigation.navigate('InvitationCodeVerification', { userId: route.params.userId, username: username, - }); - } else if (statusCode === 400) { - Alert.alert( - 'Profile update failed. 😔', - data.error || 'Something went wrong! ðŸ˜', - ); - } else { - Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); - } - } catch (error) { - Alert.alert(ERROR_PROFILE_CREATION_SHORT, ERROR_DOUBLE_CHECK_CONNECTION); - return { - name: 'Profile creation error', - description: error, - }; - } + }), + ) + .catch((error) => { + Alert.alert(error); + }); }; return ( @@ -264,6 +241,15 @@ const OnboardingStepThree: React.FC<OnboardingStepThreeProps> = ({ /> </View> <View style={styles.contentContainer}> + <UniversitySelection + selected={form.university} + setSelected={(selected) => { + setForm({ + ...form, + university: selected, + }); + }} + /> <TaggDropDown onValueChange={(value: string) => handleClassYearUpdate(value)} items={classYearList} diff --git a/src/screens/profile/EditProfile.tsx b/src/screens/profile/EditProfile.tsx index 56bed11f..8afaeb6d 100644 --- a/src/screens/profile/EditProfile.tsx +++ b/src/screens/profile/EditProfile.tsx @@ -46,6 +46,7 @@ import { ERROR_UPLOAD_SMALL_PROFILE_PIC, } from '../../constants/strings'; import TaggLoadingIndicator from '../../components/common/TaggLoadingIndicator'; +import {patchEditProfile} from '../../services'; type EditProfileNavigationProp = StackNavigationProp< MainStackParams, @@ -364,38 +365,15 @@ const EditProfile: React.FC<EditProfileProps> = ({route, navigation}) => { return; } - const endpoint = EDIT_PROFILE_ENDPOINT + `${userId}/`; - try { - const token = await AsyncStorage.getItem('token'); - let response = await fetch(endpoint, { - method: 'PATCH', - headers: { - 'Content-Type': 'multipart/form-data', - Authorization: 'Token ' + token, - }, - body: request, - }); - let statusCode = response.status; - let data = await response.json(); - if (statusCode === 200) { + patchEditProfile(request, userId) + .then((_) => { setNeedsUpdate(true); navigation.pop(); - } else if (statusCode === 400) { - Alert.alert( - 'Profile update failed. 😔', - data.error || 'Something went wrong! ðŸ˜', - ); - } else { - Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); - } - } catch (error) { - Alert.alert(ERROR_DOUBLE_CHECK_CONNECTION); - return { - name: 'Profile creation error', - description: error, - }; - } - }, [isCustomGender, form, navigation, userId]); + }) + .catch((error) => { + Alert.alert(error); + }); + }, [form, isCustomGender, university_class, userId, navigation]); React.useLayoutEffect(() => { navigation.setOptions({ |
