This repository has been archived by the owner on Mar 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
79 lines (68 loc) · 2.55 KB
/
index.tsx
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
import { getCurrentActiveFeatureNames } from '@paralleldrive/feature-toggles'
import { FeatureToggles } from '@paralleldrive/react-feature-toggles'
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import { AppContainer } from 'react-hot-loader'
import { Provider } from 'react-redux'
import { Router, Route, browserHistory } from 'react-router'
import { Actions } from 'actions'
import { Layout } from 'components/Root'
import { initialFeatures } from 'config/features'
import { createPoetStore } from 'store'
import 'react-toastify/dist/ReactToastify.css'
async function init(): Promise<void> {
const { store, pages } = await createPoetStore()
const routes = pages
.map((page: any, index: any) => page.routeHook('' + index, store))
.reduce((a: any, b: any) => a.concat(b), [])
function handlerRoutes(store: any, pathname: string): void {
const state = store.getState()
const { user } = state
const omitRoutes: ReadonlyArray<any> = [
'/',
'/login',
'/forgot-password',
'/forgot-password/change-password',
'/verified-account',
'/privacy',
'/disclaimer',
]
const notNeedOuath = omitRoutes.includes(pathname)
if (['/login', '/login/'].includes(pathname) && user.token !== '') browserHistory.push('/dashboard')
if (!notNeedOuath && user.token === '') browserHistory.push('/login')
}
function requireAuth(store: any): (route: any, replace: object) => void {
return (route: any, replace: object): void => {
const pathname = route.location.pathname
store.dispatch(Actions.Router.onEnter(pathname))
handlerRoutes(store, pathname)
}
}
function onChange(store: any): (route: any, replace: object) => void {
return (route: any, replace: any): void => {
const pathname = replace.location.pathname
store.dispatch(Actions.Router.onChange(pathname))
handlerRoutes(store, pathname)
}
}
function notFound(route: any, replace: object): void {
browserHistory.push('/')
}
ReactDOM.render(
<Provider store={store}>
<AppContainer>
<FeatureToggles features={getCurrentActiveFeatureNames({ initialFeatures })}>
<Router history={browserHistory}>
<Route component={Layout} onEnter={requireAuth(store)} onChange={onChange(store)}>
{routes}
</Route>
<Route path="*" onEnter={notFound} />
</Router>
</FeatureToggles>
</AppContainer>
</Provider>,
document.getElementById('app'),
)
}
// tslint:disable-next-line
init().catch(console.error)