Fix issue with filters

pull/1498/head
Uku Taht 5 years ago
parent 7e93500834
commit 73a2dc00ea

@ -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() {

@ -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) {

Loading…
Cancel
Save