import React from 'react'; import Bar from './bar' import MoreLink from './more-link' import * as api from '../api' export default class Countries extends React.Component { constructor(props) { super(props) this.state = { loading: true } } componentDidMount() { this.fetchCountries() } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, countries: null}) this.fetchCountries() } } fetchCountries() { api.get(`/api/stats/${this.props.site.domain}/countries`, this.props.query) .then((res) => this.setState({loading: false, countries: res})) } renderCountry(country) { return (
{country.name} {country.percentage}%
) } render() { if (this.state.loading) { return (
) } else if (this.state.countries) { return (

Top Countries

by visitors
{ this.state.countries.map(this.renderCountry.bind(this)) }
) } } }