diff --git a/client_core/router/history/index.ts b/client_core/router/history/index.ts index c7828f4..3b846b5 100644 --- a/client_core/router/history/index.ts +++ b/client_core/router/history/index.ts @@ -35,11 +35,12 @@ function patchHistory( const { pathname, search } = location const { state } = history - onHistoryChange(pathname, state, result => { - let { newPathname } = result - search && (newPathname += search) + let newPathname = pathname + search && (newPathname += search) - history.replaceState(result.newHistoryState, '', newPathname) + onHistoryChange(newPathname, state, result => { + const { newHistoryState, newPathname } = result + history.replaceState(newHistoryState, '', newPathname) }) } diff --git a/client_core/utils/query_update/README.md b/client_core/utils/query_update/README.md index 7171fe0..296c160 100644 --- a/client_core/utils/query_update/README.md +++ b/client_core/utils/query_update/README.md @@ -1,18 +1,49 @@ ## Query update -Updates URL string with new query params
+Updates URL query with new query parameters
Has **2** signatures: - Receives **2** parameters, to change one query paramenter: - **key** - **String**. Paramenter key - **value** - **String**. Paramenter value - Receives **1** parameter, to change multiple parameters: - - **paramenters** - **Object** where key is parameter key and value is paramenter value + - **paramenters** - **Object** where **key** is parameter key and **value** is paramenter value
```js import updateURLQuery from 'siegel/lib/client_core/ui/utils/query_update' -updateURLQuery(history, 'somekey', 'someValue') +console.log(location.search) // '?paramKey=paramValue' + + +// Update single key (1st parameters) with value (2nd parameter) +console.log( + updateURLQuery('paramKey', 'newParamValue') +) +// '?paramKey=newParamValue' + + + +// Update using key value hashmap +console.log( + updateURLQuery({ + paramKey: 123, + anotherKey: false, + emptyValueKey: '' + }) +) +// '?paramKey=123&anotherKey=false&emptyValueKey=' + + + +// Remove some keys passing undefined / null as values +console.log( + updateURLQuery({ + paramKey: null, + anotherKey: 'another_value', + emptyValueKey: undefined + }) +) +// '?anotherKey=another_value' ``` diff --git a/client_core/utils/query_update/index.ts b/client_core/utils/query_update/index.ts index 9e6796a..f76b861 100644 --- a/client_core/utils/query_update/index.ts +++ b/client_core/utils/query_update/index.ts @@ -1,4 +1,4 @@ -import isExists from '../../../common/is/exists' +import isNullable from '../../../common/is/nullable' type QueryValue = string | number | boolean @@ -12,28 +12,26 @@ type UpdateURLQuery = { function updateQuery(query: URLSearchParams, key: string, value: QueryValue | undefined) { - value === '' || !isExists(value) + isNullable(value) ? query.delete(key) : query.set(key, (value as string)) } /** - * Updates browser URL query params + * Returns updated URL search query * * @param key query param key. Or object with keys as query params and valuest as query param values * @param value query param value * @returns String query params */ const updateURLQuery: UpdateURLQuery = function(key, value) { - const { search, pathname } = location + const { search } = location const query = new URLSearchParams(search) if (typeof key == 'string') updateQuery(query, key, value) else for (const searchKey in key) updateQuery(query, searchKey, key[searchKey]) - history.replaceState({}, '', `${pathname}?${query}`) - return query } diff --git a/package-lock.json b/package-lock.json index 837f769..9d0f517 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "siegel", - "version": "0.14.84", + "version": "0.14.85", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index ecd6ae8..3f377fd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "siegel", - "version": "0.14.84", + "version": "0.14.85", "homepage": "https://siegel-qe3q.onrender.com", "url": "https://github.com/CyberCookie/siegel", "bugs": "https://github.com/CyberCookie/siegel/issues",