From effe56b3e4d61cdad2242f9779ccf50d26e1061b Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 1 Dec 2021 15:31:50 +0200 Subject: [PATCH] Use new iso_codes package --- assets/js/dashboard/router.js | 12 ++++++++-- assets/js/dashboard/stats/locations/index.js | 10 ++++++++ assets/js/dashboard/stats/modals/table.js | 11 ++++----- .../controllers/api/stats_controller.ex | 23 ++++++++++++------- mix.exs | 1 + mix.lock | 1 + .../api/stats_controller/countries_test.exs | 4 ++++ 7 files changed, 45 insertions(+), 17 deletions(-) diff --git a/assets/js/dashboard/router.js b/assets/js/dashboard/router.js index 216c2d4c8..0e7ec77ef 100644 --- a/assets/js/dashboard/router.js +++ b/assets/js/dashboard/router.js @@ -49,10 +49,10 @@ export default function Router({site, loggedIn, currentUserRole}) { - + site.cities && renderCountryIcon(row)} /> - + @@ -65,3 +65,11 @@ export default function Router({site, loggedIn, currentUserRole}) { ); } + +function renderCountryIcon(country) { + return {country.flag} +} + +function renderRegionIcon(region) { + return {region.country_flag} +} diff --git a/assets/js/dashboard/stats/locations/index.js b/assets/js/dashboard/stats/locations/index.js index 762d9de08..6a2c1a689 100644 --- a/assets/js/dashboard/stats/locations/index.js +++ b/assets/js/dashboard/stats/locations/index.js @@ -14,6 +14,10 @@ function Countries({query, site}) { }) } + function renderIcon(country) { + return site.cities && {country.flag} + } + return ( ) @@ -31,6 +36,10 @@ function Regions({query, site}) { return api.get(apiPath(site, '/regions'), query, {country_name: query.filters.country, limit: 9}) } + function renderIcon(region) { + return {region.country_flag} + } + return ( ) diff --git a/assets/js/dashboard/stats/modals/table.js b/assets/js/dashboard/stats/modals/table.js index 147acd603..7f5c4feee 100644 --- a/assets/js/dashboard/stats/modals/table.js +++ b/assets/js/dashboard/stats/modals/table.js @@ -1,6 +1,5 @@ import React from "react"; import { Link, withRouter } from 'react-router-dom' -import Datamap from 'datamaps' import Modal from './modal' import * as api from '../../api' @@ -32,20 +31,18 @@ class ModalTable extends React.Component { query.set(key, tableItem[valueKey]) })) - const allCountries = Datamap.prototype.worldTopo.objects.world.geometries; - const thisCountry = allCountries.find((c) => c.id === tableItem.name) || {properties: {name: tableItem.name}}; - const countryFullName = thisCountry.properties.name - return ( - {countryFullName} + {this.props.renderIcon && this.props.renderIcon(tableItem)} + {this.props.renderIcon && ' '} + {tableItem.name} {numberFormatter(tableItem.visitors)} - {tableItem.percentage && + {tableItem.percentage >= 0 && ({tableItem.percentage}%) } diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 568054441..ad42b284f 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -494,8 +494,8 @@ defmodule PlausibleWeb.Api.StatsController do countries = countries |> Enum.map(fn country -> - iso3166 = Stats.CountryName.from_iso3166(country["code"]) - Map.put(country, "name", iso3166) + country_iso_info = ISOCodes.ISO3166_1.get(country["code"]) + Map.put(country, "name", country_iso_info.name) end) if Map.has_key?(query.filters, "event:goal") do @@ -507,10 +507,14 @@ defmodule PlausibleWeb.Api.StatsController do end else countries = - Enum.map(countries, fn country -> - name = Stats.CountryName.from_iso3166(country["code"]) - code = Stats.CountryName.to_alpha3(country["code"]) - Map.merge(country, %{"name" => name, "code" => code}) + Enum.map(countries, fn row -> + country = ISOCodes.ISO3166_1.get(row["code"]) + + Map.merge(row, %{ + "name" => country.name, + "flag" => country.flag, + "code" => country.alpha_3 + }) end) json(conn, countries) @@ -526,8 +530,11 @@ defmodule PlausibleWeb.Api.StatsController do Stats.breakdown(site, query, "visit:region", ["visitors"], pagination) |> transform_keys(%{"region" => "code"}) |> Enum.map(fn region -> - name = Stats.CountryName.from_iso3166_2(region["code"]) - Map.put(region, "name", name) + country_code = region["code"] |> String.split("-") |> List.first() + + region_entry = ISOCodes.ISO3166_2.get(region["code"]) + country_entry = ISOCodes.ISO3166_1.get(country_code) + Map.merge(region, %{"name" => region_entry.name, "country_flag" => country_entry.flag}) end) json(conn, countries) diff --git a/mix.exs b/mix.exs index 4be1eac35..aea4c9e05 100644 --- a/mix.exs +++ b/mix.exs @@ -85,6 +85,7 @@ defmodule Plausible.MixProject do {:sshex, "2.2.1"}, {:geolix, "~> 1.0"}, {:clickhouse_ecto, git: "https://github.com/plausible/clickhouse_ecto.git"}, + {:iso_codes, git: "https://github.com/plausible/iso_codes"}, {:geolix_adapter_mmdb2, "~> 0.5.0"}, {:cachex, "~> 3.4"}, {:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false}, diff --git a/mix.lock b/mix.lock index 6548ac415..5aac48a2b 100644 --- a/mix.lock +++ b/mix.lock @@ -49,6 +49,7 @@ "httpoison": {:hex, :httpoison, "1.8.0", "6b85dea15820b7804ef607ff78406ab449dd78bed923a49c7160e1886e987a3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "28089eaa98cf90c66265b6b5ad87c59a3729bea2e74e9d08f9b51eb9729b3c3a"}, "hut": {:hex, :hut, "1.3.0", "71f2f054e657c03f959cf1acc43f436ea87580696528ca2a55c8afb1b06c85e7", [:"erlang.mk", :rebar, :rebar3], [], "hexpm", "7e15d28555d8a1f2b5a3a931ec120af0753e4853a4c66053db354f35bf9ab563"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, + "iso_codes": {:git, "https://github.com/plausible/iso_codes", "413312b01cbdbd9a8283deb94f91bca12830eba4", []}, "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, "jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"}, "kaffy": {:hex, :kaffy, "0.9.0", "bef34c9729f6a3af4d0dea8eede8bcb9e11371a83ac9a8b393991bce81839517", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.11", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "d18ff57b8e68feb433aed11e71510cd357abc7034e75358af5deff7d0d4c6ed3"}, diff --git a/test/plausible_web/controllers/api/stats_controller/countries_test.exs b/test/plausible_web/controllers/api/stats_controller/countries_test.exs index 345cd3f07..444ce4eff 100644 --- a/test/plausible_web/controllers/api/stats_controller/countries_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/countries_test.exs @@ -24,12 +24,14 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do %{ "code" => "EST", "name" => "Estonia", + "flag" => "🇪🇪", "visitors" => 2, "percentage" => 67 }, %{ "code" => "GBR", "name" => "United Kingdom", + "flag" => "🇬🇧", "visitors" => 1, "percentage" => 33 } @@ -62,6 +64,7 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do %{ "code" => "GBR", "name" => "United Kingdom", + "flag" => "🇬🇧", "total_visitors" => 1, "visitors" => 1, "conversion_rate" => 100.0 @@ -69,6 +72,7 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do %{ "code" => "EST", "name" => "Estonia", + "flag" => "🇪🇪", "total_visitors" => 2, "visitors" => 1, "conversion_rate" => 50.0