BE check for subscription on useEffect
This commit is contained in:
@@ -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) => {
|
||||
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();
|
||||
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')
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user