aboutsummaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/authentication/AuthProvider.tsx68
-rw-r--r--src/routes/profile/Profile.tsx2
-rw-r--r--src/routes/viewProfile/ProfileProvider.tsx49
3 files changed, 90 insertions, 29 deletions
diff --git a/src/routes/authentication/AuthProvider.tsx b/src/routes/authentication/AuthProvider.tsx
index 8dd9fd73..a705f074 100644
--- a/src/routes/authentication/AuthProvider.tsx
+++ b/src/routes/authentication/AuthProvider.tsx
@@ -1,19 +1,23 @@
-import React, {useEffect} from 'react';
-import {createContext, useState} from 'react';
-import {
- UserType,
- ProfileType,
- InstagramPostType,
- ProfilePreviewType,
-} from '../../types';
import AsyncStorage from '@react-native-community/async-storage';
+import React, {createContext, useEffect, useState} from 'react';
+import {
+ GET_FB_POSTS_ENDPOINT,
+ GET_IG_POSTS_ENDPOINT,
+ GET_TWITTER_POSTS_ENDPOINT,
+} from '../../constants';
import {
- loadProfileInfo,
loadAvatar,
loadCover,
- loadInstaPosts,
+ loadProfileInfo,
loadRecentlySearchedUsers,
+ loadSocialPosts,
} from '../../services';
+import {
+ ProfilePreviewType,
+ ProfileType,
+ SocialAccountType,
+ UserType,
+} from '../../types';
interface AuthContextProps {
user: UserType;
@@ -22,20 +26,30 @@ interface AuthContextProps {
logout: () => void;
avatar: string | null;
cover: string | null;
- instaPosts: Array<InstagramPostType>;
+ socialAccounts: Record<string, SocialAccountType>;
recentSearches: Array<ProfilePreviewType>;
newMomentsAvailable: boolean;
updateMoments: (value: boolean) => void;
}
+
const NO_USER: UserType = {
userId: '',
username: '',
};
+
const NO_PROFILE: ProfileType = {
biography: '',
website: '',
name: '',
};
+
+// Not necessary, but safer, in case SocialAccountType object is undefined
+const NO_SOCIAL_ACCOUNTS: Record<string, SocialAccountType> = {
+ Instagram: {},
+ Facebook: {},
+ Twitter: {},
+};
+
export const AuthContext = createContext<AuthContextProps>({
user: NO_USER,
profile: NO_PROFILE,
@@ -43,7 +57,7 @@ export const AuthContext = createContext<AuthContextProps>({
logout: () => {},
avatar: null,
cover: null,
- instaPosts: [],
+ socialAccounts: NO_SOCIAL_ACCOUNTS,
recentSearches: [],
newMomentsAvailable: true,
updateMoments: () => {},
@@ -57,7 +71,9 @@ const AuthProvider: React.FC = ({children}) => {
const [profile, setProfile] = useState<ProfileType>(NO_PROFILE);
const [avatar, setAvatar] = useState<string | null>(null);
const [cover, setCover] = useState<string | null>(null);
- const [instaPosts, setInstaPosts] = useState<Array<InstagramPostType>>([]);
+ const [socialAccounts, setSocialAccounts] = useState<
+ Record<string, SocialAccountType>
+ >(NO_SOCIAL_ACCOUNTS);
const [recentSearches, setRecentSearches] = useState<
Array<ProfilePreviewType>
>([]);
@@ -78,14 +94,34 @@ const AuthProvider: React.FC = ({children}) => {
loadProfileInfo(token, userId, setProfile);
loadAvatar(token, userId, setAvatar);
loadCover(token, userId, setCover);
- loadInstaPosts(token, userId, setInstaPosts);
loadRecentlySearchedUsers(setRecentSearches);
+ loadSocialPosts(
+ token,
+ userId,
+ 'Instagram',
+ GET_IG_POSTS_ENDPOINT,
+ socialAccounts,
+ ).then((newSocialAccounts) => setSocialAccounts(newSocialAccounts));
+ loadSocialPosts(
+ token,
+ userId,
+ 'Facebook',
+ GET_FB_POSTS_ENDPOINT,
+ socialAccounts,
+ ).then((newSocialAccounts) => setSocialAccounts(newSocialAccounts));
+ loadSocialPosts(
+ token,
+ userId,
+ 'Twitter',
+ GET_TWITTER_POSTS_ENDPOINT,
+ socialAccounts,
+ ).then((newSocialAccounts) => setSocialAccounts(newSocialAccounts));
} catch (err) {
console.log(err);
}
};
loadData();
- }, [userId]);
+ }, [socialAccounts, userId]);
return (
<AuthContext.Provider
@@ -94,8 +130,8 @@ const AuthProvider: React.FC = ({children}) => {
profile,
avatar,
cover,
- instaPosts,
newMomentsAvailable,
+ socialAccounts,
login: (id, username) => {
setUser({...user, userId: id, username});
},
diff --git a/src/routes/profile/Profile.tsx b/src/routes/profile/Profile.tsx
index 363e9e21..736127bf 100644
--- a/src/routes/profile/Profile.tsx
+++ b/src/routes/profile/Profile.tsx
@@ -42,7 +42,7 @@ const Profile: React.FC<ProfileStackProps> = ({route}) => {
}),
}}
mode="modal"
- initialRouteName={!isProfileView ? `Profile` : `Search`}>
+ initialRouteName={!isProfileView ? 'Profile' : 'Search'}>
<ProfileStack.Screen
name="Profile"
component={ProfileScreen}
diff --git a/src/routes/viewProfile/ProfileProvider.tsx b/src/routes/viewProfile/ProfileProvider.tsx
index 8fb9a011..a4b6cb14 100644
--- a/src/routes/viewProfile/ProfileProvider.tsx
+++ b/src/routes/viewProfile/ProfileProvider.tsx
@@ -1,14 +1,16 @@
-import React, {useEffect} from 'react';
-import {createContext, useState} from 'react';
import AsyncStorage from '@react-native-community/async-storage';
-import {UserType, ProfileType, InstagramPostType} from '../../types';
-
+import React, {createContext, useEffect, useState} from 'react';
+import {
+ GET_IG_POSTS_ENDPOINT,
+ GET_TWITTER_POSTS_ENDPOINT,
+} from '../../constants';
import {
- loadProfileInfo,
loadAvatar,
loadCover,
- loadInstaPosts,
+ loadProfileInfo,
+ loadSocialPosts,
} from '../../services';
+import {ProfileType, SocialAccountType, UserType} from '../../types';
interface ProfileContextProps {
user: UserType;
@@ -16,9 +18,9 @@ interface ProfileContextProps {
loadProfile: (userId: string, username: string) => void;
avatar: string | null;
cover: string | null;
- instaPosts: Array<InstagramPostType>;
newMomentsAvailable: boolean;
updateMoments: (value: boolean) => void;
+ socialAccounts: Record<string, SocialAccountType>;
}
const NO_USER: UserType = {
userId: '',
@@ -29,15 +31,23 @@ const NO_PROFILE: ProfileType = {
website: '',
name: '',
};
+
+// Not necessary, but safer, in case SocialAccountType object is undefined
+const NO_SOCIAL_ACCOUNTS: Record<string, SocialAccountType> = {
+ Instagram: {},
+ Facebook: {},
+ Twitter: {},
+};
+
export const ProfileContext = createContext<ProfileContextProps>({
user: NO_USER,
profile: NO_PROFILE,
loadProfile: () => {},
avatar: null,
cover: null,
- instaPosts: [],
newMomentsAvailable: true,
updateMoments: () => {},
+ socialAccounts: NO_SOCIAL_ACCOUNTS,
});
/**
@@ -48,9 +58,11 @@ const ProfileProvider: React.FC = ({children}) => {
const [profile, setProfile] = useState<ProfileType>(NO_PROFILE);
const [avatar, setAvatar] = useState<string | null>(null);
const [cover, setCover] = useState<string | null>(null);
- const [instaPosts, setInstaPosts] = useState<Array<InstagramPostType>>([]);
const [newMomentsAvailable, setNewMomentsAvailable] = useState<boolean>(true);
+ const [socialAccounts, setSocialAccounts] = useState<
+ Record<string, SocialAccountType>
+ >(NO_SOCIAL_ACCOUNTS);
const {userId} = user;
useEffect(() => {
if (!userId) {
@@ -67,13 +79,26 @@ const ProfileProvider: React.FC = ({children}) => {
loadProfileInfo(token, userId, setProfile);
loadAvatar(token, userId, setAvatar);
loadCover(token, userId, setCover);
- loadInstaPosts(token, userId, setInstaPosts);
+ loadSocialPosts(
+ token,
+ userId,
+ 'Instagram',
+ GET_IG_POSTS_ENDPOINT,
+ socialAccounts,
+ ).then((newSocialAccounts) => setSocialAccounts(newSocialAccounts));
+ loadSocialPosts(
+ token,
+ userId,
+ 'Twitter',
+ GET_TWITTER_POSTS_ENDPOINT,
+ socialAccounts,
+ ).then((newSocialAccounts) => setSocialAccounts(newSocialAccounts));
} catch (err) {
console.log(err);
}
};
loadData();
- }, [userId]);
+ }, [socialAccounts, userId]);
return (
<ProfileContext.Provider
@@ -82,8 +107,8 @@ const ProfileProvider: React.FC = ({children}) => {
profile,
avatar,
cover,
- instaPosts,
newMomentsAvailable,
+ socialAccounts,
loadProfile: (id, username) => {
setUser({...user, userId: id, username});
},