diff options
Diffstat (limited to 'src/services')
-rw-r--r-- | src/services/UserFollowServices.ts | 68 | ||||
-rw-r--r-- | src/services/UserProfileService.ts | 2 | ||||
-rw-r--r-- | src/services/index.ts | 1 |
3 files changed, 69 insertions, 2 deletions
diff --git a/src/services/UserFollowServices.ts b/src/services/UserFollowServices.ts new file mode 100644 index 00000000..bfd8058a --- /dev/null +++ b/src/services/UserFollowServices.ts @@ -0,0 +1,68 @@ +//Abstracted common user follow api calls out here + +import {Alert} from 'react-native'; +import { + FOLLOWERS_ENDPOINT, + FOLLOW_USER_ENDPOINT, + UNFOLLOW_USER_ENDPOINT, +} from '../constants'; + +import {ProfilePreviewType} from 'src/types'; + +export const loadFollowers = async (userId: string, token: string) => { + 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(); + return body; + } else { + throw new Error(await response.json()); + } + } catch (error) { + console.log(error); + } + return []; +}; + +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; + } +}; diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index 59f9649a..f5523be4 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -5,8 +5,6 @@ import { PROFILE_INFO_ENDPOINT, AVATAR_PHOTO_ENDPOINT, COVER_PHOTO_ENDPOINT, - GET_IG_POSTS_ENDPOINT, - GET_TWITTER_POSTS_ENDPOINT, } from '../constants'; import AsyncStorage from '@react-native-community/async-storage'; diff --git a/src/services/index.ts b/src/services/index.ts index 2abcef95..aa13dbe3 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,2 +1,3 @@ export * from './UserProfileService'; export * from './MomentServices'; +export * from './UserFollowServices'; |