diff --git a/frontend/package.json b/frontend/package.json index 290e937..98876d6 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -9,7 +9,6 @@ "react-icons": "^3.7.0", "react-redux": "^7.1.0", "react-redux-loading": "^1.0.1", - "react-router-dom": "^5.0.1", "redux": "^4.0.4", "redux-thunk": "^2.3.0", "fluence": "^0.3.9" diff --git a/frontend/src/actions/messages.js b/frontend/src/actions/messages.js index ab3fda5..a22c200 100644 --- a/frontend/src/actions/messages.js +++ b/frontend/src/actions/messages.js @@ -1,6 +1,4 @@ -import { saveMessage } from "../utils/api"; - -//importing loading bar to show when we submit a tweet +import {getMessages, saveMessage} from "../utils/api"; import { showLoading, hideLoading } from "react-redux-loading"; export const ADD_MESSAGE = "ADD_MESSAGE"; @@ -13,9 +11,31 @@ function addMessage(message) { }; } -//args: message text and the message that the newTweet is replying to, if any +export function fetchPosts(counter) { + return dispatch => { + + return getMessages().then((messages) => { + dispatch(receiveMessages(messages, counter)); + dispatch(hideLoading()); + }); + }; +} + +export function handleInitialData() { + return dispatch => { + //before retrieving info, show loading bar + dispatch(showLoading()); + + return getMessages().then((messages) => { + dispatch(receiveMessages(messages, 0)); + + //after everything has loaded, hide loading bar + dispatch(hideLoading()); + }); + }; +} + export function handleAddMessage(text, name) { - //using getState to get the current state of our store return (dispatch) => { dispatch(showLoading()); return saveMessage({ @@ -28,9 +48,10 @@ export function handleAddMessage(text, name) { } //action creator -export function receiveMessages(messages) { +export function receiveMessages(messages, counter) { return { type: RECEIVE_MESSAGES, + counter, messages }; } diff --git a/frontend/src/actions/shared.js b/frontend/src/actions/shared.js deleted file mode 100644 index 47b3503..0000000 --- a/frontend/src/actions/shared.js +++ /dev/null @@ -1,21 +0,0 @@ -import { getMessages } from "../utils/api"; - -//importing action creators -import { receiveMessages } from "./messages"; - -//importing action creators of loading bar -import { showLoading, hideLoading } from "react-redux-loading"; - -export function handleInitialData() { - return dispatch => { - //before retrieving info, show loading bar - dispatch(showLoading()); - - return getMessages().then((messages) => { - dispatch(receiveMessages(messages)); - - //after everything has loaded, hide loading bar - dispatch(hideLoading()); - }); - }; -} diff --git a/frontend/src/components/App.js b/frontend/src/components/App.js index 4de2b95..1fc1713 100644 --- a/frontend/src/components/App.js +++ b/frontend/src/components/App.js @@ -1,42 +1,30 @@ -import React, { Component, Fragment } from "react"; -import { BrowserRouter as Router, Route } from "react-router-dom"; -import { connect } from "react-redux"; -import { handleInitialData } from "../actions/shared"; +import React, {Component, Fragment} from "react"; +import {BrowserRouter as Router} from "react-router-dom"; +import {connect} from "react-redux"; import LoadingBar from "react-redux-loading"; //importing the loading bar given by react-redux-loading - import Dashboard from "./Dashboard"; import NewMessage from "./NewMessage"; class App extends Component { - componentDidMount() { - this.props.dispatch(handleInitialData()); - } - render() { - return ( - - {/* using a fragment so we don't add another element (div) to the DOM */} - - -
- {this.props.loading === true ? null : ( -
- - -
- )} -
-
-
- ); - } + render() { + return ( + + + +
+ {this.props.loading === true ? null : ( +
+ + +
+ )} +
+
+
+ ); + } } -function mapStateToProps({ }) { - return { - - }; -} - -export default connect(mapStateToProps)(App); +export default connect()(App); diff --git a/frontend/src/components/Dashboard.js b/frontend/src/components/Dashboard.js index bd6867f..b6779e0 100644 --- a/frontend/src/components/Dashboard.js +++ b/frontend/src/components/Dashboard.js @@ -2,14 +2,53 @@ import React, { Component } from "react"; import { connect } from "react-redux"; import Message from "./Message"; +import {handleInitialData, fetchPosts} from "../actions/messages"; class Dashboard extends Component { + + constructor(props) { + super(props); + this.state = {counter: 1, lastUpdateCounter: 0}; + } + + componentDidMount() { + this.pollingMessages = setInterval( + () => this.getMessages(), + 2000 + ); + this.props.dispatch(handleInitialData()); + } + + componentWillUnmount() { + clearInterval(this.pollingMessages); + } + + getMessages() { + this.props.dispatch(fetchPosts(this.state.counter + 1)); + + this.setState((state) => ({ + counter: state.counter + 1 + })); + } + + shouldComponentUpdate(nextProps) { + return nextProps.updates.counter > this.state.lastUpdateCounter + } + render() { + let updates = this.props.updates; + if (updates.counter) { + this.setState(() => ({ + lastUpdateCounter: updates.counter + })); + } return (
-

Your Timeline

+ Counter: {this.state.counter} + Last update: {this.state.lastUpdateCounter} +

Feed