loading posts from storage on startup

This commit is contained in:
Nouman Tahir 2021-04-08 20:41:31 +05:00
parent aa3fce6877
commit bca8bd8d7f

View File

@ -2,8 +2,10 @@ import React, {useState, useEffect} from 'react';
import PostsList from '../../postsList';
import { getPromotedPosts, loadPosts } from '../services/tabbedPostsFetch';
import { LoadPostsOptions, TabContentProps, TabMeta } from '../services/tabbedPostsModels';
import {useSelector } from 'react-redux';
import {useSelector, useDispatch } from 'react-redux';
import TabEmptyView from './listEmptyView';
import { filter } from 'core-js/core/array';
import { setInitPosts } from '../../../redux/actions/postsAction';
const TabContent = ({
@ -16,12 +18,22 @@ const TabContent = ({
}: TabContentProps) => {
//redux properties
const dispatch = useDispatch();
const isLoggedIn = useSelector((state) => state.application.isLoggedIn);
const isAnalytics = useSelector((state) => state.application.isAnalytics);
const nsfw = useSelector((state) => state.application.nsfw);
const isConnected = useSelector((state) => state.application.isConnected);
const username = useSelector((state) => state.account.currentAccount.name);
const initPosts = useSelector((state) => {
if(isFeedScreen){
if(username && filterKey === 'friends'){
return state.posts.initPosts
}else if (!username && filterKey === 'hot'){
return state.posts.initPosts
}
}
return []
});
//state
const [posts, setPosts] = useState([]);
@ -33,19 +45,23 @@ const TabContent = ({
//side effects
useEffect(() => {
_initContent();
_initContent(initPosts);
}, [])
useEffect(()=>{
if(isConnected && (username !== sessionUser || forceLoadPosts)){
_initContent();
if(filterKey !== 'friends'){
_initContent();
}else{
setPosts([])
}
}
},[username, forceLoadPosts])
//actions
const _initContent = () => {
setPosts([]);
const _initContent = (_initPosts:any[] = []) => {
setPosts(_initPosts);
setTabMeta({
startAuthor:'',
startPermlink:'',
@ -79,6 +95,14 @@ const TabContent = ({
const updatedPosts = await loadPosts(options)
if(updatedPosts && Array.isArray(updatedPosts)){
if (isFeedScreen && shouldReset) {
// //schedule refetch of new posts by checking time of current post
// _scheduleLatestPostsCheck(nextPosts[0]);
if (filterKey == username ? 'friends' : 'hot') {
dispatch(setInitPosts(updatedPosts));
}
}
setPosts(updatedPosts);
}else{
console.warn("Wrong data returned", updatedPosts)