|
1 | | -import React, { Component, PropTypes } from 'react'; |
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import { push } from 'react-router-redux'; |
2 | 4 | import { Field, reduxForm } from 'redux-form'; |
3 | 5 | import Button from 'react-bootstrap/lib/Button'; |
4 | 6 | // import validator from 'validator'; |
@@ -33,32 +35,31 @@ class LoginForm extends Component { |
33 | 35 | } |
34 | 36 |
|
35 | 37 | _login(json) { |
36 | | - return this.context.store.dispatch(loginUser({ |
| 38 | + return this.props.dispatch(loginUser({ |
37 | 39 | token: json.token, |
38 | 40 | data: json.user, |
39 | 41 | })); |
40 | 42 | } |
41 | 43 |
|
42 | 44 | _handleSubmit(formData) { |
43 | | - return userAPI(this.context.store.getState().apiEngine) |
| 45 | + // let { store } = this.context; |
| 46 | + let { dispatch, apiEngine } = this.props; |
| 47 | + |
| 48 | + return userAPI(apiEngine) |
44 | 49 | .login(formData) |
45 | 50 | .catch((err) => { |
46 | | - this.context.store.dispatch(pushErrors(err)); |
| 51 | + dispatch(pushErrors(err)); |
47 | 52 | throw err; |
48 | 53 | }) |
49 | 54 | .then((json) => { |
50 | 55 | if (json.isAuth) { |
51 | 56 | this.login(json).then(() => { |
52 | 57 | // redirect to the origin path before logging in |
53 | | - const { location } = this.props; |
54 | | - if (location && location.state && location.state.nextPathname) { |
55 | | - this.context.router.push(location.state.nextPathname); |
56 | | - } else { |
57 | | - this.context.router.push('/'); |
58 | | - } |
| 58 | + let { next } = this.props.routing.locationBeforeTransitions.query; |
| 59 | + dispatch(push(next || '/')); |
59 | 60 | }); |
60 | 61 | } else { |
61 | | - this.context.store.dispatch(pushErrors([{ |
| 62 | + dispatch(pushErrors([{ |
62 | 63 | title: 'User Not Exists', |
63 | 64 | detail: 'You may type wrong email or password.', |
64 | 65 | }])); |
@@ -100,12 +101,10 @@ class LoginForm extends Component { |
100 | 101 | } |
101 | 102 | }; |
102 | 103 |
|
103 | | -LoginForm.contextTypes = { |
104 | | - store: PropTypes.object.isRequired, |
105 | | - router: PropTypes.any.isRequired, |
106 | | -}; |
107 | | - |
108 | 104 | export default reduxForm({ |
109 | 105 | form: 'login', |
110 | 106 | validate, |
111 | | -})(LoginForm); |
| 107 | +})(connect(state => ({ |
| 108 | + apiEngine: state.apiEngine, |
| 109 | + routing: state.routing, |
| 110 | +}))(LoginForm)); |
0 commit comments