-
Notifications
You must be signed in to change notification settings - Fork 15
/
queryParams.js
42 lines (35 loc) · 1.14 KB
/
queryParams.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { coeffect, registerCoeffectHandler } from 'reffects';
export function queryParamsGet(selectedQueryParams) {
return coeffect('queryParams', selectedQueryParams);
}
export default function registerQueryParamsCoeffect(globalObject) {
registerCoeffectHandler('queryParams', function retrieveQueryParams(
selectedQueryParams = []
) {
const urlParams = new URLSearchParams(globalObject.location.search);
const queryParams = selectedQueryParams.reduce(
function convertQueryParamsToObject(queryParamValues, queryParam) {
const queryParamValue = urlParams
.getAll(queryParam)
.map(function urlDecodeParams(param) {
return decodeURIComponent(param);
});
if (queryParamValue.length === 0) {
return queryParamValues;
}
if (queryParamValue.length === 1) {
return Object.assign({}, queryParamValues, {
[queryParam]: queryParamValue[0],
});
}
return Object.assign({}, queryParamValues, {
[queryParam]: queryParamValue,
});
},
{}
);
return {
queryParams,
};
});
}