From 80428fc4125b68ad001a54f53c89ff3b44f4b387 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Thu, 1 Sep 2022 23:03:33 +1200 Subject: [PATCH] Expand `updateUrl` function --- .../pages/basePages/listPage/listPage.jsx | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/client/homebrew/pages/basePages/listPage/listPage.jsx b/client/homebrew/pages/basePages/listPage/listPage.jsx index e546e2c7f..b360adc6e 100644 --- a/client/homebrew/pages/basePages/listPage/listPage.jsx +++ b/client/homebrew/pages/basePages/listPage/listPage.jsx @@ -22,9 +22,9 @@ const ListPage = createClass({ }, getInitialState : function() { return { - sortType : 'alpha', - sortDir : 'asc', filterString : this.props.query?.filter || '', + sortType : this.props.query?.sort || 'alpha', + sortDir : this.props.query?.dir || 'asc', query : this.props.query }; }, @@ -50,14 +50,18 @@ const ListPage = createClass({ }, handleSortOptionChange : function(event){ + this.updateUrl(this.state.filterString, event.target.value, this.state.sortDir); this.setState({ sortType : event.target.value }); }, handleSortDirChange : function(event){ + const newDir = this.state.sortDir == 'asc' ? 'desc' : 'asc'; + + this.updateUrl(this.state.filterString, this.state.sortType, newDir); this.setState({ - sortDir : `${(this.state.sortDir == 'asc' ? 'desc' : 'asc')}` + sortDir : newDir }); }, @@ -77,19 +81,22 @@ const ListPage = createClass({ this.setState({ filterString : e.target.value, }); - this.updateUrl(e.target.value); + this.updateUrl(e.target.value, this.state.sortType, this.state.sortDir); return; }, - updateUrl : function(filterTerm){ + updateUrl : function(filterTerm, sortType, sortDir){ const url = new URL(window.location.href); const urlParams = new URLSearchParams(url.search); - if(urlParams.get('filter') == filterTerm) - return; + + urlParams.set('sort', sortType); + urlParams.set('dir', sortDir); + if(!filterTerm) urlParams.delete('filter'); else urlParams.set('filter', filterTerm); + url.search = urlParams; window.history.replaceState(null, null, url); },