Guest User

Untitled

a guest
Jan 1st, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import browserHistory from 'react-router/lib/browserHistory';
  5.  
  6. import { checkIfUserLoggedInAction, doLoginAction } from '../actions/Login';
  7.  
  8. class Login extends Component {
  9. static propTypes = {
  10. checkIfUserLoggedInAction: PropTypes.func.isRequired,
  11. doLoginAction: PropTypes.func.isRequired
  12. }
  13.  
  14. constructor() {
  15. super();
  16.  
  17. this.state = {
  18. username: '',
  19. password: ''
  20. };
  21.  
  22. this.handleOnChange = this.handleOnChange.bind(this);
  23. this.handleOnSubmit = this.handleOnSubmit.bind(this);
  24. }
  25.  
  26. componentDidMount() {
  27. const ifLoggedIn = this.props.checkIfUserLoggedInAction();
  28. if (ifLoggedIn) {
  29. browserHistory.replace({
  30. pathname: '/dashboard'
  31. });
  32. }
  33. }
  34.  
  35. handleOnChange(e) {
  36. const { name, value } = e.target;
  37. this.setState({
  38. [name]: value,
  39. });
  40. }
  41.  
  42. handleOnSubmit(e) {
  43. e.preventDefault();
  44. const { username, password } = this.state;
  45. const data = {
  46. username,
  47. password
  48. };
  49.  
  50. this.props.doLoginAction(data);
  51. }
  52.  
  53. render() {
  54. const { username, password } = this.state;
  55.  
  56. return (
  57. <div className="login-form">
  58. <form onSubmit={this.handleOnSubmit}>
  59. <div className="form-group">
  60. <input type="text" name="username" id="username" value={username} onChange={this.handleOnChange} />
  61. </div>
  62. <div className="form-group">
  63. <input type="text" name="password" id="password" value={password} onChange={this.handleOnChange} />
  64. </div>
  65. <div className="form-actioon">
  66. <input type="submit" value="Login" />
  67. </div>
  68. </form>
  69. </div>
  70. )
  71. }
  72.  
  73. export default connect(() => {}, {
  74. checkIfUserLoggedInAction,
  75. doLoginAction,
  76. })(Login)
Add Comment
Please, Sign In to add comment