import React from 'react'; const THIRTY_SECONDS = 30000 export default class CurrentVisitors extends React.Component { constructor(props) { super(props) this.state = {currentVisitors: null} } componentDidMount() { this.updateCount().then(() => { this.intervalId = setInterval(this.updateCount.bind(this), THIRTY_SECONDS) }) } componentWillUnMount() { clearInverval(this.intervalId) } updateCount() { return fetch(`/api/stats/${encodeURIComponent(this.props.site.domain)}/current-visitors`) .then(res => res.json()) .then((res) => this.setState({currentVisitors: res})) } render() { if (this.state.currentVisitors !== null) { return (
{this.state.currentVisitors} current visitors
) } else { return null } } }