-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
104 lines (96 loc) · 3.44 KB
/
Copy pathApp.js
File metadata and controls
104 lines (96 loc) · 3.44 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
import agent from './agent';
import Header from './Header';
import React from 'react';
import { connect } from 'react-redux';
import { APP_LOAD, REDIRECT, SIGN_IN, LOGOUT } from './constants/actionTypes';
import { Route, Switch } from 'react-router-dom';
import Home from './containers/Home';
import { store } from './store';
import { push } from 'react-router-redux';
import Book from './containers/Book';
import Article from './containers/Article';
import './bootstrap.min.css'
import Profile from './containers/Profile';
import BookCreate from './containers/Book/BookCreate';
import BookList from './containers/Book/BookList';
import ArticleCreate from './containers/Article/ArticleCreate';
import BookEdit from './containers/Book/BookEdit';
import { IntlProvider } from 'react-intl';
import ArticleEdit from './containers/Article/ArticleEdit';
import Login from './containers/Authentication/Login';
import Register from './containers/Authentication/Register';
const mapStateToProps = state => {
return {
appLoaded: state.common.appLoaded,
appName: state.common.appName,
currentUser: state.common.currentUser,
redirectTo: state.common.redirectTo,
}
};
const mapDispatchToProps = dispatch => ({
onLoad: (payload, token) =>
dispatch({ type: APP_LOAD, payload, token, skipTracking: true }),
onRedirect: () =>
dispatch({ type: REDIRECT }),
onLoadProfile: (payload) =>
dispatch({ type: SIGN_IN, payload }),
onClickLogout: () => dispatch({ type: LOGOUT })
});
class App extends React.Component {
componentWillReceiveProps(nextProps) {
if (nextProps.redirectTo) {
store.dispatch(push(nextProps.redirectTo));
this.props.onRedirect();
}
}
componentWillMount() {
const token = window.localStorage.getItem('access_token');
if (token) {
agent.setToken(token);
this.props.onLoadProfile(agent.Auth.current())
}
this.props.onLoad(token ? agent.Auth.current() : null, token);
}
logout = () => {
this.props.onClickLogout();
}
render() {
if (!(window.location.pathname === "/login" ||
window.location.pathname === "/register") &&
!window.localStorage.getItem('access_token')) {
store.dispatch(push("/login"));
}
if (this.props.appLoaded) {
return (
<div>
<Header
appName={this.props.appName}
currentUser={this.props.currentUser}
onClickLogout={this.logout} />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/profiles/:id" component={Profile} />
<Route path="/books/:bookId/articles/create" component={ArticleCreate} />
<Route path="/books/:bookId/articles/:articleId/edit" component={ArticleEdit} />
<Route path="/books/:bookId/articles/:articleId" component={Article} />
<Route path="/books/create" component={BookCreate} />
<Route path="/books/:id/edit" component={BookEdit} />
<Route path="/books/:id" component={Book} />
<Route path="/books" component={BookList} />
<Route path="/login" component={Login} />
<Route path="/register" component={Register} />
</Switch>
</div>
);
}
return (
<div>
<Header
appName={this.props.appName}
currentUser={this.props.currentUser}
onClickLogout={this.logout} />
</div>
);
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App);