Merge pull request #3 from krivchun/master

Fix messages fetching
This commit is contained in:
Dima 2019-08-20 11:26:49 +03:00 committed by GitHub
commit cd7a0ba9cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
}
export function fetchPosts(counter) {
let lastResponseRequestTime = 0;
export function fetchPosts() {
return dispatch => {
const currentRequestTime = Date.now();
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());
return getMessages().then((messages) => {
dispatch(receiveMessages(messages, 0));
dispatch(receiveMessages(messages));
//after everything has loaded, hide loading bar
dispatch(hideLoading());
@ -50,10 +56,9 @@ export function handleAddMessage(text, name) {
}
//action creator
export function receiveMessages(messages, counter) {
export function receiveMessages(messages) {
return {
type: RECEIVE_MESSAGES,
counter,
messages
};
}

View File

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