diff options
author | Ivan Chen <ivan@tagg.id> | 2021-03-18 19:48:53 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-18 19:48:53 -0400 |
commit | aa0ddb7c5a6612ff067f7dce1c6d5b083db44309 (patch) | |
tree | 89326275ce5ff4e48bc29952c60856258cd8b0ab /src/services/UserFriendsService.ts | |
parent | 07a15098625786451270e30e61e2d6e78c02d4db (diff) | |
parent | 9a7e34bf992e0bfa3b9ce7d83643d97fad209e6e (diff) |
Merge pull request #303 from shravyaramesh/add-friends-thru-contacts
[TMA-622] Add friends from contacts
Diffstat (limited to 'src/services/UserFriendsService.ts')
-rw-r--r-- | src/services/UserFriendsService.ts | 69 |
1 files changed, 66 insertions, 3 deletions
diff --git a/src/services/UserFriendsService.ts b/src/services/UserFriendsService.ts index dbec1974..cea20fbe 100644 --- a/src/services/UserFriendsService.ts +++ b/src/services/UserFriendsService.ts @@ -1,9 +1,17 @@ //Abstracted common friends api calls out here +import AsyncStorage from '@react-native-community/async-storage'; import {Alert} from 'react-native'; -import {FriendshipStatusType} from '../types'; -import {FRIENDS_ENDPOINT} from '../constants'; -import {ERROR_SOMETHING_WENT_WRONG_REFRESH} from '../constants/strings'; +import {ContactType, FriendshipStatusType} from '../types'; +import { + FRIENDS_ENDPOINT, + INVITE_FRIEND_ENDPOINT, + USERS_FROM_CONTACTS_ENDPOINT, +} from '../constants'; +import { + ERROR_SOMETHING_WENT_WRONG, + ERROR_SOMETHING_WENT_WRONG_REFRESH, +} from '../constants/strings'; export const loadFriends = async (userId: string, token: string) => { try { @@ -181,3 +189,58 @@ export const deleteFriendshipService = async ( return false; } }; + +export const usersFromContactsService = async ( + contacts: Array<ContactType>, +) => { + console.log('Contacts: ', contacts); + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(USERS_FROM_CONTACTS_ENDPOINT, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + contacts: contacts, + }), + }); + const status = response.status; + if (Math.floor(status / 100) === 2) { + const users_data = await response.json(); + return users_data; + } else { + console.log( + 'Something went wrong! ðŸ˜', + 'Not able to retrieve tagg users list', + ); + } + } catch (error) { + console.log(error); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + return false; + } +}; + +export const inviteFriendService = async ( + phoneNumber: string, + 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) { + return await response.json(); + } + return false; +}; |