2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
2020-07-14 16:52:26 +03:00
|
|
|
import { Link } from 'react-router-dom'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
export default class CurrentVisitors extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {currentVisitors: null}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-07-14 16:52:26 +03:00
|
|
|
this.updateCount()
|
|
|
|
this.props.timer.onTick(this.updateCount.bind(this))
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateCount() {
|
2020-02-04 16:44:13 +03:00
|
|
|
return fetch(`/api/stats/${encodeURIComponent(this.props.site.domain)}/current-visitors`)
|
2020-05-26 13:40:49 +03:00
|
|
|
.then( response => {
|
|
|
|
if (!response.ok) { throw response }
|
|
|
|
return response.json()
|
|
|
|
})
|
2019-11-19 07:30:42 +03:00
|
|
|
.then((res) => this.setState({currentVisitors: res}))
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-07-29 10:07:18 +03:00
|
|
|
const { currentVisitors } = this.state;
|
|
|
|
if (currentVisitors !== null) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-07-14 16:52:26 +03:00
|
|
|
<Link to={`/${encodeURIComponent(this.props.site.domain)}?period=realtime`} className="block text-sm font-bold text-gray-500 mt-1">
|
2020-03-06 12:11:38 +03:00
|
|
|
<svg className="w-2 mr-2 fill-current text-green-500 inline" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
|
2019-11-19 07:30:42 +03:00
|
|
|
<circle cx="8" cy="8" r="8"/>
|
|
|
|
</svg>
|
2020-07-29 10:07:18 +03:00
|
|
|
{currentVisitors} current visitor{currentVisitors === 1 ? '' : 's'}
|
2020-07-14 16:52:26 +03:00
|
|
|
</Link>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|