This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Root.js
105 lines (92 loc) · 2.68 KB
/
Root.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
/* global __DEVTOOLS__ */
import '../assets/stylesheets/index.css'
import React, { PropTypes } from 'react'
import { Redirect, Route } from 'react-router'
import { ReduxRouter } from 'redux-router'
import { connect } from 'react-redux'
import { IntlProvider } from 'react-intl'
import configureStore from './utils/configure-store'
import * as storage from './persistence/storage'
import * as components from './components'
import * as constants from './constants'
import * as i18n from './i18n'
const {
About,
Account,
AccountHome,
Application,
GithubStargazers,
GithubRepo,
GithubUser,
Home,
Login,
SuperSecretArea
} = components
const initialState = {
application: {
token: storage.get('token'),
locale: storage.get('locale') || 'en',
user: { permissions: [/*'manage_account'*/] }
}
}
export const store = configureStore(initialState)
function getRootChildren (props) {
const intlData = {
locale: props.application.locale,
messages: i18n[props.application.locale]
}
const rootChildren = [
<IntlProvider key="intl" {...intlData}>
{renderRoutes()}
</IntlProvider>
]
if (__DEVTOOLS__) {
const DevTools = require('./components/DevTools').default
rootChildren.push(<DevTools key="devtools" />)
}
return rootChildren
}
function renderRoutes () {
return (
<ReduxRouter>
<Route component={Application}>
<Route path="/" component={Home} />
<Redirect from="/account" to="/account/profile" />
<Route path="stargazers" component={GithubStargazers}>
<Route path=':username/:repo' component={GithubRepo} />
<Route path=':username' component={GithubUser} />
</Route>
<Route path="about" component={About} />
<Route path="account" component={Account} onEnter={requireAuth}>
<Route path="profile" component={AccountHome} />
<Route path="secret-area" component={SuperSecretArea} />
</Route>
<Route path="login" component={Login} />
<Route path="logout" onEnter={logout} />
</Route>
</ReduxRouter>
)
}
function requireAuth (nextState, replaceState) {
const state = store.getState()
const isLoggedIn = Boolean(state.application.token)
if (!isLoggedIn)
replaceState({
nextPathname: nextState.location.pathname
}, '/login')
}
function logout (nextState, replaceState) {
store.dispatch({ type: constants.LOG_OUT })
replaceState({}, '/login')
}
class Root extends React.Component {
static propTypes = {
application: PropTypes.object.isRequired
};
render () {
return (
<div>{getRootChildren(this.props)}</div>
)
}
}
export default connect(({ application }) => ({ application }))(Root)