2016-11-23 18:40:35 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-03 08:54:22 +00:00
|
|
|
import { h, Component } from 'preact';
|
2016-12-08 11:27:19 +00:00
|
|
|
import Client from '../lib/client.js';
|
2018-05-01 14:06:17 +00:00
|
|
|
import { bind } from 'decko';
|
2016-11-23 18:40:35 +00:00
|
|
|
|
|
|
|
class Realtime extends Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
count: 0
|
|
|
|
}
|
2018-05-01 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.fetchData();
|
2018-05-03 08:54:22 +00:00
|
|
|
this.interval = window.setInterval(this.fetchData, 15000);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
clearInterval(this.interval);
|
2016-11-23 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 09:37:12 +00:00
|
|
|
@bind
|
|
|
|
setDocumentTitle() {
|
|
|
|
// update document title
|
|
|
|
let visitorText = this.state.count == 1 ? 'visitor' : 'visitors';
|
|
|
|
document.title = ( this.state.count > 0 ? `${this.state.count} current ${visitorText} — Fathom` : 'Fathom' );
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:06:17 +00:00
|
|
|
@bind
|
2016-11-23 18:40:35 +00:00
|
|
|
fetchData() {
|
2018-05-07 14:05:53 +00:00
|
|
|
Client.request(`stats/site/realtime`)
|
2018-05-16 15:02:46 +00:00
|
|
|
.then((d) => {
|
|
|
|
this.setState({ count: d })
|
2018-05-21 09:37:12 +00:00
|
|
|
this.setDocumentTitle();
|
2018-05-16 15:02:46 +00:00
|
|
|
})
|
2018-05-08 11:20:09 +00:00
|
|
|
.catch((e) => {
|
2018-05-15 12:32:30 +00:00
|
|
|
if(e.message == 401) {
|
2018-05-08 11:20:09 +00:00
|
|
|
this.props.onError();
|
|
|
|
}
|
|
|
|
})
|
2016-11-23 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 08:54:22 +00:00
|
|
|
render(props, state) {
|
|
|
|
let visitorText = state.count == 1 ? 'visitor' : 'visitors';
|
2016-11-23 18:40:35 +00:00
|
|
|
return (
|
2018-05-03 08:54:22 +00:00
|
|
|
<span><span class="count">{state.count}</span> <span>current {visitorText}</span></span>
|
2016-11-23 18:40:35 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Realtime
|