2019-11-19 07:30:42 +03:00
|
|
|
import React from "react";
|
2020-10-13 12:03:42 +03:00
|
|
|
import { Link, withRouter } from 'react-router-dom'
|
2021-07-23 13:44:05 +03:00
|
|
|
import Datamap from 'datamaps'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
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)
|
2020-07-14 16:52:26 +03:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
query: parseQuery(props.location.search, props.site)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-07-14 16:52:26 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/countries`, this.state.query, {limit: 100})
|
2019-11-19 07:30:42 +03:00
|
|
|
.then((res) => this.setState({loading: false, countries: res}))
|
|
|
|
}
|
|
|
|
|
2021-08-13 11:00:42 +03:00
|
|
|
label() {
|
|
|
|
return this.state.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:17:11 +03:00
|
|
|
showConversionRate() {
|
|
|
|
return !!this.state.query.filters.goal
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
renderCountry(country) {
|
2020-10-13 12:03:42 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
|
|
|
query.set('country', country.name)
|
|
|
|
|
2021-07-23 13:44:05 +03:00
|
|
|
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
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-12-16 12:57:28 +03:00
|
|
|
<tr className="text-sm dark:text-gray-200" key={country.name}>
|
2020-10-13 12:03:42 +03:00
|
|
|
<td className="p-2">
|
2021-08-13 11:00:42 +03:00
|
|
|
<Link
|
|
|
|
className="hover:underline"
|
|
|
|
to={{search: query.toString(),
|
|
|
|
pathname: `/${ encodeURIComponent(this.props.site.domain)}`}}
|
|
|
|
>
|
2021-07-23 13:44:05 +03:00
|
|
|
{countryFullName}
|
2020-10-13 12:03:42 +03:00
|
|
|
</Link>
|
|
|
|
</td>
|
2020-08-20 11:16:26 +03:00
|
|
|
<td className="p-2 w-32 font-medium" align="right">
|
2020-10-13 10:41:49 +03:00
|
|
|
{numberFormatter(country.count)} <span className="inline-block text-xs w-8 text-right">({country.percentage}%)</span>
|
2020-08-20 11:16:26 +03:00
|
|
|
</td>
|
2021-09-20 17:17:11 +03:00
|
|
|
{this.showConversionRate() && <td className="p-2 w-32 font-medium" align="right">{country.conversion_rate}%</td> }
|
2020-02-10 16:17:00 +03:00
|
|
|
</tr>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderBody() {
|
2020-03-03 17:18:02 +03:00
|
|
|
if (this.state.loading) {
|
|
|
|
return (
|
|
|
|
<div className="loading mt-32 mx-auto"><div></div></div>
|
|
|
|
)
|
2021-08-13 11:00:42 +03:00
|
|
|
}
|
2021-09-20 17:17:11 +03:00
|
|
|
|
2021-08-13 11:00:42 +03:00
|
|
|
if (this.state.countries) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2021-08-13 11:00:42 +03:00
|
|
|
<>
|
2020-12-16 12:57:28 +03:00
|
|
|
<h1 className="text-xl font-bold dark:text-gray-100">Top countries</h1>
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="my-4 border-b border-gray-300 dark:border-gray-500"></div>
|
2019-11-19 07:30:42 +03:00
|
|
|
<main className="modal__content">
|
2021-08-13 11:00:42 +03:00
|
|
|
<table className="w-max overflow-x-auto md:w-full table-striped table-fixed">
|
2020-02-10 16:17:00 +03:00
|
|
|
<thead>
|
|
|
|
<tr>
|
2021-08-13 11:00:42 +03:00
|
|
|
<th
|
|
|
|
className="p-2 w-48 lg:w-1/2 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400"
|
|
|
|
align="left"
|
|
|
|
>
|
|
|
|
Country
|
|
|
|
</th>
|
|
|
|
<th
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
className="p-2 w-32 lg:w-1/2 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400"
|
|
|
|
align="right"
|
|
|
|
>
|
|
|
|
{this.label()}
|
|
|
|
</th>
|
2021-09-20 17:17:11 +03:00
|
|
|
{this.showConversionRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500 dark:text-gray-400" align="right">CR</th>}
|
2020-02-10 16:17:00 +03:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{ this.state.countries.map(this.renderCountry.bind(this)) }
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2019-11-19 07:30:42 +03:00
|
|
|
</main>
|
2021-08-13 11:00:42 +03:00
|
|
|
</>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-03-03 12:13:08 +03:00
|
|
|
<Modal site={this.props.site} show={!this.state.loading}>
|
2019-11-19 07:30:42 +03:00
|
|
|
{ this.renderBody() }
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(CountriesModal)
|