From 0a565025d2acce203ad4d2138bb4b5ec2e7dce4b Mon Sep 17 00:00:00 2001 From: birjolaxew Date: Wed, 11 Nov 2020 11:23:42 +0100 Subject: [PATCH] Implement function for updating current page query This centralizes updating the search parameters It's needed in order to implement proper history handling Previously navigating to a new period filter would not revert if going back in history The new function will update history entry to include the period if a new period is navigated to This way going back in history will correctly revert the period filter --- assets/js/dashboard/query.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/assets/js/dashboard/query.js b/assets/js/dashboard/query.js index 00e8b3799..5c0f60745 100644 --- a/assets/js/dashboard/query.js +++ b/assets/js/dashboard/query.js @@ -41,6 +41,27 @@ export function parseQuery(querystring, site) { } } +export function navigateToQuery(history, queryFrom, newData) { + // if we update any data that we store in localstorage, make sure going back in history will revert them + if (newData.period && newData.period !== queryFrom.period) { + const replaceQuery = new URLSearchParams(window.location.search) + replaceQuery.set('period', queryFrom.period) + history.replace({ search: replaceQuery.toString() }) + } + + // then push the new query to the history + const query = new URLSearchParams(window.location.search) + Object.keys(newData).forEach(key => { + if (!newData[key]) { + query.delete(key) + return + } + + query.set(key, newData[key]) + }) + history.push({ search: query.toString() }) +} + export function toHuman(query) { if (query.period === 'day') { return `on ${formatDay(query.date)}`