Skip to content

Commit 08d54b5

Browse files
committed
generalize parseStateFromUrl
1 parent 3771727 commit 08d54b5

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/utils/parseStateFromUrl.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
import updateQueryStringBatch from './updateQueryStringBatch'
22

3-
export default function({ initialState }) {
3+
export default function({ initialState, ignoredKeys }) {
44
// a whitelist of keys to look for
55
const initialKeys = Object.keys(initialState)
66

77
// read the query string in the url
88
// parse out and apply any filters found there
99
const url = new URL(window.location)
1010
const params = new URLSearchParams(url.search)
11+
const newQueryStringState = {}
1112

1213
// override starting values if they are found in the url
1314
for (let entry of params.entries()) {
1415
const key = decodeURIComponent(entry[0])
15-
const value = decodeURIComponent(entry[1])
16-
if (initialKeys.includes(key)) {
17-
initialState[key] = value
16+
if (ignoreKeys && ignoreKeys.includes(key)) {
17+
continue
18+
} else {
19+
// the key is not ignored
20+
// so we want it in the query string
21+
const value = decodeURIComponent(entry[1])
22+
if (initialKeys.includes(key)) {
23+
// if we find the value in the query string
24+
// update it
25+
initialState[key] = value
26+
}
27+
newQueryStringState[key] = initialState[key]
1828
}
1929
}
2030

2131
// update the query string
2232
// handle the case where one or both places
2333
// are initially absent from the query string
24-
updateQueryStringBatch({
25-
source: initialState.source,
26-
compare: initialState.compare
27-
})
34+
updateQueryStringBatch({ newQueryStringState })
2835

2936
return initialState
3037
}

0 commit comments

Comments
 (0)