BE check for subscription on useEffect

This commit is contained in:
aeroreyna
2022-11-18 10:58:18 -05:00
parent 193284e00d
commit 4e1112fbf4
2 changed files with 26 additions and 10 deletions

View File

@@ -38,19 +38,26 @@ let Feed = ({ navigation, route }) => {
routes: [{ name: 'Login' }], routes: [{ name: 'Login' }],
}); });
if (route.params && route.params.profileid) { 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) => { API.getMe().then((me) => {
if (subscribed)
GlobalState.me = me; GlobalState.me = me;
}); });
console.log("Feed from cache") console.log("Feed from cache")
let cacheFeed = await getFeed() || []; let cacheFeed = await getFeed() || [];
if(cacheFeed.length) setPosts(cacheFeed); if (cacheFeed.length && subscribed) setPosts(cacheFeed);
console.log("Feed from server") console.log("Feed from server")
let posts = await API.getPosts(); let posts = await API.getPosts();
if (subscribed) {
setPosts(posts); setPosts(posts);
storeFeed(posts); storeFeed(posts);
}
console.log("Feed, end useEffect") console.log("Feed, end useEffect")
return () => {
subscribed = false;
}
}, [route.params]); }, [route.params]);
const renderPost = (({ item }) => { const renderPost = (({ item }) => {
if (item.nonOrganicType === 'PopularUsers' || item.nonOrganicType === 'PopularGroups') if (item.nonOrganicType === 'PopularUsers' || item.nonOrganicType === 'PopularGroups')

View File

@@ -5,6 +5,7 @@ import API from './../API.js';
import Post from './../components/Post.js'; import Post from './../components/Post.js';
import NewPost from "./../components/NewPost.js"; import NewPost from "./../components/NewPost.js";
import ProfileHeader from '../components/ProfileHeader.js'; import ProfileHeader from '../components/ProfileHeader.js';
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeProfilePosts = async (profileid, value) => { const storeProfilePosts = async (profileid, value) => {
try { try {
@@ -18,10 +19,12 @@ const getProfilePosts = async (profileid) => {
try { try {
const value = await AsyncStorage.getItem('profile_' + profileid) const value = await AsyncStorage.getItem('profile_' + profileid)
if (value !== null) { if (value !== null) {
console.log(JSON.parse(value))
return JSON.parse(value); return JSON.parse(value);
} }
return []; return [];
} catch (e) { } catch (e) {
console.log('fail getProfilePosts', e)
return []; return [];
} }
} }
@@ -31,17 +34,20 @@ let Profile = ({ navigation, route }) => {
let [profile, setProfile] = useState({}); let [profile, setProfile] = useState({});
useEffect(async () => { useEffect(async () => {
let subscribed = true;
setPosts([]); setPosts([]);
if (route.params && route.params.profileid) { if (route.params && route.params.profileid) {
console.log('Loading Cache Profile:' + route.params.profileid); console.log('Loading Cache Profile:' + route.params.profileid);
getProfilePosts(route.params.profileid).then(setPosts); await API.getUserProfile(route.params.profileid).then((profileObj) => {
console.log('Loaded Cache Profile:' + route.params.profileid); if(!subscribed) return 0;
API.getUserProfile(route.params.profileid).then((profileObj) => {
let profile = profileObj.profile let profile = profileObj.profile
setProfile(profileObj); setProfile(profileObj);
navigation.setOptions({ title: profile.firstName + " " + profile.lastName }); 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) => { API.getPosts(route.params.profileid).then((data) => {
if(!subscribed) return 0;
setPosts(data); setPosts(data);
storeProfilePosts(route.params.profileid, data); storeProfilePosts(route.params.profileid, data);
console.log('Store Cache Profile:' + route.params.profileid); console.log('Store Cache Profile:' + route.params.profileid);
@@ -49,6 +55,9 @@ let Profile = ({ navigation, route }) => {
} else { } else {
navigation.navigate('Feed') navigation.navigate('Feed')
} }
return ()=>{
subscribed = false;
}
}, [route.params]); }, [route.params]);
const renderPost = (({ item }) => { const renderPost = (({ item }) => {
if (item.nonOrganicType) if (item.nonOrganicType)