Fix messages fetching

This commit is contained in:
Igor Krivchun 2019-08-20 14:35:57 +07:00
parent 62d30f82b4
commit 416ecd359b
2 changed files with 12 additions and 28 deletions

View File

@ -15,10 +15,16 @@ export async function toggleConnection(devnet, appId = 413) {
return changeConnection(devnet, appId) return changeConnection(devnet, appId)
} }
export function fetchPosts(counter) { let lastResponseRequestTime = 0;
export function fetchPosts() {
return dispatch => { return dispatch => {
const currentRequestTime = Date.now();
return getMessages().then((messages) => { return getMessages().then((messages) => {
dispatch(receiveMessages(messages, counter)); if (currentRequestTime > lastResponseRequestTime) {
lastResponseRequestTime = currentRequestTime;
dispatch(receiveMessages(messages));
}
}) })
}; };
} }
@ -29,7 +35,7 @@ export function handleInitialData() {
dispatch(showLoading()); dispatch(showLoading());
return getMessages().then((messages) => { return getMessages().then((messages) => {
dispatch(receiveMessages(messages, 0)); dispatch(receiveMessages(messages));
//after everything has loaded, hide loading bar //after everything has loaded, hide loading bar
dispatch(hideLoading()); dispatch(hideLoading());
@ -50,10 +56,9 @@ export function handleAddMessage(text, name) {
} }
//action creator //action creator
export function receiveMessages(messages, counter) { export function receiveMessages(messages) {
return { return {
type: RECEIVE_MESSAGES, type: RECEIVE_MESSAGES,
counter,
messages messages
}; };
} }

View File

@ -5,12 +5,6 @@ import Message from "./Message";
import {handleInitialData, fetchPosts} from "../actions/messages"; import {handleInitialData, fetchPosts} from "../actions/messages";
class Dashboard extends Component { class Dashboard extends Component {
constructor(props) {
super(props);
this.state = {counter: 1, lastUpdateCounter: 0, firstFetch: true};
}
componentDidMount() { componentDidMount() {
this.props.dispatch(handleInitialData()); this.props.dispatch(handleInitialData());
this.pollingMessages = setInterval( this.pollingMessages = setInterval(
@ -24,30 +18,15 @@ class Dashboard extends Component {
} }
getMessages() { getMessages() {
this.props.dispatch(fetchPosts(this.state.counter + 1, this.state.firstFetch)); this.props.dispatch(fetchPosts());
this.setState((state) => ({
counter: state.counter + 1,
firstFetch: false
}));
}
shouldComponentUpdate(nextProps) {
return nextProps.updates.counter > this.state.lastUpdateCounter
} }
render() { render() {
let updates = this.props.updates;
if (updates.counter) {
this.setState(() => ({
lastUpdateCounter: updates.counter
}));
}
return ( return (
<div> <div>
<h3 className="center">Feed</h3> <h3 className="center">Feed</h3>
<ul className="dashbord-list"> <ul className="dashbord-list">
{updates.messages.map((m, i) => { {this.props.updates.messages.map((m, i) => {
return ( return (
<li key={i}> <li key={i}>
<Message message={m} /> <Message message={m} />