From 4844b69ed6c381fe8e573e77d32302965c4de274 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 23 Mar 2021 16:18:19 -0400 Subject: updated types, using api/profile --- src/utils/friends.ts | 4 ++-- src/utils/users.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/utils') diff --git a/src/utils/friends.ts b/src/utils/friends.ts index 3398c123..6e3b645a 100644 --- a/src/utils/friends.ts +++ b/src/utils/friends.ts @@ -1,7 +1,7 @@ // Handles click on friend/requested/unfriend button import {RootState} from '../store/rootReducer'; -import {ProfilePreviewType, ProfileType, ScreenType, UserType} from '../types'; +import {ProfilePreviewType, ProfileInfoType, ScreenType, UserType} from '../types'; import {AppDispatch} from '../store/configureStore'; import { addFriend, @@ -25,7 +25,7 @@ import {getUserAsProfilePreviewType} from './users'; export const handleFriendUnfriend = async ( screenType: ScreenType, user: UserType, - profile: ProfileType, + profile: ProfileInfoType, dispatch: AppDispatch, state: RootState, loggedInUser: UserType, diff --git a/src/utils/users.ts b/src/utils/users.ts index af4f3813..d1f416e0 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -20,7 +20,7 @@ import {RootState} from './../store/rootReducer'; import { ProfilePreviewType, CategoryPreviewType, - ProfileType, + ProfileInfoType, ScreenType, UserType, } from './../types/types'; @@ -137,7 +137,7 @@ export const getTokenOrLogout = async (dispatch: Function): Promise => { */ export const getUserAsProfilePreviewType = ( passedInUser: UserType, - passedInProfile: ProfileType, + passedInProfile: ProfileInfoType, ): ProfilePreviewType => { const fullName = passedInProfile.name.split(' '); return { -- cgit v1.2.3-70-g09d2 From 6e054a927c390cbc6aba4185921397cdcd867e33 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Wed, 24 Mar 2021 11:25:58 -0400 Subject: cleaned up logic --- src/services/UserFriendsService.ts | 3 +-- src/services/UserProfileService.ts | 7 +++++-- src/store/actions/userX.ts | 2 +- src/utils/users.ts | 8 +++++--- 4 files changed, 12 insertions(+), 8 deletions(-) (limited to 'src/utils') diff --git a/src/services/UserFriendsService.ts b/src/services/UserFriendsService.ts index dbec1974..5ce9df29 100644 --- a/src/services/UserFriendsService.ts +++ b/src/services/UserFriendsService.ts @@ -16,12 +16,11 @@ export const loadFriends = async (userId: string, token: string) => { 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 friendOrUnfriendUser = async ( diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index e733cb47..828cdbf7 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -107,8 +107,11 @@ const integratedSocialPostsEndpoints: {[social: string]: string} = { export const loadSocialPosts: ( userId: string, socialType: string, -) => Promise = async (userId, socialType) => { - const token = await AsyncStorage.getItem('token'); + token?: string, +) => Promise = async (userId, socialType, token) => { + if (!token) { + token = (await AsyncStorage.getItem('token')) ?? ''; + } const endpoint = integratedSocialPostsEndpoints[socialType]; const accountData: SocialAccountType = {}; accountData.posts = []; diff --git a/src/store/actions/userX.ts b/src/store/actions/userX.ts index 5d49cdf9..6302eb3c 100644 --- a/src/store/actions/userX.ts +++ b/src/store/actions/userX.ts @@ -88,7 +88,7 @@ export const loadUserX = ( }); } }); - loadAllSocialsForUser(userId).then((data) => + loadAllSocialsForUser(userId, token).then((data) => dispatch({ type: userXSocialsFetched.type, payload: {screenType, userId, data}, diff --git a/src/utils/users.ts b/src/utils/users.ts index d1f416e0..e11e8c78 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -103,12 +103,15 @@ export const userXInStore = ( * Abstracted the code to laod all socials out. * @param userId userId for whom socials should be fetched */ -export const loadAllSocialsForUser = async (userId: string) => { +export const loadAllSocialsForUser = async (userId: string, token?: string) => { + if (!token) { + token = (await AsyncStorage.getItem('token')) ?? ''; + } let socials = NO_SOCIAL_ACCOUNTS; try { let socialNeedsUpdate = INTEGRATED_SOCIAL_LIST; for (let socialType of socialNeedsUpdate) { - const social = await loadSocialPosts(userId, socialType); + const social = await loadSocialPosts(userId, socialType, token); socials = {...socials, [socialType]: social}; } return socials; @@ -165,4 +168,3 @@ export const defaultUserProfile = () => { const defaultImage = require('../assets/images/avatar-placeholder.png'); return defaultImage; }; - -- cgit v1.2.3-70-g09d2 From 96477697afe4dd92ce68f0f778decbca30d83e77 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Wed, 24 Mar 2021 13:21:59 -0400 Subject: async social load --- src/utils/users.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/utils') diff --git a/src/utils/users.ts b/src/utils/users.ts index e11e8c78..f54d461c 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -109,10 +109,16 @@ export const loadAllSocialsForUser = async (userId: string, token?: string) => { } let socials = NO_SOCIAL_ACCOUNTS; try { - let socialNeedsUpdate = INTEGRATED_SOCIAL_LIST; - for (let socialType of socialNeedsUpdate) { - const social = await loadSocialPosts(userId, socialType, token); - socials = {...socials, [socialType]: social}; + const fetchedSocials = await Promise.all( + INTEGRATED_SOCIAL_LIST.map((socialType) => + loadSocialPosts(userId, socialType, token).then((data) => ({ + key: socialType, + data, + })), + ), + ); + for (const fetchedSocial of fetchedSocials) { + socials = {...socials, [fetchedSocial.key]: fetchedSocial.data}; } return socials; } catch (error) { -- cgit v1.2.3-70-g09d2