modify realtime component to work properly from componentWillReceiveProps

This commit is contained in:
Danny van Kooten 2018-10-09 17:05:49 +02:00
parent 6f97341e42
commit 901e09bcac
1 changed files with 11 additions and 7 deletions

View File

@ -17,11 +17,11 @@ class Realtime extends Component {
componentDidMount() { componentDidMount() {
this.fetchData(); this.fetchData();
this.interval = window.setInterval(this.fetchData, 15000); this.interval = window.setInterval(this.handleIntervalEvent, 15000);
} }
componentWillUnmount() { componentWillUnmount() {
clearInterval(this.interval); window.clearInterval(this.interval);
} }
componentWillReceiveProps(newProps, newState) { componentWillReceiveProps(newProps, newState) {
@ -29,12 +29,11 @@ class Realtime extends Component {
return; return;
} }
this.fetchData() this.fetchData(newProps.siteId)
} }
paramsChanged(o, n) { paramsChanged(o, n) {
return o.siteId != n.siteId;
return o.siteId != n.siteId || o.before != n.before || o.after != n.after;
} }
@bind @bind
@ -44,9 +43,14 @@ class Realtime extends Component {
document.title = ( this.state.count > 0 ? `${numbers.formatPretty(this.state.count)} current ${visitorText} — Fathom` : 'Fathom' ); document.title = ( this.state.count > 0 ? `${numbers.formatPretty(this.state.count)} current ${visitorText} — Fathom` : 'Fathom' );
} }
@bind
handleIntervalEvent() {
this.fetchData(this.props.siteId)
}
@bind @bind
fetchData() { fetchData(siteId) {
let url = `/sites/${this.props.siteId}/stats/site/realtime` let url = `/sites/${siteId}/stats/site/realtime`
Client.request(url) Client.request(url)
.then((d) => { .then((d) => {
this.setState({ count: d }) this.setState({ count: d })