From 0fd892ad288f2e1eaaa4fdf5e1fd6f15dbd45860 Mon Sep 17 00:00:00 2001 From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com> Date: Fri, 4 Dec 2020 08:50:24 -0800 Subject: [TMA - 398 AND TMA-430] Replace Providers with Redux Store (#125) * First * WIP * Thunk * Some more comments * sc * recent searches and follounfollow * Edit profile dummy * Block / unblock and some cleanup * Replace auth provider * Sc * Delete AP after rebase * Discover users * Cleanup * More cleanup * Replace profile provider * Fixed build failure * Fixed a bug reported * Prevent app crash when backend server is down --- src/services/BlockUserService.ts | 8 ++--- src/services/ExploreServices.ts | 7 ++--- src/services/MomentServices.ts | 31 ++++++++++++++++++-- src/services/UserFollowServices.ts | 18 +++--------- src/services/UserProfileService.ts | 60 +++++++------------------------------- 5 files changed, 47 insertions(+), 77 deletions(-) (limited to 'src/services') diff --git a/src/services/BlockUserService.ts b/src/services/BlockUserService.ts index 56243729..21e259b6 100644 --- a/src/services/BlockUserService.ts +++ b/src/services/BlockUserService.ts @@ -3,11 +3,7 @@ import {Alert} from 'react-native'; import {BLOCK_USER_ENDPOINT} from '../constants'; -export const loadBlockedUsers = async ( - userId: string, - token: string, - callback: Function, -) => { +export const loadBlockedUsers = async (userId: string, token: string) => { try { const response = await fetch(BLOCK_USER_ENDPOINT + `?user_id=${userId}`, { method: 'GET', @@ -17,7 +13,7 @@ export const loadBlockedUsers = async ( }); if (response.status === 200) { const body = await response.json(); - callback(body); + return body; } else { throw new Error(await response.json()); } diff --git a/src/services/ExploreServices.ts b/src/services/ExploreServices.ts index 7c242d57..2181ea7d 100644 --- a/src/services/ExploreServices.ts +++ b/src/services/ExploreServices.ts @@ -1,9 +1,6 @@ import {ALL_USERS_ENDPOINT} from '../constants'; -export const getAllTaggUsers = async ( - token: string, - setTaggUsers: Function, -) => { +export const getAllTaggUsers = async (token: string) => { try { const response = await fetch(ALL_USERS_ENDPOINT, { method: 'GET', @@ -14,7 +11,7 @@ export const getAllTaggUsers = async ( const status = response.status; if (status === 200) { const response_data = await response.json(); - setTaggUsers(response_data); + return response_data; } else { console.log( 'Something went wrong! 😭', diff --git a/src/services/MomentServices.ts b/src/services/MomentServices.ts index bf846b1c..46ca1351 100644 --- a/src/services/MomentServices.ts +++ b/src/services/MomentServices.ts @@ -1,7 +1,7 @@ //Common moments api abstracted out here - -import {COMMENTS_ENDPOINT} from '../constants'; +import {COMMENTS_ENDPOINT, MOMENTS_ENDPOINT} from '../constants'; import {Alert} from 'react-native'; +import {MomentType} from '../types'; //Get all comments for a moment export const getMomentComments = async ( @@ -96,3 +96,30 @@ export const getMomentCommentsCount = async ( ); } }; + +export const loadMoments: ( + userId: string, + token: string, +) => Promise = async (userId, token) => { + let moments: MomentType[] = []; + try { + const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const data = await response.json(); + moments = data; + } else { + console.log('Could not load moments!'); + return []; + } + } catch (err) { + console.log(err); + return []; + } + return moments; +}; diff --git a/src/services/UserFollowServices.ts b/src/services/UserFollowServices.ts index 105124bc..f0f176fc 100644 --- a/src/services/UserFollowServices.ts +++ b/src/services/UserFollowServices.ts @@ -8,13 +8,7 @@ import { FOLLOWING_ENDPOINT, } from '../constants'; -import {ProfilePreviewType} from 'src/types'; - -export const loadFollowers = async ( - userId: string, - token: string, - callback: Function, -) => { +export const loadFollowers = async (userId: string, token: string) => { try { const response = await fetch(FOLLOWERS_ENDPOINT + `?user_id=${userId}`, { method: 'GET', @@ -24,7 +18,7 @@ export const loadFollowers = async ( }); if (response.status === 200) { const body = await response.json(); - callback(body); + return body; } else { throw new Error(await response.json()); } @@ -33,11 +27,7 @@ export const loadFollowers = async ( } }; -export const loadFollowing = async ( - userId: string, - token: string, - callback: Function, -) => { +export const loadFollowing = async (userId: string, token: string) => { try { const response = await fetch(FOLLOWING_ENDPOINT + `?user_id=${userId}`, { method: 'GET', @@ -47,7 +37,7 @@ export const loadFollowing = async ( }); if (response.status === 200) { const body = await response.json(); - callback(body); + return body; } else { throw new Error(await response.json()); } diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index 38e04221..e69e4103 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -12,14 +12,9 @@ import { GET_IG_POSTS_ENDPOINT, GET_TWITTER_POSTS_ENDPOINT, PROFILE_INFO_ENDPOINT, - MOMENTS_ENDPOINT, } from '../constants'; -export const loadProfileInfo = async ( - token: string, - userId: string, - callback: Function, -) => { +export const loadProfileInfo = async (token: string, userId: string) => { try { const response = await fetch(PROFILE_INFO_ENDPOINT + `${userId}/`, { method: 'GET', @@ -33,7 +28,7 @@ export const loadProfileInfo = async ( let {name, biography, website, birthday, gender} = info; // user should always have a birthday, but a safety check here birthday = birthday && moment(birthday).format('YYYY-MM-DD'); - callback({name, biography, website, birthday, gender}); + return {name, biography, website, birthday, gender}; } } catch (error) { Alert.alert( @@ -43,11 +38,7 @@ export const loadProfileInfo = async ( } }; -export const loadAvatar = async ( - token: string, - userId: string, - callback: Function, -) => { +export const loadAvatar = async (token: string, userId: string) => { try { const response = await RNFetchBlob.config({ fileCache: true, @@ -57,20 +48,16 @@ export const loadAvatar = async ( }); const status = response.info().status; if (status === 200) { - callback(response.path()); + return response.path(); } else { - callback(''); + return ''; } } catch (error) { console.log(error); } }; -export const loadCover = async ( - token: string, - userId: string, - callback: Function, -) => { +export const loadCover = async (token: string, userId: string) => { try { let response = await RNFetchBlob.config({ fileCache: true, @@ -80,9 +67,9 @@ export const loadCover = async ( }); const status = response.info().status; if (status === 200) { - callback(response.path()); + return response.path(); } else { - callback(''); + return ''; } } catch (error) { console.log(error); @@ -124,37 +111,10 @@ export const loadSocialPosts: ( return accountData; }; -export const loadMoments: ( - userId: string, - token: string, -) => Promise = async (userId, token) => { - let moments: MomentType[] = []; - try { - const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - const status = response.status; - if (status === 200) { - const data = await response.json(); - moments = data; - } else { - console.log('Could not load moments!'); - return []; - } - } catch (err) { - console.log(err); - return []; - } - return moments; -}; - -export const loadRecentlySearchedUsers = async (callback: Function) => { +export const loadRecentlySearchedUsers = async () => { try { const asyncCache = await AsyncStorage.getItem('@recently_searched_users'); - asyncCache != null ? callback(JSON.parse(asyncCache)) : null; + return asyncCache != null ? JSON.parse(asyncCache) : null; } catch (e) { console.log(e); } -- cgit v1.2.3-70-g09d2