Guest User

Untitled

a guest
May 25th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { login } from '../../redux/reducer';
  4. import './LoginForm.css';
  5. import './UserPage';
  6. import avatar from '../../avatar.png';
  7. import Back from '../../Back.js';
  8. import {withRouter,BrowserRouter as Link } from 'react-router-dom';
  9.  
  10. class LoginForm extends Component {
  11.  
  12.  
  13. constructor(props) {
  14. super(props);
  15. this.state = {};
  16. this.onSubmit = this.onSubmit.bind(this);
  17. }
  18.  
  19. componentWillReceiveProps(nextProps) {
  20. if (!nextProps.isLoginPending && this.props.isLoginPending) {
  21. if (nextProps.isLoginSuccess) {
  22. this.props.history.push('/UserPage')
  23. } else {
  24. console.log(this.props.loginError)
  25. }
  26. }
  27. }
  28.  
  29. render() {
  30. let {username, password} = this.state;
  31. let {isLoginPending, isLoginSuccess, loginError} = this.props;
  32.  
  33. return (
  34. <div>
  35. <Back/>
  36. <header>
  37. <h1 className="log">Company Login</h1>
  38. </header>
  39.  
  40. <form name="loginForm" onSubmit={this.onSubmit}>
  41.  
  42. <div className="imgcontainer">
  43. <img src={avatar} alt={"Avatar"} className={"avatar"}/>
  44. </div>
  45.  
  46. <div className="form-group-collection">
  47. <div className="form-group">
  48.  
  49. <label>Username/User ID:</label>
  50. <input name="username" onChange={e => this.setState({username: e.target.value})} value={username}/>
  51. </div>
  52.  
  53.  
  54. <div className="form-group">
  55. <label>Password:</label>
  56. <input type="password" name="password" onChange={e => this.setState({password: e.target.value})} value={password}/>
  57. </div>
  58. </div>
  59. <br/>
  60.  
  61. <input type="submit" value="Login" />
  62.  
  63.  
  64. </form>
  65.  
  66.  
  67.  
  68. </div>
  69. )
  70. }
  71.  
  72.  
  73. onSubmit(e) {
  74. e.preventDefault();
  75. let { username, password } = this.state;
  76. this.props.login(username, password);
  77. this.setState({
  78. username: '',
  79. password: ''
  80. });
  81. }
  82. }
  83.  
  84.  
  85. const mapStateToProps = (state) => {
  86. return {
  87. isLoginPending: state.isLoginPending,
  88. isLoginSuccess: state.isLoginSuccess,
  89. loginError: state.loginError
  90. };
  91. }
  92.  
  93. const mapDispatchToProps = (dispatch) => {
  94. return {
  95. login: (username, password) => dispatch(login(username, password))
  96. };
  97. }
  98. LoginForm = withRouter(LoginForm);
  99.  
  100. export default connect(mapStateToProps, mapDispatchToProps)(LoginForm);
Add Comment
Please, Sign In to add comment