-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (75 loc) · 2.45 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
84
85
86
87
88
import 'babel-polyfill'
import React, {Component} from 'react'
import ReactDOM from 'react-dom'
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom';
// import {Router, Route} from 'react-router';
// import { BrowserRouter } from 'react-router-dom';
import {createStore, applyMiddleware} from 'redux';
import createSagaMiddleware from 'redux-saga';
import {Provider} from 'react-redux';
import {createLogger} from 'redux-logger';
import reducer from './Reducers';
import rootSaga from './Sagas';
import {clearError} from './containers/App/actions';
// import './styles/main.css';
import App from './containers/App';
import Home from './containers/Home'
import Login from './containers/Login';
// import Register from './components/Register'
// import Dashboard from './components/Dashboard'
// import NotFound from './components/NotFound'
let logger = createLogger({
// Ignore `CHANGE_FORM` actions in the logger, since they fire after every keystroke
predicate: (getState, action) => action.type !== 'CHANGE_FORM'
});
let sagaMiddleware = createSagaMiddleware();
// let reducer = createReducer()
// Creates the Redux store using our reducer and the logger and saga middlewares
let store = createStore(reducer, applyMiddleware(logger, sagaMiddleware));
// We run the root saga automatically
sagaMiddleware.run(rootSaga);
function checkAuth (nextState, replace) {
let {loggedIn} = store.getState();
store.dispatch(clearError());
if (nextState.location.pathname !== '/dashboard') {
if (loggedIn) {
if (nextState.location.state && nextState.location.pathname) {
replace(nextState.location.pathname)
} else {
replace('/')
}
}
} else {
// If the user is already logged in, forward them to the homepage
if (!loggedIn) {
if (nextState.location.state && nextState.location.pathname) {
replace(nextState.location.pathname)
} else {
replace('/')
}
}
}
}
class Root extends Component {
render () {
return (
<Provider store={store}>
<Router>
<Route component={App}>
<Route path='/' component={Home} />
<Route onEnter={checkAuth}>
<Route path='/login' component={Login} />
</Route>
</Route>
</Router>
</Provider>
)
}
}
ReactDOM.render(<Root />, document.getElementById('root'));
// ReactDOM.render(<App />, document.getElementById('root'));
// registerServiceWorker();