-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwithQuery.js
39 lines (33 loc) · 984 Bytes
/
withQuery.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
import React from 'react';
import URI from 'urijs';
function isValidValue(v) {
return v !== undefined;
}
function setQuery(obj, name, value) {
if (!isValidValue(obj[name]) && isValidValue(value)) {
Object.assign(obj, { [name]: value });
}
}
function uriWithExtraQuery(props) {
const { source: { uri }, query } = props;
const { userId, token, appName, appVersion, appType } = query || {};
const uriObj = URI(uri);
const queryObj = { ...uriObj.search(true) };
setQuery(queryObj, 'userId', userId);
setQuery(queryObj, 'token', token);
setQuery(queryObj, 'appName', appName);
setQuery(queryObj, 'appVersion', appVersion);
setQuery(queryObj, 'app-type', appType);
return uriObj.search(queryObj).toString();
}
// eslint-disable-next-line
export default WrappedWebView => class extends React.Component {
render() {
return (
<WrappedWebView
{...this.props}
source={{ uri: uriWithExtraQuery(this.props) }}
/>
);
}
};