2019-11-19 07:30:42 +03:00
|
|
|
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() {
|
2020-02-04 16:44:13 +03:00
|
|
|
return fetch(`/api/stats/${encodeURIComponent(this.props.site.domain)}/current-visitors`)
|
2019-11-19 07:30:42 +03:00
|
|
|
.then(res => res.json())
|
|
|
|
.then((res) => this.setState({currentVisitors: res}))
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.currentVisitors !== null) {
|
|
|
|
return (
|
2020-02-10 16:17:00 +03:00
|
|
|
<div className="text-sm font-bold text-grey-darker mt-1">
|
2019-11-19 07:30:42 +03:00
|
|
|
<svg className="w-2 mr-1 fill-current text-green" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
|
|
|
|
<circle cx="8" cy="8" r="8"/>
|
|
|
|
</svg>
|
2020-02-10 16:17:00 +03:00
|
|
|
{this.state.currentVisitors} current visitors
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|