import React from "react";
import { Link, withRouter } from 'react-router-dom'
import Modal from './modal'
import * as api from '../../api'
import numberFormatter from '../../number-formatter'
import Bar from '../bar'
import {parseQuery} from '../../query'
class CountriesModal extends React.Component {
constructor(props) {
super(props)
this.state = {
loading: true,
query: parseQuery(props.location.search, props.site)
}
}
componentDidMount() {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/countries`, this.state.query, {limit: 100})
.then((res) => this.setState({loading: false, countries: res}))
}
renderCountry(country) {
const query = new URLSearchParams(window.location.search)
query.set('country', country.name)
return (
{country.full_country_name}
|
{numberFormatter(country.count)} ({country.percentage}%)
|
)
}
label() {
return this.state.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
}
renderBody() {
if (this.state.loading) {
return (
)
} else if (this.state.countries) {
return (
Top countries
Country |
{this.label()} |
{ this.state.countries.map(this.renderCountry.bind(this)) }
)
}
}
render() {
return (
{ this.renderBody() }
)
}
}
export default withRouter(CountriesModal)