diff --git a/API.js b/API.js index aa15a75..4bd087c 100644 --- a/API.js +++ b/API.js @@ -141,6 +141,9 @@ const API = { getVideo(videoId) { return getCall("/post/video/" + videoId); }, + registerToken(token) { + return postCall("/token/", {token}); + }, deletePost(postid){ return deleteCall("/post/" + postid); }, diff --git a/Views/Feed.js b/Views/Feed.js index ba042fe..b0c0816 100644 --- a/Views/Feed.js +++ b/Views/Feed.js @@ -31,30 +31,34 @@ const getFeed = async () => { let Feed = ({ navigation, route }) => { let [Posts, setPosts] = useState([]); console.log("Render Feed"); - useEffect(async () => { - let loggedIn = await API.isLoggedIn(); - if (!loggedIn) return navigation.reset({ - index: 0, - routes: [{ name: 'Login' }], - }); - if (route.params && route.params.profileid) { - return navigation.navigate('Profile', { profileid: route.params.profileid }) - } + useEffect(() => { let subscribed = true; - API.getMe().then((me) => { - if (subscribed) - GlobalState.me = me; - }); - console.log("Feed from cache") - let cacheFeed = await getFeed() || []; - if (cacheFeed.length && subscribed) setPosts(cacheFeed); - console.log("Feed from server") - let posts = await API.getPosts(); - if (subscribed) { - setPosts(posts); - storeFeed(posts); + const getData = async () => { + let loggedIn = await API.isLoggedIn(); + if (!loggedIn) return navigation.reset({ + index: 0, + routes: [{ name: 'Login' }], + }); + if (route.params && route.params.profileid) { + return navigation.navigate('Profile', { profileid: route.params.profileid }) + } + API.getMe().then((me) => { + if (subscribed) + GlobalState.me = me; + }); + console.log("Feed from cache") + let cacheFeed = await getFeed() || []; + 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") + } - console.log("Feed, end useEffect") + getData() return () => { subscribed = false; } diff --git a/Views/Profile.js b/Views/Profile.js index b4b7d5a..f48af8e 100644 --- a/Views/Profile.js +++ b/Views/Profile.js @@ -33,28 +33,31 @@ let Profile = ({ navigation, route }) => { let [Posts, setPosts] = useState([]); let [profile, setProfile] = useState({}); - useEffect(async () => { + useEffect(() => { let subscribed = true; - setPosts([]); - if (route.params && route.params.profileid) { - console.log('Loading Cache Profile:' + route.params.profileid); - 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); - }); - } else { - navigation.navigate('Feed') + const getData = async () => { + setPosts([]); + if (route.params && route.params.profileid) { + console.log('Loading Cache Profile:' + route.params.profileid); + 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); + }); + } else { + navigation.navigate('Feed') + } } + getData(); return ()=>{ subscribed = false; } diff --git a/components/Media.js b/components/Media.js index 4aef258..70f811f 100644 --- a/components/Media.js +++ b/components/Media.js @@ -44,13 +44,20 @@ let Media = (props) => { const [videosFiles, setVideosFiles] = useState([]); const [poster, setPoster] = useState(''); const [loaded, setLoaded] = useState(false); - useEffect(async () => { - if (!videosId[1]) return 0; - let videoObj = await API.getVideo(videosId[1]); - if(videoObj && videoObj.files){ - setVideosFiles(videoObj.files); - setPoster(videoObj.pictures.sizes[4].link); - } + useEffect(() => { + let subscribed = true; + let getData = async () => { + if (!videosId[1]) return 0; + let videoObj = await API.getVideo(videosId[1]); + if(videoObj && videoObj.files && subscribed){ + setVideosFiles(videoObj.files); + setPoster(videoObj.pictures.sizes[4].link); + } + }; + getData(); + return ()=>{ + subscribed = false; + }; }, [props.content]) const video = videosFiles.length ? ( loaded ? : diff --git a/components/Post.js b/components/Post.js index 01ba2fc..b84e92b 100644 --- a/components/Post.js +++ b/components/Post.js @@ -58,7 +58,7 @@ let Post = (props) => { return ( - + {!post.nonOrganicType ? diff --git a/components/UserName.js b/components/UserName.js index 74ee0d0..b9aef30 100644 --- a/components/UserName.js +++ b/components/UserName.js @@ -28,12 +28,20 @@ let UserName = ({ profileid, hideIcon }) => { let [profile, setProfile] = useState({}); const navigation = useNavigation(); - useEffect(async () => { - let cacheProfile = await getName(profileid); - if (cacheProfile && cacheProfile.profile) setProfile(cacheProfile); - let p = await API.getUserProfile(profileid).catch(() => { return {} }); - setProfile(p); - storeName(profileid, p) + useEffect(() => { + let subscribed = true; + let getData = async () => { + let cacheProfile = await getName(profileid); + if (cacheProfile && cacheProfile.profile && subscribed) setProfile(cacheProfile); + let p = await API.getUserProfile(profileid).catch(() => { return {} }); + if (subscribed) + setProfile(p); + storeName(profileid, p) + } + getData(); + return () => { + subscribed = false; + }; }, [profileid]); let icon = profile._id ? (!profile.isGroup ? "person-outline" : "group") : '';