Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import Modal from 'react-bootstrap/Modal';
  3. import Button from 'react-bootstrap/Button';
  4. import { connect } from 'react-redux';
  5. import { login } from '../../store/actions/auth';
  6. import { validateEmail } from '../../util/validation';
  7. import history from '../../history';
  8.  
  9. const INIT_STATE = {
  10. email: '',
  11. password: '',
  12. emailError: '',
  13. passwordError: '',
  14. }
  15.  
  16. class LoginDialog extends Component {
  17.  
  18. constructor(props) {
  19. super(props);
  20. this.state = INIT_STATE;
  21. }
  22.  
  23. submitForm = event => {
  24. event.preventDefault();
  25. const isValid = this.validateInput();
  26. if (isValid) {
  27. const user = {
  28. email: this.state.email,
  29. password: this.state.password
  30. }
  31. this.props.login(user);
  32. this.setState(INIT_STATE, () => {
  33. this.props.toggle();
  34. history.push('/');
  35. });
  36. }
  37. }
  38.  
  39. handleChange = event => {
  40. this.setState({
  41. [event.target.name]: event.target.value
  42. });
  43. }
  44.  
  45. validateInput = () => {
  46. var emailError = '';
  47. var passwordError = '';
  48.  
  49. // password check
  50. if (this.state.password === '') {
  51. passwordError = `Password shouldn't be empty`;
  52. } else if (this.state.password.length < 6) {
  53. passwordError = `Password should containt at least 6 characters`;
  54. }
  55.  
  56. // email check
  57. if (this.state.email === '') {
  58. emailError = `Email shouldn't be empty`;
  59. } else if(!validateEmail(this.state.email)) {
  60. emailError = `Wrong format of email address`;
  61. }
  62.  
  63. if(emailError !== '' || passwordError !== '') {
  64. this.setState({
  65. emailError,
  66. passwordError
  67. }, () => {
  68. return false;
  69. });
  70. } else {
  71. return true;
  72. }
  73.  
  74. }
  75.  
  76. render() {
  77. return (
  78. <Modal show={this.props.show} onHide={this.props.toggle}>
  79. <Modal.Header closeButton>
  80. <Modal.Title>Login</Modal.Title>
  81. </Modal.Header>
  82. <Modal.Body>
  83. <form>
  84. <div className="form-group">
  85. <label htmlFor="emailInput">Email address</label>
  86. <input type="email" className="form-control" id="emailInput" value={this.state.email} onChange={this.handleChange} name="email" aria-describedby="emailError" placeholder="Enter email" />
  87. <small id="emailError" className="form-text text-muted">{this.state.emailError}</small>
  88. </div>
  89. <div className="form-group">
  90. <label htmlFor="passwordInput">Password</label>
  91. <input type="password" className="form-control" id="passwordInput" value={this.state.password} onChange={this.handleChange} name="password" aria-describedby="passwordError" placeholder="Password" />
  92. <small id="passwordError" className="form-text text-muted">{this.state.passwordError}</small>
  93. </div>
  94. <Button className="btn btn-block" type="submit" variant="success" onClick={this.submitForm}>Login</Button>
  95. </form>
  96. </Modal.Body>
  97. </Modal>
  98. );
  99. }
  100. }
  101.  
  102. export default connect(null, { login })(LoginDialog);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement