Skip to content

Commit

Permalink
Fix router popstate, update query_update util
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberCookie committed Dec 12, 2023
1 parent b54f2df commit 9510a8d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
9 changes: 5 additions & 4 deletions client_core/router/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
37 changes: 34 additions & 3 deletions client_core/utils/query_update/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
## Query update

Updates URL string with new query params<br />
Updates URL query with new query parameters<br />

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

<br />

```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'
```
10 changes: 4 additions & 6 deletions client_core/utils/query_update/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isExists from '../../../common/is/exists'
import isNullable from '../../../common/is/nullable'


type QueryValue = string | number | boolean
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 9510a8d

Please sign in to comment.