Skip to content

Commit

Permalink
do some validation on filters before updating the map
Browse files Browse the repository at this point in the history
Highlight empty filters and also prevent the map updating if there are
empty filters.
Also ignore empty filters at the back end to avoid 500 errors

Fixes #328
  • Loading branch information
struan committed Nov 29, 2023
1 parent 92b7b4f commit 5e6297d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions hub/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class FilterMixin:
def filters(self):
filters = []
for param, value in self.request.GET.items():
# we don't filter on blank values so skip them
if value == "":
continue

if "__" in param and param not in ["shader", "columns"]:
name, comparator = param.split("__", 1)
else: # pragma: nocover
Expand Down
16 changes: 14 additions & 2 deletions hub/static/js/explore.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,21 @@ const app = createApp({

return url
},
filtersValid() {
var valid = true
this.filters.forEach(function(d) {
var value = d.selectedValue
if (value === "") {
valid = false
}
})
return valid
},
updateState() {
window.history.replaceState(this.state(), '', this.url())
this.updateResults()
if (this.filtersValid()) {
window.history.replaceState(this.state(), '', this.url())
this.updateResults()
}
},
restoreState() {
const params = new URL(window.location).searchParams
Expand Down
6 changes: 3 additions & 3 deletions hub/templates/hub/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ <h3 class="h6">${ filter.title }</h3>
<option v-for="option in filter.options" :value="option">${ option }</option>
</select>
<div v-else-if="filter.data_type == 'percent'" class="input-group flex-grow-1 flex-shrink-1">
<input type="text" inputmode="numeric" pattern="[0-9]*" v-model="filter.selectedValue" class="form-control form-control-sm text-end">
<input type="text" inputmode="numeric" pattern="[0-9]*" v-model="filter.selectedValue" class="form-control form-control-sm text-end" :class="{ 'is-invalid': filter.selectedValue === '' }">
<span class="input-group-text lh-1">%</span>
</div>
<input v-else-if="filter.data_type == 'integer'" type="text" inputmode="numeric" pattern="[0-9]*" v-model="filter.selectedValue" class="form-control form-control-sm flex-grow-1 flex-shrink-1">
<input v-else type="text" v-model="filter.selectedValue" class="form-control form-control-sm flex-grow-1 flex-shrink-1">
<input v-else-if="filter.data_type == 'integer'" type="text" inputmode="numeric" pattern="[0-9]*" v-model="filter.selectedValue" class="form-control form-control-sm flex-grow-1 flex-shrink-1" :class="{ 'is-invalid': filter.selectedValue === '' }">
<input v-else type="text" v-model="filter.selectedValue" class="form-control form-control-sm flex-grow-1 flex-shrink-1" :class="{ 'is-invalid': filter.selectedValue === '' }">
<p v-if="filter.is_in" class="flex-grow-1 text-muted mb-0 mt-2 text-center fs-8">Tip: Ctrl-click (PC) or Cmd-click (Mac) to select multiple items</p>
</div>
<details class="mt-3">
Expand Down
8 changes: 8 additions & 0 deletions hub/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ def setUp(self):
self.u = User.objects.create(username="[email protected]")
self.client.force_login(self.u)

def test_explore_json_page_null_value(self):
url = reverse("explore_json")
response = self.client.get(url + "?mp_last_elected__year__lt=")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "South Borsetshire")
self.assertContains(response, "Borsetshire West")
self.assertContains(response, "Borsetshire East")

def test_explore_json_page_year_lt(self):
url = reverse("explore_json")
response = self.client.get(url + "?mp_last_elected__year__lt=2019")
Expand Down

0 comments on commit 5e6297d

Please sign in to comment.