-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PB-877: add autoselect to swisssearch url param
- Loading branch information
Showing
8 changed files
with
153 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import AbstractParamConfig, { | ||
STORE_DISPATCHER_ROUTER_PLUGIN, | ||
} from '@/router/storeSync/abstractParamConfig.class' | ||
import { removeQueryParamFromHref } from '@/utils/searchParamUtils' | ||
import { URL_PARAM_NAME_SWISSSEARCH } from './SearchParamConfig.class' | ||
|
||
const URL_PARAM_NAME = 'swisssearch_autoselect' | ||
/** | ||
* The goal is to stop centering on the search when sharing a position. When we share a position, | ||
* both the center and the crosshair are sets. | ||
* | ||
* @param {Object} to The route object containing the query | ||
* @param {Object} store The store | ||
* @param {String} urlParamValue The search param | ||
*/ | ||
function dispatchSearchFromUrl(to, store, urlParamValue) { | ||
// avoiding setting the swisssearch autoselect to the store when there is nothing to autoselect because there is no swisssearch query | ||
if (urlParamValue && to.query[URL_PARAM_NAME_SWISSSEARCH]) { | ||
store.dispatch('setSwisssearchAutoSelect', { | ||
value: urlParamValue, | ||
dispatcher: STORE_DISPATCHER_ROUTER_PLUGIN, | ||
}) | ||
} | ||
} | ||
|
||
export default class SearchAutoSelectConfig extends AbstractParamConfig { | ||
constructor() { | ||
super({ | ||
urlParamName: URL_PARAM_NAME, | ||
mutationsToWatch: ['setSwisssearchAutoSelect'], | ||
setValuesInStore: dispatchSearchFromUrl, | ||
afterSetValuesInStore: () => removeQueryParamFromHref(URL_PARAM_NAME), | ||
extractValueFromStore: (store) => store.state.search.swisssearchAutoSelect, | ||
keepInUrlWhenDefault: false, | ||
valueType: Boolean, | ||
defaultValue: false, | ||
validateUrlInput: null, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* This will remove the query param from the URL It is necessary to do this in vanilla JS because | ||
* the router does not provide a way to remove a query without reloading the page which then removes | ||
* the value from the store. | ||
* | ||
* @param {Object} key The key to remove from the URL | ||
*/ | ||
export function removeQueryParamFromHref(key) { | ||
const [baseUrl, queryString] = window.location.href.split('?') | ||
if (!queryString) { | ||
return | ||
} | ||
|
||
const params = new URLSearchParams(queryString) | ||
if (!params.has(key)) { | ||
return | ||
} | ||
params.delete(key) | ||
|
||
const newQueryString = params.toString() | ||
const newUrl = newQueryString ? `${baseUrl}?${newQueryString}` : baseUrl | ||
window.history.replaceState({}, document.title, newUrl) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters