|
1 | 1 | import React from 'react'
|
2 | 2 | import { render } from 'react-dom'
|
3 |
| -import { browserHistory, Router, Route, Link, routerShape } from 'react-router' |
| 3 | +import { browserHistory, Router, Route, Link, withRouter } from 'react-router' |
4 | 4 | import auth from './auth'
|
5 | 5 |
|
6 | 6 | const App = React.createClass({
|
@@ -55,51 +55,49 @@ const Dashboard = React.createClass({
|
55 | 55 | }
|
56 | 56 | })
|
57 | 57 |
|
58 |
| -const Login = React.createClass({ |
| 58 | +const Login = withRouter( |
| 59 | + React.createClass({ |
59 | 60 |
|
60 |
| - contextTypes: { |
61 |
| - router: routerShape.isRequired |
62 |
| - }, |
63 |
| - |
64 |
| - getInitialState() { |
65 |
| - return { |
66 |
| - error: false |
67 |
| - } |
68 |
| - }, |
69 |
| - |
70 |
| - handleSubmit(event) { |
71 |
| - event.preventDefault() |
72 |
| - |
73 |
| - const email = this.refs.email.value |
74 |
| - const pass = this.refs.pass.value |
75 |
| - |
76 |
| - auth.login(email, pass, (loggedIn) => { |
77 |
| - if (!loggedIn) |
78 |
| - return this.setState({ error: true }) |
79 |
| - |
80 |
| - const { location } = this.props |
81 |
| - |
82 |
| - if (location.state && location.state.nextPathname) { |
83 |
| - this.context.router.replace(location.state.nextPathname) |
84 |
| - } else { |
85 |
| - this.context.router.replace('/') |
| 61 | + getInitialState() { |
| 62 | + return { |
| 63 | + error: false |
86 | 64 | }
|
87 |
| - }) |
88 |
| - }, |
89 |
| - |
90 |
| - render() { |
91 |
| - return ( |
92 |
| - <form onSubmit={this.handleSubmit}> |
93 |
| - <label><input ref="email" placeholder="email" defaultValue="[email protected]" /></label> |
94 |
| - <label><input ref="pass" placeholder="password" /></label> (hint: password1)<br /> |
95 |
| - <button type="submit">login</button> |
96 |
| - {this.state.error && ( |
97 |
| - <p>Bad login information</p> |
98 |
| - )} |
99 |
| - </form> |
100 |
| - ) |
101 |
| - } |
102 |
| -}) |
| 65 | + }, |
| 66 | + |
| 67 | + handleSubmit(event) { |
| 68 | + event.preventDefault() |
| 69 | + |
| 70 | + const email = this.refs.email.value |
| 71 | + const pass = this.refs.pass.value |
| 72 | + |
| 73 | + auth.login(email, pass, (loggedIn) => { |
| 74 | + if (!loggedIn) |
| 75 | + return this.setState({ error: true }) |
| 76 | + |
| 77 | + const { location } = this.props |
| 78 | + |
| 79 | + if (location.state && location.state.nextPathname) { |
| 80 | + this.props.router.replace(location.state.nextPathname) |
| 81 | + } else { |
| 82 | + this.props.router.replace('/') |
| 83 | + } |
| 84 | + }) |
| 85 | + }, |
| 86 | + |
| 87 | + render() { |
| 88 | + return ( |
| 89 | + <form onSubmit={this.handleSubmit}> |
| 90 | + <label><input ref="email" placeholder="email" defaultValue="[email protected]" /></label> |
| 91 | + <label><input ref="pass" placeholder="password" /></label> (hint: password1)<br /> |
| 92 | + <button type="submit">login</button> |
| 93 | + {this.state.error && ( |
| 94 | + <p>Bad login information</p> |
| 95 | + )} |
| 96 | + </form> |
| 97 | + ) |
| 98 | + } |
| 99 | + }) |
| 100 | +) |
103 | 101 |
|
104 | 102 | const About = React.createClass({
|
105 | 103 | render() {
|
|
0 commit comments