Skip to content

Commit 776c295

Browse files
codejockiesupasate
authored andcommitted
Injected query into location (#356)
1 parent afe8c22 commit 776c295

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/reducer.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
import { LOCATION_CHANGE } from './actions'
22

3+
/**
4+
* Adds query to location.
5+
* Utilises the search prop of location to construct query.
6+
*/
7+
const injectQuery = (location) => {
8+
if (!location) {
9+
return location
10+
}
11+
12+
const searchQuery = location.search || window.location.search
13+
14+
if (typeof searchQuery !== 'string' || searchQuery.length === 0) {
15+
return {
16+
...location,
17+
query: {}
18+
}
19+
}
20+
21+
// Ignore the `?` part of the search string e.g. ?username=codejockie
22+
const search = searchQuery.substring(1)
23+
// Split the query string on `&` e.g. ?username=codejockie&name=Kennedy
24+
const queries = search.split('&')
25+
// Contruct query
26+
const query = queries.reduce((acc, currentQuery) => {
27+
// Split on `=`, to get key and value
28+
const [queryKey, queryValue] = currentQuery.split('=')
29+
return {
30+
...acc,
31+
[queryKey]: queryValue
32+
}
33+
}, {})
34+
35+
return {
36+
...location,
37+
query
38+
}
39+
}
40+
341
const createConnectRouter = (structure) => {
442
const {
543
fromJS,
@@ -8,7 +46,7 @@ const createConnectRouter = (structure) => {
846

947
const createRouterReducer = (history) => {
1048
const initialRouterState = fromJS({
11-
location: history.location,
49+
location: injectQuery(history.location),
1250
action: history.action,
1351
})
1452

@@ -23,7 +61,7 @@ const createConnectRouter = (structure) => {
2361
// to prevent the double-rendering issue on initilization
2462
return isFirstRendering
2563
? state
26-
: merge(state, { location: fromJS(location), action })
64+
: merge(state, { location: fromJS(injectQuery(location)), action })
2765
}
2866

2967
return state

test/reducer.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe('connectRouter', () => {
6363
pathname: '/path/to/somewhere',
6464
search: '?query=test',
6565
hash: '',
66+
query: { query: 'test' }
6667
},
6768
action: 'PUSH',
6869
},
@@ -167,6 +168,7 @@ describe('connectRouter', () => {
167168
pathname: '/path/to/somewhere',
168169
search: '?query=test',
169170
hash: '',
171+
query: { query: 'test' }
170172
},
171173
action: 'PUSH',
172174
},
@@ -248,6 +250,7 @@ describe('connectRouter', () => {
248250
pathname: '/path/to/somewhere',
249251
search: '?query=test',
250252
hash: '',
253+
query: { query: 'test' }
251254
},
252255
action: 'PUSH',
253256
},

test/selectors.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("selectors", () => {
5555

5656
describe("getLocation", () => {
5757
it("gets the location from the state", () => {
58-
const location = { pathname: "/", hash: '', search: '' }
58+
const location = { pathname: "/", hash: '', query: {}, search: '', }
5959
store.dispatch(push('/'))
6060
const state = store.getState()
6161
expect(getLocation(state)).toEqual(location)

0 commit comments

Comments
 (0)