fathom/assets/js/components/Realtime.js

40 lines
842 B
JavaScript
Raw Normal View History

2016-11-23 18:40:35 +00:00
'use strict';
import { h, render, Component } from 'preact';
class Realtime extends Component {
constructor(props) {
super(props)
this.state = {
count: 0
}
this.fetchData = this.fetchData.bind(this);
this.fetchData();
2016-11-25 15:39:51 +00:00
window.setInterval(this.fetchData, 6000);
2016-11-23 18:40:35 +00:00
}
fetchData() {
return fetch('/api/visits/count/realtime', {
credentials: 'include'
}).then((r) => {
2016-11-25 15:39:51 +00:00
if( r.ok ) { return r.json(); }
throw new Error();
}).then((data) => {
2016-11-23 18:40:35 +00:00
this.setState({ count: data })
2016-11-25 15:39:51 +00:00
});
2016-11-23 18:40:35 +00:00
}
render() {
let visitors = this.state.count == 1 ? 'visitor' : 'visitors';
return (
<div class="block block-float">
2016-11-24 13:37:41 +00:00
<span class="count">{this.state.count}</span> <span>{visitors} on the site right now.</span>
2016-11-23 18:40:35 +00:00
</div>
)
}
}
export default Realtime