diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components/friends/InviteFriendTile.tsx | 44 | ||||
-rw-r--r-- | src/services/UserFriendsService.ts | 65 |
2 files changed, 55 insertions, 54 deletions
diff --git a/src/components/friends/InviteFriendTile.tsx b/src/components/friends/InviteFriendTile.tsx index 9967b365..c47c8764 100644 --- a/src/components/friends/InviteFriendTile.tsx +++ b/src/components/friends/InviteFriendTile.tsx @@ -1,4 +1,3 @@ -import AsyncStorage from '@react-native-community/async-storage'; import React, {useEffect, useState} from 'react'; import { Alert, @@ -11,11 +10,7 @@ import { } from 'react-native'; import {useSelector} from 'react-redux'; import {RootState} from 'src/store/rootReducer'; -import { - CREATE_INVITE_CODE, - GET_REMAINING_INVITES, - TAGG_LIGHT_BLUE, -} from '../../constants'; +import {TAGG_LIGHT_BLUE} from '../../constants'; import { ERROR_NO_CONTACT_INVITE_LEFT, ERROR_SOMETHING_WENT_WRONG, @@ -25,7 +20,11 @@ import { SUCCESS_LAST_CONTACT_INVITE, } from '../../constants/strings'; import {InviteContactType} from '../../screens/profile/InviteFriendsScreen'; -import {inviteFriendService} from '../../services'; +import { + getRemainingInviteCount, + handleCreateInviteCode, + inviteFriendService, +} from '../../services'; import {normalize} from '../../utils'; interface InviteFriendTileProps { @@ -75,37 +74,6 @@ const InviteFriendTile: React.FC<InviteFriendTileProps> = ({item}) => { } }; - const getRemainingInviteCount = async () => { - const firstName = name.split(' ')[0]; - const lastName = name.split(' ')[1]; - const token = await AsyncStorage.getItem('token'); - const response = await fetch(GET_REMAINING_INVITES, { - method: 'POST', - headers: { - Authorization: 'Token ' + token, - }, - body: JSON.stringify({ - invitee_first_name: firstName, - invitee_last_name: lastName, - }), - }); - if (response.status === 200) { - const data = await response.json(); - return data.invites_left; - } else if (response.status === 500) { - return -1; - } - }; - - const handleCreateInviteCode = async () => { - const response = await fetch(CREATE_INVITE_CODE, {method: 'POST'}); - if (response.status === 200) { - const data = await response.json(); - return data.code; - } else if (response.status === 500) { - return -1; - } - }; useEffect(() => { const formatPhoneNumer = () => { const unformatted_number: string = item.phoneNumber; diff --git a/src/services/UserFriendsService.ts b/src/services/UserFriendsService.ts index deb3ec6d..453f35a5 100644 --- a/src/services/UserFriendsService.ts +++ b/src/services/UserFriendsService.ts @@ -1,7 +1,9 @@ import AsyncStorage from '@react-native-community/async-storage'; import {Alert} from 'react-native'; import { + CREATE_INVITE_CODE, FRIENDS_ENDPOINT, + GET_REMAINING_INVITES, INVITE_FRIEND_ENDPOINT, USERS_FROM_CONTACTS_ENDPOINT, } from '../constants'; @@ -220,23 +222,54 @@ export const inviteFriendService = async ( inviteeFirstName: string, inviteeLastName: string, ) => { - const token = await AsyncStorage.getItem('token'); - const response = await fetch(INVITE_FRIEND_ENDPOINT, { - method: 'POST', - headers: { - Authorization: 'Token ' + token, - }, - body: JSON.stringify({ - invitee_phone_number: phoneNumber, - invitee_first_name: inviteeFirstName, - invitee_last_name: inviteeLastName, - }), - }); - if (response.status === 201 || response.status === 200) { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(INVITE_FRIEND_ENDPOINT, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + invitee_phone_number: phoneNumber, + invitee_first_name: inviteeFirstName, + invitee_last_name: inviteeLastName, + }), + }); + return response.status === 201 || response.status === 200; + } catch (error) { + console.log(error); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + return false; + } +}; + +export const handleCreateInviteCode = async () => { + const response = await fetch(CREATE_INVITE_CODE, {method: 'POST'}); + if (response.status === 200) { const data = await response.json(); - return data.invites_left; - } else if (response.status === 400) { + return data.code; + } else if (response.status === 500) { return -1; } - return false; +}; + +export const getRemainingInviteCount = async () => { + const token = await AsyncStorage.getItem('token'); + try { + const response = await fetch(GET_REMAINING_INVITES, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + if (response.status === 200) { + const data = await response.json(); + return data.invites_left; + } else if (response.status === 500) { + return -1; + } + } catch (error) { + console.log(error); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + } }; |