//Abstracted common user follow api calls out here import {Alert} from 'react-native'; import { FOLLOWERS_ENDPOINT, FOLLOW_USER_ENDPOINT, UNFOLLOW_USER_ENDPOINT, FOLLOWING_ENDPOINT, } from '../constants'; import {ProfilePreviewType} from 'src/types'; export const loadFollowers = async ( userId: string, token: string, callback: Function, ) => { try { const response = await fetch(FOLLOWERS_ENDPOINT + `?user_id=${userId}`, { method: 'GET', headers: { Authorization: 'Token ' + token, }, }); if (response.status === 200) { const body = await response.json(); callback(body); } else { throw new Error(await response.json()); } } catch (error) { console.log(error); } }; export const loadFollowing = async ( userId: string, token: string, callback: Function, ) => { try { const response = await fetch(FOLLOWING_ENDPOINT + `?user_id=${userId}`, { method: 'GET', headers: { Authorization: 'Token ' + token, }, }); if (response.status === 200) { const body = await response.json(); callback(body); } else { throw new Error(await response.json()); } } catch (error) { console.log(error); } }; export const followOrUnfollowUser = async ( follower: string, followed: string, token: string, isFollowed: boolean, ) => { try { const endpoint = isFollowed ? UNFOLLOW_USER_ENDPOINT : FOLLOW_USER_ENDPOINT; const response = await fetch(endpoint, { method: 'POST', headers: { Authorization: 'Token ' + token, }, body: JSON.stringify({ follower, followed, }), }); if (Math.floor(response.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; } };