Skip to content

Commit 93d126b

Browse files
committed
Add menu component
1 parent b95772a commit 93d126b

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import React from 'react';
2+
import { Router } from 'react-router'
23

34
class HomeComponent extends React.Component{
45

56
render() {
67
if (!localStorage.token) {
7-
window.location.href = "/";
8+
this.context.router.push("/");
89
}
910
return (
1011
<h3>Welcome!</h3>
1112
);
1213
}
1314
}
1415

16+
HomeComponent.contextTypes = {
17+
router: React.PropTypes.object.isRequired
18+
};
19+
1520
export default HomeComponent;
1621

1722

app/components/Login/LoginComponent.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { Router } from 'react-router'
23

34
import User from 'services/User';
45

@@ -17,7 +18,7 @@ class LoginComponent extends React.Component{
1718
).then((response) => {
1819
if (response.data.success == 200) {
1920
localStorage.token = response.data.token;
20-
window.location.href = "/";
21+
this.context.router.push("/");
2122
}
2223
});
2324
}
@@ -70,4 +71,8 @@ class LoginComponent extends React.Component{
7071
}
7172
}
7273

74+
LoginComponent.contextTypes = {
75+
router: React.PropTypes.object.isRequired
76+
};
77+
7378
export default LoginComponent;

app/components/Menu/Menu.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react';
2+
import { Router, Link } from 'react-router'
3+
4+
class Menu extends React.Component{
5+
constructor(props, context) {
6+
super(props, context);
7+
this.handleLogout = this.handleLogout.bind(this);
8+
this.handleView = this.handleView.bind(this);
9+
}
10+
11+
handleLogout() {
12+
delete localStorage.token;
13+
this.context.router.push("/");
14+
}
15+
16+
handleView() {
17+
return (
18+
<div>
19+
<nav className="nav has-shadow" id="top">
20+
<div className="container">
21+
<div className="nav-left">
22+
<a className="nav-item" href="../index.html">IClient</a>
23+
</div>
24+
<span className="nav-toggle">
25+
<span></span>
26+
<span></span>
27+
<span></span>
28+
</span>
29+
<div className="nav-right nav-menu">
30+
<Link to="/" className="nav-item is-tab is-active">Home</Link>
31+
<Link to="/client" className="nav-item is-tab">Client</Link>
32+
<Link to="/area" className="nav-item is-tab">Area</Link>
33+
<span className="nav-item">
34+
<a className="button" onClick={this.handleLogout}>Logout</a>
35+
<a className="button is-info">Sign up </a>
36+
</span>
37+
</div>
38+
</div>
39+
</nav>
40+
<div className="content">
41+
{this.props.children}
42+
</div>
43+
</div>
44+
);
45+
}
46+
47+
render() {
48+
let view = this.handleView();
49+
50+
if (!localStorage.token) {
51+
view = <span></span>;
52+
}
53+
return (
54+
view
55+
);
56+
}
57+
}
58+
59+
Menu.contextTypes = {
60+
router: React.PropTypes.object.isRequired
61+
};
62+
63+
export default Menu;
64+

app/components/iClient/iClientComponent.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@ import React from 'react';
22
import { Router, Route, IndexRoute, IndexLink, Link } from 'react-router'
33

44
import LoginComponent from 'components/Login/LoginComponent';
5+
import MenuComponent from 'components/Menu/Menu';
56

67
class iClientComponent extends React.Component{
78
constructor(props) {
89
super(props);
910
}
1011

1112
render() {
12-
let view = <div>
13-
<h1>Simple SPA</h1>
14-
<ul className="header">
15-
<li><Link to="/">Home</Link></li>
16-
<li><Link to="/client">Clients</Link></li>
17-
</ul>
18-
<div className="content">
19-
{this.props.children}
20-
</div>
21-
</div>;
13+
let view = <MenuComponent children={this.props.children} />;
2214

2315
if (!localStorage.token) {
2416
view = <LoginComponent />;

0 commit comments

Comments
 (0)