fathom/assets/js/components/Realtime.js

34 lines
678 B
JavaScript
Raw Normal View History

2016-11-23 18:40:35 +00:00
'use strict';
import { h, render, 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
}
this.fetchData();
window.setInterval(this.fetchData, 15000);
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
}
render() {
let visitors = this.state.count == 1 ? 'visitor' : 'visitors';
return (
2018-05-01 14:06:17 +00:00
<span><span class="count">{this.state.count}</span> <span>current {visitors}</span></span>
2016-11-23 18:40:35 +00:00
)
}
}
export default Realtime