Skip to content

Commit 83b3b18

Browse files
committed
Extracts protocol redirection to helper
1 parent 9b8234d commit 83b3b18

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

client/components/forceProtocol.jsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
import React, { PropTypes } from 'react';
1+
import React from 'react';
22
import { format, parse } from 'url';
33

4+
const findCurrentProtocol = () => (
5+
parse(window.location.href).protocol
6+
);
7+
8+
const redirectToProtocol = (protocol, { appendSource, disable = false } = {}) => {
9+
const currentProtocol = findCurrentProtocol();
10+
11+
if (protocol !== currentProtocol) {
12+
if (disable === true) {
13+
console.info(`forceProtocol: would have redirected from "${currentProtocol}" to "${protocol}"`);
14+
} else {
15+
const url = parse(window.location.href, true /* parse query string */);
16+
url.protocol = protocol;
17+
if (appendSource === true) {
18+
url.query.source = currentProtocol;
19+
}
20+
window.location = format(url);
21+
}
22+
}
23+
};
24+
425
/**
526
* A Higher Order Component that forces the protocol to change on mount
627
*
@@ -14,29 +35,12 @@ const forceProtocol = ({ targetProtocol = 'https', sourceProtocol, disable = fal
1435
static propTypes = {}
1536

1637
componentDidMount() {
17-
this.redirectToProtocol(targetProtocol, { appendSource: true });
38+
redirectToProtocol(targetProtocol, { appendSource: true, disable });
1839
}
1940

2041
componentWillUnmount() {
2142
if (sourceProtocol != null) {
22-
this.redirectToProtocol(sourceProtocol, { appendSource: false });
23-
}
24-
}
25-
26-
redirectToProtocol(protocol, { appendSource }) {
27-
const currentProtocol = parse(window.location.href).protocol;
28-
29-
if (protocol !== currentProtocol) {
30-
if (disable === true) {
31-
console.info(`forceProtocol: would have redirected from "${currentProtocol}" to "${protocol}"`);
32-
} else {
33-
const url = parse(window.location.href, true /* parse query string */);
34-
url.protocol = protocol;
35-
if (appendSource === true) {
36-
url.query.source = currentProtocol;
37-
}
38-
window.location = format(url);
39-
}
43+
redirectToProtocol(sourceProtocol, { appendSource: false, disable });
4044
}
4145
}
4246

@@ -65,6 +69,8 @@ const findSourceProtocol = (state, location) => {
6569

6670
export default forceProtocol;
6771
export {
72+
findCurrentProtocol,
6873
findSourceProtocol,
74+
redirectToProtocol,
6975
protocols,
7076
};

0 commit comments

Comments
 (0)