From 73a2dc00eabada9b961c8e71c56b00a595218fca Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Fri, 26 Nov 2021 11:41:41 +0200 Subject: [PATCH] Fix issue with filters --- assets/js/dashboard/stats/modals/filter.js | 25 ++++++---------------- assets/js/dashboard/url.js | 6 +++++- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index 4c27d6ee7..944d6c724 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -1,7 +1,6 @@ import React, { Fragment } from "react"; import { withRouter } from 'react-router-dom' import classNames from 'classnames' -import Datamap from 'datamaps' import { Menu, Transition } from '@headlessui/react' import { ChevronDownIcon } from '@heroicons/react/solid' @@ -9,7 +8,7 @@ import SearchSelect from '../../components/search-select' import Modal from './modal' import { parseQuery, formattedFilters } from '../../query' import * as api from '../../api' -import {apiPath, sitePath} from '../../url' +import {apiPath, siteBasePath} from '../../url' export const FILTER_GROUPS = { 'page': ['page'], @@ -24,12 +23,6 @@ export const FILTER_GROUPS = { 'goal': ['goal'] } -function getCountryName(ISOCode) { - const allCountries = Datamap.prototype.worldTopo.objects.world.geometries; - const selectedCountry = allCountries.find((c) => c.id === ISOCode); - return selectedCountry && selectedCountry.properties.name -} - function getFormState(filterGroup, query) { return FILTER_GROUPS[filterGroup].reduce((result, filter) => { const filterValue = query.filters[filter] || '' @@ -37,10 +30,9 @@ function getFormState(filterGroup, query) { const type = filterValue[0] === '!' ? 'is_not' : 'is' - if (filter === 'country') { - filterName = getCountryName(filterValue) + if (filter === 'country' && filterValue !== '') { + filterName = (new URLSearchParams(window.location.search)).get('country_name') } - if (filter === 'region' && filterValue !== '') { filterName = (new URLSearchParams(window.location.search)).get('region_name') } @@ -115,16 +107,11 @@ class FilterModal extends React.Component { const { formState } = this.state; const filters = Object.entries(formState).reduce((res, [filterKey, {type, value, name}]) => { - let finalFilterValue = value - if (filterKey === 'country') { - const allCountries = Datamap.prototype.worldTopo.objects.world.geometries; - const selectedCountry = allCountries.find((c) => c.properties.name === value) || { id: value }; - finalFilterValue = selectedCountry.id - } - + if (filterKey === 'country') { res.push({filter: 'country_name', value: name}) } if (filterKey === 'region') { res.push({filter: 'region_name', value: name}) } if (filterKey === 'city') { res.push({filter: 'city_name', value: name}) } + let finalFilterValue = value finalFilterValue = (type === 'is_not' ? '!' : '') + finalFilterValue.trim() res.push({filter: filterKey, value: finalFilterValue}) @@ -196,7 +183,7 @@ class FilterModal extends React.Component { } }) - this.props.history.replace({pathname: sitePath(this.props.site), search: queryString.toString()}) + this.props.history.replace({pathname: siteBasePath(this.props.site), search: queryString.toString()}) } renderFilterInputs() { diff --git a/assets/js/dashboard/url.js b/assets/js/dashboard/url.js index 7c44b129d..71fe793a3 100644 --- a/assets/js/dashboard/url.js +++ b/assets/js/dashboard/url.js @@ -2,8 +2,12 @@ export function apiPath(site, path = '') { return `/api/stats/${encodeURIComponent(site.domain)}${path}` } +export function siteBasePath(site, path = '') { + return `/${encodeURIComponent(site.domain)}${path}` +} + export function sitePath(site, path = '') { - return `/${encodeURIComponent(site.domain)}${path}${window.location.search}` + siteBasePath(site, path) + window.location.search } export function setQuery(key, value) {