mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
46 lines
907 B
JavaScript
46 lines
907 B
JavaScript
'use strict';
|
|
|
|
import { h, Component } from 'preact';
|
|
import Client from '../lib/client.js';
|
|
import { bind } from 'decko';
|
|
|
|
class Realtime extends Component {
|
|
|
|
constructor(props) {
|
|
super(props)
|
|
|
|
this.state = {
|
|
count: 0
|
|
}
|
|
}
|
|
|
|
componentDidMount() {
|
|
this.fetchData();
|
|
this.interval = window.setInterval(this.fetchData, 15000);
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
clearInterval(this.interval);
|
|
}
|
|
|
|
@bind
|
|
fetchData() {
|
|
Client.request(`stats/site/realtime`)
|
|
.then((d) => { this.setState({ count: d })})
|
|
.catch((e) => {
|
|
if(e.message === "Unauthorized") {
|
|
this.props.onError();
|
|
}
|
|
})
|
|
}
|
|
|
|
render(props, state) {
|
|
let visitorText = state.count == 1 ? 'visitor' : 'visitors';
|
|
return (
|
|
<span><span class="count">{state.count}</span> <span>current {visitorText}</span></span>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default Realtime
|