-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.js
156 lines (134 loc) · 3.53 KB
/
Router.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// NOTE: our routing needs are so simple, we roll our own
// to avoid dependencies on react-router, which doesn't appear to
// be keeping up with React changes.
import React, { Children, Component } from 'react';
import PropTypes from 'prop-types';
const RouterContext = React.createContext({});
export class Router extends Component {
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.search !== prevState.search) {
return { search: nextProps.search };
}
return null;
}
state = {};
componentDidMount() {
window.addEventListener('popstate', this.onPopState);
this.onPopState();
}
componentWillUnmount() {
window.removeEventListener('popstate', this.onPopState);
}
onPopState = () => {
const { location } = document;
this.setState({ path: location.pathname, search: location.search });
};
onPush = nextPath => {
const { path, search } = this.state;
if (nextPath !== path) {
if (nextPath.startsWith('http')) {
window.location = nextPath;
} else {
window.history.pushState(
undefined,
undefined,
`${nextPath}${search || ''}`,
);
this.setState({ path: nextPath });
window.scrollTo(0, 0);
}
}
};
render() {
const { children } = this.props;
const { path, search } = this.state;
return (
<RouterContext.Provider value={{ path, search, push: this.onPush }}>
{children}
</RouterContext.Provider>
);
}
}
Router.propTypes = {
children: PropTypes.node.isRequired,
};
export const Routes = ({ children, notFoundRedirect }) => (
<RouterContext.Consumer>
{({ path: currentPath }) => {
let found;
Children.forEach(children, child => {
if (
!found &&
currentPath &&
currentPath.split('#')[0] === child.props.path
) {
found = child;
}
});
if (currentPath && !found) {
window.location.replace(notFoundRedirect);
}
return found;
}}
</RouterContext.Consumer>
);
Routes.propTypes = {
children: PropTypes.node.isRequired,
notFoundRedirect: PropTypes.string.isRequired,
};
export const Route = ({ component: Comp, path, redirect }) => (
<RouterContext.Consumer>
{({ path: currentPath }) => {
if (currentPath && currentPath.split('#')[0] === path) {
if (redirect) {
window.location.replace(redirect);
} else if (Comp) {
return <Comp />;
} else {
console.error('Route missing component or redirect');
}
}
return null;
}}
</RouterContext.Consumer>
);
Route.propTypes = {
component: PropTypes.func,
path: PropTypes.string.isRequired,
redirect: PropTypes.string,
};
Route.defaultProps = {
component: undefined,
redirect: undefined,
};
export const Clicker = ({ children, path }) => (
<RouterContext.Consumer>
{({ push }) =>
children(event => {
event.preventDefault();
push(path);
})
}
</RouterContext.Consumer>
);
Clicker.propTypes = {
children: PropTypes.func.isRequired,
path: PropTypes.string.isRequired,
};
export const Watcher = ({ children }) => (
<RouterContext.Consumer>
{({ path }) => children(path)}
</RouterContext.Consumer>
);
Watcher.propTypes = {
children: PropTypes.func.isRequired,
};
export const Pusher = ({ children }) => (
<RouterContext.Consumer>
{({ push }) => children(push)}
</RouterContext.Consumer>
);
Pusher.propTypes = {
children: PropTypes.func.isRequired,
};
export default Router;