import React from "react";
import { Link, withRouter } from 'react-router-dom'
import Datamap from 'datamaps'
import Modal from './modal'
import * as api from '../../api'
import numberFormatter from '../../number-formatter'
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}))
}
label() {
return this.state.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
}
renderCountry(country) {
const query = new URLSearchParams(window.location.search)
query.set('country', country.name)
const allCountries = Datamap.prototype.worldTopo.objects.world.geometries;
const thisCountry = allCountries.find((c) => c.id === country.name) || {properties: {name: country.name}};
const countryFullName = thisCountry.properties.name
return (
{countryFullName}
|
{numberFormatter(country.count)} ({country.percentage}%)
|
)
}
renderBody() {
if (this.state.loading) {
return (
)
}
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)