2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
2020-02-10 16:17:00 +03:00
|
|
|
import Datamap from 'datamaps'
|
2020-10-13 12:03:42 +03:00
|
|
|
import { withRouter } from 'react-router-dom'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-10-13 10:41:49 +03:00
|
|
|
import numberFormatter from '../number-formatter'
|
2020-03-03 12:13:08 +03:00
|
|
|
import FadeIn from '../fade-in'
|
2021-03-25 12:55:15 +03:00
|
|
|
import LazyLoader from '../lazy-loader'
|
2019-11-19 07:30:42 +03:00
|
|
|
import Bar from './bar'
|
|
|
|
import MoreLink from './more-link'
|
|
|
|
import * as api from '../api'
|
2020-11-11 13:58:05 +03:00
|
|
|
import { navigateToQuery } from '../query'
|
2020-12-16 12:57:28 +03:00
|
|
|
import { withThemeConsumer } from '../theme-consumer-hoc';
|
2021-03-25 12:55:15 +03:00
|
|
|
|
2020-10-13 12:03:42 +03:00
|
|
|
class Countries extends React.Component {
|
2019-11-19 07:30:42 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2020-02-10 16:17:00 +03:00
|
|
|
this.resizeMap = this.resizeMap.bind(this)
|
2020-12-16 12:57:28 +03:00
|
|
|
this.drawMap = this.drawMap.bind(this)
|
|
|
|
this.getDataset = this.getDataset.bind(this)
|
2020-07-14 16:52:26 +03:00
|
|
|
this.state = {loading: true}
|
2021-03-25 12:55:15 +03:00
|
|
|
this.onVisible = this.onVisible.bind(this)
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:15 +03:00
|
|
|
onVisible() {
|
2020-07-14 16:52:26 +03:00
|
|
|
this.fetchCountries().then(this.drawMap.bind(this))
|
2020-02-10 16:17:00 +03:00
|
|
|
window.addEventListener('resize', this.resizeMap);
|
2020-07-14 16:52:26 +03:00
|
|
|
if (this.props.timer) this.props.timer.onTick(this.updateCountries.bind(this))
|
2020-02-10 16:17:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.resizeMap);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.query !== prevProps.query) {
|
|
|
|
this.setState({loading: true, countries: null})
|
2020-07-16 14:49:18 +03:00
|
|
|
this.fetchCountries().then(this.drawMap.bind(this))
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2020-12-16 12:57:28 +03:00
|
|
|
|
|
|
|
if (this.props.darkTheme !== prevProps.darkTheme) {
|
|
|
|
if (document.getElementById('map-container')) {
|
|
|
|
document.getElementById('map-container').removeChild(document.querySelector('.datamaps-hoverover'));
|
|
|
|
document.getElementById('map-container').removeChild(document.querySelector('.datamap'));
|
|
|
|
}
|
|
|
|
this.drawMap();
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
getDataset() {
|
2020-02-10 16:17:00 +03:00
|
|
|
var dataset = {};
|
|
|
|
|
|
|
|
var onlyValues = this.state.countries.map(function(obj){ return obj.count });
|
2020-10-19 13:46:08 +03:00
|
|
|
var maxValue = Math.max.apply(null, onlyValues);
|
2020-02-10 16:17:00 +03:00
|
|
|
|
|
|
|
var paletteScale = d3.scale.linear()
|
2020-10-19 13:46:08 +03:00
|
|
|
.domain([0,maxValue])
|
2020-12-16 12:57:28 +03:00
|
|
|
.range([
|
|
|
|
this.props.darkTheme ? "#2e3954" : "#f3ebff",
|
|
|
|
this.props.darkTheme ? "#6366f1" : "#a779e9"
|
|
|
|
]);
|
2020-02-10 16:17:00 +03:00
|
|
|
|
|
|
|
this.state.countries.forEach(function(item){
|
|
|
|
dataset[item.name] = {numberOfThings: item.count, fillColor: paletteScale(item.count)};
|
|
|
|
});
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
return dataset
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCountries() {
|
|
|
|
this.fetchCountries().then(() => {
|
|
|
|
this.map.updateChoropleth(this.getDataset(), {reset: true})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchCountries() {
|
|
|
|
return api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/countries`, this.props.query)
|
|
|
|
.then((res) => this.setState({loading: false, countries: res}))
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeMap() {
|
|
|
|
this.map && this.map.resize()
|
|
|
|
}
|
|
|
|
|
|
|
|
drawMap() {
|
|
|
|
var dataset = this.getDataset();
|
2020-08-18 14:14:56 +03:00
|
|
|
const label = this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
|
2020-12-16 12:57:28 +03:00
|
|
|
const defaultFill = this.props.darkTheme ? '#2d3747' : '#f8fafc'
|
|
|
|
const highlightFill = this.props.darkTheme ? '#374151' : '#F5F5F5'
|
|
|
|
const borderColor = this.props.darkTheme ? '#1f2937' : '#dae1e7'
|
|
|
|
const highlightBorderColor = this.props.darkTheme ? '#4f46e5' : '#a779e9'
|
2020-07-14 16:52:26 +03:00
|
|
|
|
2020-02-10 16:17:00 +03:00
|
|
|
this.map = new Datamap({
|
|
|
|
element: document.getElementById('map-container'),
|
|
|
|
responsive: true,
|
|
|
|
projection: 'mercator',
|
2020-12-16 12:57:28 +03:00
|
|
|
fills: { defaultFill },
|
2020-02-10 16:17:00 +03:00
|
|
|
data: dataset,
|
|
|
|
geographyConfig: {
|
2020-12-16 12:57:28 +03:00
|
|
|
borderColor,
|
2020-02-10 16:17:00 +03:00
|
|
|
highlightBorderWidth: 2,
|
|
|
|
highlightFillColor: function(geo) {
|
2020-12-16 12:57:28 +03:00
|
|
|
return geo['fillColor'] || highlightFill;
|
2020-02-10 16:17:00 +03:00
|
|
|
},
|
2020-12-16 12:57:28 +03:00
|
|
|
highlightBorderColor,
|
2020-02-10 16:17:00 +03:00
|
|
|
popupTemplate: function(geo, data) {
|
|
|
|
if (!data) { return ; }
|
2020-07-30 13:44:43 +03:00
|
|
|
const pluralizedLabel = data.numberOfThings === 1 ? label.slice(0, -1) : label
|
2020-12-16 12:57:28 +03:00
|
|
|
return ['<div class="hoverinfo dark:bg-gray-800 dark:shadow-gray-850 dark:border-gray-850 dark:text-gray-200">',
|
2020-02-10 16:17:00 +03:00
|
|
|
'<strong>', geo.properties.name, '</strong>',
|
2020-12-16 12:57:28 +03:00
|
|
|
'<br><strong class="dark:text-indigo-400">', numberFormatter(data.numberOfThings), '</strong> ' + pluralizedLabel,
|
2020-02-10 16:17:00 +03:00
|
|
|
'</div>'].join('');
|
|
|
|
}
|
2020-10-13 12:03:42 +03:00
|
|
|
},
|
|
|
|
done: (datamap) => {
|
|
|
|
datamap.svg.selectAll('.datamaps-subunit').on('click', (geography) => {
|
2020-11-11 13:38:49 +03:00
|
|
|
navigateToQuery(
|
|
|
|
this.props.history,
|
|
|
|
this.props.query,
|
|
|
|
{
|
|
|
|
country: geography.id
|
|
|
|
}
|
|
|
|
)
|
2020-10-13 12:03:42 +03:00
|
|
|
})
|
2020-02-10 16:17:00 +03:00
|
|
|
}
|
|
|
|
});
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 12:21:07 +03:00
|
|
|
geolocationDbNotice() {
|
|
|
|
if (this.props.site.selfhosted) {
|
|
|
|
return (
|
|
|
|
<span className="text-xs text-gray-500 absolute bottom-4 right-3">IP Geolocation by <a target="_blank" href="https://db-ip.com" className="text-indigo-600">DB-IP</a></span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 12:13:08 +03:00
|
|
|
renderBody() {
|
|
|
|
if (this.state.countries) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-03-03 12:13:08 +03:00
|
|
|
<React.Fragment>
|
2020-12-16 12:57:28 +03:00
|
|
|
<h3 className="font-bold dark:text-gray-100">Countries</h3>
|
2021-04-12 12:21:07 +03:00
|
|
|
<div className="mx-auto mt-6" style={{width: '100%', maxWidth: '475px', height: '335px'}} id="map-container"></div>
|
2020-02-10 16:17:00 +03:00
|
|
|
<MoreLink site={this.props.site} list={this.state.countries} endpoint="countries" />
|
2021-04-12 12:21:07 +03:00
|
|
|
{ this.geolocationDbNotice() }
|
2020-03-03 12:13:08 +03:00
|
|
|
</React.Fragment>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 12:13:08 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2021-03-25 12:55:15 +03:00
|
|
|
<div className="relative p-4 bg-white rounded shadow-xl stats-item dark:bg-gray-825" style={{height: '436px'}}>
|
|
|
|
<LazyLoader onVisible={this.onVisible}>
|
|
|
|
{ this.state.loading && <div className="mx-auto my-32 loading"><div></div></div> }
|
|
|
|
<FadeIn show={!this.state.loading}>
|
|
|
|
{ this.renderBody() }
|
|
|
|
</FadeIn>
|
|
|
|
</LazyLoader>
|
2020-03-03 12:13:08 +03:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2020-10-13 12:03:42 +03:00
|
|
|
|
2020-12-16 12:57:28 +03:00
|
|
|
export default withRouter(withThemeConsumer(Countries))
|