|
1 | 1 | import updateQueryStringBatch from './updateQueryStringBatch' |
2 | 2 |
|
3 | | -export default function({ initialState }) { |
| 3 | +export default function({ initialState, ignoredKeys }) { |
4 | 4 | // a whitelist of keys to look for |
5 | 5 | const initialKeys = Object.keys(initialState) |
6 | 6 |
|
7 | 7 | // read the query string in the url |
8 | 8 | // parse out and apply any filters found there |
9 | 9 | const url = new URL(window.location) |
10 | 10 | const params = new URLSearchParams(url.search) |
| 11 | + const newQueryStringState = {} |
11 | 12 |
|
12 | 13 | // override starting values if they are found in the url |
13 | 14 | for (let entry of params.entries()) { |
14 | 15 | 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] |
18 | 28 | } |
19 | 29 | } |
20 | 30 |
|
21 | 31 | // update the query string |
22 | 32 | // handle the case where one or both places |
23 | 33 | // are initially absent from the query string |
24 | | - updateQueryStringBatch({ |
25 | | - source: initialState.source, |
26 | | - compare: initialState.compare |
27 | | - }) |
| 34 | + updateQueryStringBatch({ newQueryStringState }) |
28 | 35 |
|
29 | 36 | return initialState |
30 | 37 | } |
0 commit comments