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/onboarding/OnboardingStepThree.tsx | |
parent | 5b975a354f0707c0671571387d5bda501fbe75ae (diff) | |
parent | bef5728b24a71d1bf327a72e425346020a997037 (diff) |
Merge branch 'master' into tma-722-cornell-fe
# Conflicts:
# src/types/types.ts
Diffstat (limited to 'src/screens/onboarding/OnboardingStepThree.tsx')
-rw-r--r-- | src/screens/onboarding/OnboardingStepThree.tsx | 100 |
1 files changed, 43 insertions, 57 deletions
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} |