diff options
author | ankit-thanekar007 <ankit.thanekar007@gmail.com> | 2021-03-25 13:51:14 -0700 |
---|---|---|
committer | ankit-thanekar007 <ankit.thanekar007@gmail.com> | 2021-03-29 12:07:23 -0700 |
commit | 7a521127177838bcae0cd85b2e5bd912c46406b9 (patch) | |
tree | 19a470ea25cf725bb403060f27e397344bc6b373 /src | |
parent | ff3f0acadf810cd1b5bf276b9ee926f2c480b7be (diff) |
Added API for updating account toggle
Diffstat (limited to 'src')
-rw-r--r-- | src/constants/strings.ts | 1 | ||||
-rw-r--r-- | src/screens/profile/AccountType.tsx | 31 | ||||
-rw-r--r-- | src/screens/profile/PrivacyScreen.tsx | 17 | ||||
-rw-r--r-- | src/services/UserProfileService.ts | 47 |
4 files changed, 77 insertions, 19 deletions
diff --git a/src/constants/strings.ts b/src/constants/strings.ts index 019d0bea..4f792dcc 100644 --- a/src/constants/strings.ts +++ b/src/constants/strings.ts @@ -31,6 +31,7 @@ export const ERROR_NEXT_PAGE = 'There was a problem while loading the next page export const ERROR_NOT_ONBOARDED = 'You are now on waitlist, please enter your invitation code if you have one'; export const ERROR_PHONE_IN_USE = 'Phone already in use, please try another one'; export const ERROR_PROFILE_CREATION_SHORT = 'Profile creation failed 😓'; +export const ERROR_PROFILE_UPDATE_SHORT = 'Profile update failed. 😔'; export const ERROR_PWD_ACCOUNT = (str: string) => `Please make sure that the email / username entered is registered with us. You may contact our customer support at ${str}`; export const ERROR_REGISTRATION = (str: string) => `Registration failed 😔, ${str}`; export const ERROR_SELECT_BIRTHDAY = 'Please select your birthday'; diff --git a/src/screens/profile/AccountType.tsx b/src/screens/profile/AccountType.tsx index dedf4420..e1e19389 100644 --- a/src/screens/profile/AccountType.tsx +++ b/src/screens/profile/AccountType.tsx @@ -1,22 +1,33 @@ -import React, {useEffect, useState} from 'react'; +import React, {useState} from 'react'; import {StatusBar, StyleSheet, Switch, Text, View} from 'react-native'; import {SafeAreaView} from 'react-native-safe-area-context'; +import {useDispatch, useSelector} from 'react-redux'; import {Background} from '../../components'; +import {updateProfileVisibility} from '../../services'; +import {NO_PROFILE} from '../../store/initialStates'; +import {RootState} from '../../store/rootReducer'; import {BackgroundGradientType} from '../../types'; +import {getTokenOrLogout} from '../../utils'; import {normalize} from '../../utils/layouts'; const AccountType: React.FC = () => { const [isPrivateAccount, setIsPrivateAccount] = useState(false); - const switchAccountType = () => - setIsPrivateAccount((previousState) => !previousState); + const { + user: {userId, username}, + profile: {is_private} = NO_PROFILE, + } = useSelector((state: RootState) => state.user); - useEffect(() => { - // Fetching from Redux and Set the result - // setIsPrivateAccount(true); - }, []); + const dispatch = useDispatch(); + + const updateAccountVisibility = async () => { + const isPrivate = !isPrivateAccount; + setIsPrivateAccount(isPrivate); + const token = await getTokenOrLogout(dispatch); + updateProfileVisibility(token, {userId, username}, isPrivate, dispatch); + }; const getAccountText = () => { - return isPrivateAccount ? 'Private Account' : 'Public Account'; + return is_private ? 'Private Account' : 'Public Account'; }; return ( @@ -37,8 +48,8 @@ const AccountType: React.FC = () => { borderWidth: 2, borderColor: 'white', }} - value={isPrivateAccount} - onValueChange={switchAccountType}></Switch> + value={is_private} + onValueChange={updateAccountVisibility}></Switch> </View> <View style={{marginTop: '40%'}}> diff --git a/src/screens/profile/PrivacyScreen.tsx b/src/screens/profile/PrivacyScreen.tsx index eea0230a..37f042af 100644 --- a/src/screens/profile/PrivacyScreen.tsx +++ b/src/screens/profile/PrivacyScreen.tsx @@ -1,6 +1,6 @@ +import {useNavigation} from '@react-navigation/core'; import React from 'react'; import { - Alert, Image, SectionList, StatusBar, @@ -10,14 +10,12 @@ import { View, } from 'react-native'; import {SafeAreaView} from 'react-native-safe-area-context'; -import {normalize, SCREEN_WIDTH} from '../../utils/layouts'; +import {useSelector} from 'react-redux'; +import {RootState} from 'src/store/rootReducer'; import {Background} from '../../components'; +import {NO_PROFILE} from '../../store/initialStates'; import {BackgroundGradientType} from '../../types'; -import {logout} from '../../store/actions'; -import {useDispatch, useSelector} from 'react-redux'; -import {useNavigation} from '@react-navigation/core'; -import {RootState} from 'src/store/rootReducer'; -import {ERROR_ATTEMPT_EDIT_SP} from '../../constants/strings'; +import {normalize, SCREEN_WIDTH} from '../../utils/layouts'; const DATA = [ { @@ -39,6 +37,9 @@ const DATA = [ const PrivacyScreen: React.FC = () => { const navigation = useNavigation(); + const {profile: {is_private} = NO_PROFILE} = useSelector( + (state: RootState) => state.user, + ); const getActions = (type: string) => { switch (type) { @@ -78,7 +79,7 @@ const PrivacyScreen: React.FC = () => { <View style={[styles.item, {position: 'absolute', right: 0}]}> {title === 'Account Type' && ( <Text style={[styles.title, {color: '#C4C4C4', marginRight: 13}]}> - {'Private'} + {is_private ? 'Private' : 'Public'} </Text> )} <Image style={{width: 15, height: 15}} source={postimage} /> diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index 085787c3..c1901be1 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -1,6 +1,8 @@ import AsyncStorage from '@react-native-community/async-storage'; import moment from 'moment'; +import {useEffect} from 'react'; import {Alert} from 'react-native'; +import {loadUserData} from '../store/actions'; import { EDIT_PROFILE_ENDPOINT, GET_FB_POSTS_ENDPOINT, @@ -20,6 +22,7 @@ import { ERROR_DOUBLE_CHECK_CONNECTION, ERROR_DUP_OLD_PWD, ERROR_INVALID_PWD_CODE, + ERROR_PROFILE_UPDATE_SHORT, ERROR_PWD_ACCOUNT, ERROR_SOMETHING_WENT_WRONG, ERROR_SOMETHING_WENT_WRONG_REFRESH, @@ -27,7 +30,12 @@ import { SUCCESS_PWD_RESET, SUCCESS_VERIFICATION_CODE_SENT, } from '../constants/strings'; -import {ProfileInfoType, ProfileType, SocialAccountType} from '../types'; +import { + ProfileInfoType, + ProfileType, + SocialAccountType, + UserType, +} from '../types'; export const loadProfileInfo = async (token: string, userId: string) => { try { @@ -75,6 +83,43 @@ export const getProfilePic = async ( } }; +export const updateProfileVisibility = async ( + token: string, + user: UserType, + isPrivateAccount: Boolean, + dispatch: Function, +) => { + try { + const url = EDIT_PROFILE_ENDPOINT + `${user.userId}/`; + const request = new FormData(); + request.append('is_private', isPrivateAccount); + let response = await fetch(url, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Token ' + token, + }, + body: request, + }); + const {status} = response; + let data = await response.json(); + if (status === 200) { + dispatch(loadUserData(user)); + } else if (status >= 400) { + Alert.alert( + ERROR_PROFILE_UPDATE_SHORT, + data.error || 'Something went wrong! ðŸ˜', + ); + } + } catch (error) { + Alert.alert(ERROR_PROFILE_UPDATE_SHORT, ERROR_DOUBLE_CHECK_CONNECTION); + return { + name: 'Profile update error', + description: error, + }; + } +}; + const integratedSocialPostsEndpoints: {[social: string]: string} = { Facebook: GET_FB_POSTS_ENDPOINT, Instagram: GET_IG_POSTS_ENDPOINT, |