mirror of
https://github.com/fluencelabs/fluid
synced 2025-04-24 14:22:18 +00:00
Fix messages fetching
This commit is contained in:
parent
62d30f82b4
commit
416ecd359b
@ -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
|
||||
};
|
||||
}
|
||||
|
@ -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} />
|
||||
|
Loading…
x
Reference in New Issue
Block a user