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
This commit is contained in:
birjolaxew 2020-11-11 11:23:42 +01:00
parent d0d7b823f8
commit 0a565025d2

View File

@ -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) { export function toHuman(query) {
if (query.period === 'day') { if (query.period === 'day') {
return `on ${formatDay(query.date)}` return `on ${formatDay(query.date)}`