import AsyncStorage from '@react-native-community/async-storage'; import {Alert} from 'react-native'; import { FRIENDS_ENDPOINT, GET_REMAINING_INVITES, INVITE_FRIEND_ENDPOINT, USERS_FROM_CONTACTS_ENDPOINT, } from '../constants'; import {ERROR_SOMETHING_WENT_WRONG_REFRESH} from '../constants/strings'; import {ContactType, FriendshipStatusType} from '../types'; export const loadFriends = async (userId: string, token: string) => { try { const response = await fetch(FRIENDS_ENDPOINT + `?user_id=${userId}`, { method: 'GET', headers: { Authorization: 'Token ' + token, }, }); if (response.status === 200) { const body = await response.json(); return body; } } catch (error) { console.log(error); } return []; }; export const friendOrUnfriendUser = async ( friend: string, token: string, friendship_status: FriendshipStatusType, ) => { try { let body; let method = ''; let endpoint = FRIENDS_ENDPOINT; switch (friendship_status) { case 'no_record': method = 'POST'; body = JSON.stringify({ requested: friend, }); break; case 'requested': method = 'DELETE'; endpoint += `${friend}/`; body = JSON.stringify({ reason: 'cancelled', }); break; case 'friends': method = 'DELETE'; endpoint += `${friend}/`; body = JSON.stringify({ reason: 'unfriended', }); } const response = await fetch(endpoint, { method: method, headers: { 'Content-Type': 'application/json', Authorization: 'Token ' + token, }, body: body, }); const status = response.status; if (Math.floor(status / 100) === 2) { return true; } else { console.log(await response.json()); Alert.alert( 'Something went wrong! 😭', "Would you believe me if I told you that I don't know what happened?", ); return false; } } catch (error) { console.log(error); Alert.alert( 'Something went wrong! 😭', "Would you believe me if I told you that I don't know what happened?", ); return false; } }; export const addFriendService = async (friend: string, token: string) => { try { const response = await fetch(FRIENDS_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Token ' + token, }, body: JSON.stringify({ requested: friend, }), }); const status = response.status; if (Math.floor(status / 100) === 2) { return true; } else { console.log(await response.json()); Alert.alert( 'Something went wrong! 😭', "Would you believe me if I told you that I don't know what happened?", ); return false; } } catch (error) { console.log(error); Alert.alert( 'Something went wrong! 😭', "Would you believe me if I told you that I don't know what happened?", ); return false; } }; export const acceptFriendRequestService = async ( requester_id: string, token: string | null, ) => { try { const response = await fetch(FRIENDS_ENDPOINT + `${requester_id}/`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', Authorization: 'Token ' + token, }, }); const status = response.status; if (Math.floor(status / 100) === 2) { return true; } else { console.log(await response.json()); Alert.alert( 'Something went wrong! 😭', "Would you believe me if I told you that I don't know what happened?", ); return false; } } catch (error) { console.log(error); Alert.alert( 'Something went wrong! 😭', "Would you believe me if I told you that I don't know what happened?", ); return false; } }; export const deleteFriendshipService = async ( user_id: string, reason: 'declined' | 'cancelled' | 'unfriended', token: string | null, ) => { try { console.log('deleteFriendshipService!'); const response = await fetch(FRIENDS_ENDPOINT + `${user_id}/`, { method: 'DELETE', headers: { Authorization: 'Token ' + token, }, body: JSON.stringify({ reason, }), }); const status = response.status; if (Math.floor(status / 100) === 2) { return true; } else { console.log(await response.json()); Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); return false; } } catch (error) { console.log(error); Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); return false; } }; export const usersFromContactsService = async ( contacts: Array, ) => { 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, reminding: boolean, ) => { 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, reminding: reminding, }), }); if (response.status === 201 || response.status === 200) { return await response.json(); } else { return undefined; } } catch (error) { console.log(error); Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); 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); } };