fathom/assets/js/components/Realtime.js

41 lines
795 B
JavaScript
Raw Normal View History

2016-11-23 18:40:35 +00:00
'use strict';
2018-05-03 08:54:22 +00:00
import { h, Component } from 'preact';
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
}
}
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-01 14:06:17 +00:00
@bind
2016-11-23 18:40:35 +00:00
fetchData() {
Client.request(`visitors/count/realtime`)
.then((d) => { this.setState({ count: d })})
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