-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathRouter.js
More file actions
143 lines (117 loc) · 3.27 KB
/
Router.js
File metadata and controls
143 lines (117 loc) · 3.27 KB
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
import React from 'react';
import { Router as ReactRouter, Route } from 'react-router';
import utils from './../utils';
import context from './../context';
import HomeRoute from './HomeRoute';
import LoginRoute from './LoginRoute';
import LogoutRoute from './LogoutRoute';
import AuthenticatedRoute from './AuthenticatedRoute';
export default class Router extends ReactRouter {
static childContextTypes = {
authenticated: React.PropTypes.bool,
user: React.PropTypes.object
};
static propTypes = ReactRouter.propTypes;
static defaultProps = ReactRouter.defaultProps;
state = {
authenticated: false,
user: undefined
};
markedRoutes = {
home: {
type: HomeRoute,
authenticated: {
props: null
},
notAuthenticated: {
props: null
}
},
login: {
type: LoginRoute,
props: null
},
logout: {
type: LogoutRoute,
props: null
}
};
constructor() {
super(...arguments);
if (this.props.routes) {
this._isPlainRoute(this.props.routes)
? this._mapMarkedPlainRoutes(this.props.routes)
// The reason we wrap in a div is because we just need to have a root element.
: this._mapMarkedRoutes(<div>{this.props.routes}</div>);
} else {
this._mapMarkedRoutes(this);
}
this.sessionChangeListener = this._setSessionState.bind(this);
context.setRouter(this);
}
_isPlainRoute(routeData) {
return (routeData.component)
|| (routeData.length && routeData[0] && routeData[0].component);
}
_mapMarkedPlainRoutes(routeData, index) {
if (routeData.length) {
return (<div>{routeData.map(this._mapMarkedPlainRoutes)}</div>);
}
return (
<Route key={index} {...routeData} />
);
}
_mapMarkedRoutes(routes) {
let markedRoutes = this.markedRoutes;
utils.deepForEach(routes, (node, parent) => {
// Try and map the route node to a marked route.
for (var routeName in markedRoutes) {
var route = markedRoutes[routeName];
if (node.type === route.type) {
var markedRoute = markedRoutes[routeName];
if (node.type === HomeRoute) {
if (parent.type === AuthenticatedRoute) {
markedRoute = markedRoute.authenticated;
} else {
markedRoute = markedRoute.notAuthenticated;
}
}
markedRoute.props = node.props;
break;
}
}
});
}
getHomeRoute() {
return this.markedRoutes.home.notAuthenticated.props;
}
getAuthenticatedHomeRoute() {
return this.markedRoutes.home.authenticated.props;
}
getLoginRoute() {
return this.markedRoutes.login.props;
}
getLogoutRoute() {
return this.markedRoutes.logout.props;
}
_setSessionState(user) {
this.setState({
authenticated: user !== undefined,
user: user
});
}
componentDidMount() {
this._setSessionState(context.sessionStore.get());
context.sessionStore.addListener('changed', this.sessionChangeListener);
}
componentWillUnmount() {
super.componentWillUnmount();
context.sessionStore.removeListener('changed', this.sessionChangeListener);
}
getChildContext() {
return {
authenticated: this.state.authenticated,
user: this.state.user
};
}
}