From 4e1112fbf44a14416399a28889c14599bbda52e5 Mon Sep 17 00:00:00 2001 From: aeroreyna Date: Fri, 18 Nov 2022 10:58:18 -0500 Subject: [PATCH] BE check for subscription on useEffect --- Views/Feed.js | 21 ++++++++++++++------- Views/Profile.js | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Views/Feed.js b/Views/Feed.js index 9365e3f..ba042fe 100644 --- a/Views/Feed.js +++ b/Views/Feed.js @@ -33,24 +33,31 @@ let Feed = ({ navigation, route }) => { console.log("Render Feed"); useEffect(async () => { let loggedIn = await API.isLoggedIn(); - if(!loggedIn) return navigation.reset({ + if (!loggedIn) return navigation.reset({ index: 0, routes: [{ name: 'Login' }], - }); + }); if (route.params && route.params.profileid) { - navigation.navigate('Profile', { profileid: route.params.profileid }) + return navigation.navigate('Profile', { profileid: route.params.profileid }) } + let subscribed = true; API.getMe().then((me) => { - GlobalState.me = me; + if (subscribed) + GlobalState.me = me; }); console.log("Feed from cache") let cacheFeed = await getFeed() || []; - if(cacheFeed.length) setPosts(cacheFeed); + if (cacheFeed.length && subscribed) setPosts(cacheFeed); console.log("Feed from server") let posts = await API.getPosts(); - setPosts(posts); - storeFeed(posts); + if (subscribed) { + setPosts(posts); + storeFeed(posts); + } console.log("Feed, end useEffect") + return () => { + subscribed = false; + } }, [route.params]); const renderPost = (({ item }) => { if (item.nonOrganicType === 'PopularUsers' || item.nonOrganicType === 'PopularGroups') diff --git a/Views/Profile.js b/Views/Profile.js index 550e512..b4b7d5a 100644 --- a/Views/Profile.js +++ b/Views/Profile.js @@ -5,6 +5,7 @@ import API from './../API.js'; import Post from './../components/Post.js'; import NewPost from "./../components/NewPost.js"; import ProfileHeader from '../components/ProfileHeader.js'; +import AsyncStorage from '@react-native-async-storage/async-storage'; const storeProfilePosts = async (profileid, value) => { try { @@ -18,10 +19,12 @@ const getProfilePosts = async (profileid) => { try { const value = await AsyncStorage.getItem('profile_' + profileid) if (value !== null) { + console.log(JSON.parse(value)) return JSON.parse(value); } return []; } catch (e) { + console.log('fail getProfilePosts', e) return []; } } @@ -31,17 +34,20 @@ let Profile = ({ navigation, route }) => { let [profile, setProfile] = useState({}); useEffect(async () => { + let subscribed = true; setPosts([]); if (route.params && route.params.profileid) { console.log('Loading Cache Profile:' + route.params.profileid); - getProfilePosts(route.params.profileid).then(setPosts); - console.log('Loaded Cache Profile:' + route.params.profileid); - API.getUserProfile(route.params.profileid).then((profileObj) => { + await API.getUserProfile(route.params.profileid).then((profileObj) => { + if(!subscribed) return 0; let profile = profileObj.profile setProfile(profileObj); navigation.setOptions({ title: profile.firstName + " " + profile.lastName }); }); + await getProfilePosts(route.params.profileid).then(setPosts); + console.log('Loaded Cache Profile:' + route.params.profileid); API.getPosts(route.params.profileid).then((data) => { + if(!subscribed) return 0; setPosts(data); storeProfilePosts(route.params.profileid, data); console.log('Store Cache Profile:' + route.params.profileid); @@ -49,6 +55,9 @@ let Profile = ({ navigation, route }) => { } else { navigation.navigate('Feed') } + return ()=>{ + subscribed = false; + } }, [route.params]); const renderPost = (({ item }) => { if (item.nonOrganicType)