forked from PacktPublishing/JavaScript-by-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
109 lines (93 loc) · 2.85 KB
/
Copy pathApp.js
File metadata and controls
109 lines (93 loc) · 2.85 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
105
106
107
108
109
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import "bootstrap/dist/css/bootstrap.min.css";
import { Collapse, Navbar, NavbarToggler, Nav, NavItem } from 'reactstrap';
import { NavLink, Route, withRouter } from 'react-router-dom';
import './App.css';
import routes from './routes';
import Home from './Components/Home/Home';
import Post from './Components/Post/Post';
import AuthorList from './Components/Author/AuthorList';
import AuthorPosts from './Components/Author/AuthorPosts';
import NewPost from './Components/NewPost/NewPost';
/**
* Redux Actions
*/
import * as postActions from './redux/actions/postActions';
/**
* React-Redux connect function
*/
import { connect } from 'react-redux';
/**
* Redux bindActionCreators function
*/
import { bindActionCreators } from 'redux';
class App extends Component {
static propTypes = {
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
match: PropTypes.object.isRequired,
postActions: PropTypes.object.isRequired,
}
constructor(props) {
super(props);
this.state = {
isOpen: false,
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState({
isOpen: !this.state.isOpen
});
}
componentWillMount() {
this.props.postActions.getAllPosts();
if(this.props.location.pathname === '/') {
this.props.history.replace(routes.home);
}
}
render() {
return (
<div className="App">
<Navbar color="faded" light toggleable>
<NavbarToggler right onClick={this.toggle} />
<NavLink className="navbar-brand" to={routes.home}>Blog</NavLink>
<Collapse isOpen={this.state.isOpen} navbar>
<Nav className="ml-auto" navbar>
<NavItem>
<NavLink className="nav-link" activeClassName="active" to={routes.home}>Home</NavLink>
</NavItem>
<NavItem>
<NavLink className="nav-link" activeClassName="active" to={routes.authors}>Authors</NavLink>
</NavItem>
<NavItem>
<NavLink className="nav-link" activeClassName="active" to={routes.newPost}>New Post</NavLink>
</NavItem>
</Nav>
</Collapse>
</Navbar>
<Route exact path={routes.home} component={Home} />
<Route exact path={routes.post} component={Post} />
<Route exact path={routes.authors} component={AuthorList} />
<Route exact path={routes.author} component={AuthorPosts} />
<Route exact path={routes.newPost} component={NewPost} />
</div>
);
}
}
function mapStateToProps() {
return {
};
}
function mapDispatchToProps(dispatch) {
return {
postActions: bindActionCreators(postActions, dispatch),
};
}
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(App)
);