From 3a1e72ac23c23ae02986c3a1ff4e8dc9ca7938e1 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 29 Jun 2021 10:50:52 +0300 Subject: [PATCH] Simplify filter modal --- assets/js/dashboard/stats/modals/filter.js | 46 +++++++++++----------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index ea05cb29e..c31d643c2 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -11,14 +11,16 @@ import * as api from '../../api' class FilterModal extends React.Component { constructor(props) { super(props) + const query = parseQuery(props.location.search, props.site) + const selectedFilter = this.props.match.params.field || 'page' + this.state = { - query: parseQuery(props.location.search, props.site), - selectedFilter: this.props.match.params.field || 'page', + query, + selectedFilter, negated: false, - filterValue: "", + filterValue: query.filters[selectedFilter], } - this.editableGoals = Object.keys(this.state.query.filters).filter(filter => !['props'].includes(filter)) this.handleKeydown = this.handleKeydown.bind(this) this.handleSubmit = this.handleSubmit.bind(this) } @@ -92,9 +94,20 @@ class FilterModal extends React.Component { ) } + selectFilterAndCloseModal(filterKey, filterValue) { + const queryString = new URLSearchParams(window.location.search) + + if (filterValue) { + queryString.set(filterKey, filterValue) + } else { + queryString.delete(filterKey) + } + + this.props.history.replace({pathname: `/${encodeURIComponent(this.props.site.domain)}`, search: queryString.toString()}) + } + handleSubmit() { const { selectedFilter, negated, filterValue } = this.state; - const validFilter = this.editableGoals.includes(selectedFilter) && filterValue.trim() let finalFilterValue = (this.negationSupported(selectedFilter) && negated ? '!' : '') + filterValue.trim() if (selectedFilter == 'country') { @@ -102,17 +115,13 @@ class FilterModal extends React.Component { const selectedCountry = allCountries.find((c) => c.properties.name === finalFilterValue) || { id: finalFilterValue }; finalFilterValue = selectedCountry.id } - const finalizedQuery = new URLSearchParams(window.location.search) - finalizedQuery.set(selectedFilter, finalFilterValue) - if (validFilter) { - this.setState({ finalizedQuery }) - } + this.selectFilterAndCloseModal(selectedFilter, finalFilterValue) } renderBody() { const { selectedFilter, negated, filterValue, query } = this.state; - const validFilter = this.editableGoals.includes(selectedFilter) && filterValue.trim() + const editableFilters = Object.keys(this.state.query.filters).filter(filter => !['props'].includes(filter)) return ( <> @@ -122,13 +131,13 @@ class FilterModal extends React.Component {
{this.negationSupported(selectedFilter) && ( @@ -150,7 +159,7 @@ class FilterModal extends React.Component {