import React from 'react';
import { Link } from 'react-router-dom'
export default class CurrentVisitors extends React.Component {
constructor(props) {
super(props)
this.state = {currentVisitors: null}
}
componentDidMount() {
this.updateCount()
this.props.timer.onTick(this.updateCount.bind(this))
}
updateCount() {
return fetch(`/api/stats/${encodeURIComponent(this.props.site.domain)}/current-visitors`)
.then( response => {
if (!response.ok) { throw response }
return response.json()
})
.then((res) => this.setState({currentVisitors: res}))
}
render() {
const { currentVisitors } = this.state;
if (currentVisitors !== null) {
return (
{currentVisitors} current visitor{currentVisitors === 1 ? '' : 's'}
)
} else {
return null
}
}
}