BE check for subscription on useEffect
This commit is contained in:
@@ -33,24 +33,31 @@ let Feed = ({ navigation, route }) => {
|
|||||||
console.log("Render Feed");
|
console.log("Render Feed");
|
||||||
useEffect(async () => {
|
useEffect(async () => {
|
||||||
let loggedIn = await API.isLoggedIn();
|
let loggedIn = await API.isLoggedIn();
|
||||||
if(!loggedIn) return navigation.reset({
|
if (!loggedIn) return navigation.reset({
|
||||||
index: 0,
|
index: 0,
|
||||||
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) => {
|
||||||
GlobalState.me = me;
|
if (subscribed)
|
||||||
|
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();
|
||||||
setPosts(posts);
|
if (subscribed) {
|
||||||
storeFeed(posts);
|
setPosts(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')
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user