-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (74 loc) · 2.24 KB
/
index.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
// bootstrap imports need to be before other components and css imports
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap/dist/css/bootstrap-theme.css'
import './styles/index.css'
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { Route, Redirect } from 'react-router'
import { ConnectedRouter } from 'react-router-redux'
import configureStore, { history } from './store/configureStore'
import { Explore, FormPage, LoginPage, NavBar } from './components'
import { ToastContainer, toast } from 'react-toastify'
import {
appLoaded,
exitPOIForm,
loadMaps,
loadStories,
setSelectedMap
} from './actions'
import registerServiceWorker from './registerServiceWorker'
import { storage } from './utils'
import './styles/toast.css'
export const ROUTES = {
INDEX: '/',
FORM: '/form',
LOGIN: '/login'
}
const store = configureStore()
// dispatch the following on app load
store.dispatch(appLoaded())
loadMaps()(store.dispatch).then(action => {
const maps = action.payload
if (maps.length) {
maps.sort((a, b) => a.year - b.year)
setSelectedMap(maps[0])(store.dispatch)
}
})
loadStories()(store.dispatch)
// dispatch actions based on route changes
history.listen(location => {
// dunno if this is jank or not,
// but it prevents needing to imperitively dispatch this action
// in every case of a user leaving the form
if (location.pathname !== ROUTES.FORM) {
exitPOIForm()(store.dispatch)
}
})
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
!!storage.get('authorizationToken') ? (
<Component {...props} />
) : (
<Redirect to="/login" />
)}
/>
)
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}>
<div>
<NavBar />
<ToastContainer style={{ color: 'red' }} />
<Route exact path={ROUTES.INDEX} component={Explore} />
<PrivateRoute exact path={ROUTES.FORM} component={FormPage} />
<Route exact path={ROUTES.LOGIN} component={LoginPage} />
</div>
</ConnectedRouter>
</Provider>,
document.getElementById('root')
)
export const toastNotify = (message, options) => toast(message, options)
registerServiceWorker()