Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. // Libraries
  2. import { connect } from 'react-redux'
  3. import React, { PureComponent, PropTypes } from 'react'
  4.  
  5. // Our actions
  6. import { loginAction, redirectedToLoginAction } from 'store/actions'
  7. import './index.css'
  8.  
  9. // Our components
  10. import { loginErrorToMessage } from '../../util'
  11.  
  12. class Login extends PureComponent {
  13. constructor () {
  14. super()
  15.  
  16. // Refs
  17. let { form, usernameInput, passwordInput } = this
  18.  
  19. // Validation
  20. this.state = {
  21. emailInvalid: false,
  22. passwordInvalid: false
  23. }
  24. }
  25.  
  26. static propTypes = {
  27. location: PropTypes.object.isRequired,
  28. hasBeenRedirected: PropTypes.func.isRequired,
  29. loginUser: PropTypes.func.isRequired,
  30. error: PropTypes.object,
  31. token: PropTypes.string
  32. }
  33.  
  34. static contextTypes = {
  35. router: PropTypes.object.isRequired
  36. }
  37.  
  38. componentDidMount () {
  39. if (this.props.location.state) {
  40. if (this.props.location.state.fromRegistration) {
  41. this.props.hasBeenRedirected()
  42. }
  43. }
  44.  
  45. if (this.props.token) {
  46. setImmediate(this.context.router.push('/dashboard'))
  47. }
  48. }
  49.  
  50. componentDidUpdate () {
  51. if (this.props.token) {
  52. setImmediate(this.context.router.push('/dashboard'))
  53. }
  54. }
  55.  
  56. errorHandling = () => {
  57. !this.emailInput.value ? this.setState({usernameInvalid: true}) : this.setState({usernameInvalid: false})
  58. !this.passwordInput.value ? this.setState({passwordInvalid: true}) : this.setState({passwordInvalid: false})
  59. }
  60.  
  61. handleClick = e => {
  62. e.preventDefault()
  63.  
  64. // Check validity
  65. if (!this.form.checkValidity()) {
  66. this.errorHandling()
  67. return
  68. }
  69.  
  70. // Set all input fields to valid
  71. this.setState({
  72. usernameInvalid: false,
  73. passwordInvalid: false
  74. })
  75.  
  76. this.props.loginUser({
  77. email: this.emailInput.value,
  78. password: this.passwordInput.value
  79. })
  80. }
  81.  
  82. render () {
  83. return (
  84. <form className='registerField' ref={f => { this.form = f }} >
  85. <label><strong>Login</strong> </label>
  86. <ul>
  87.  
  88. <li>
  89. <label>Email:</label>
  90. <input
  91. type='text'
  92. name='email'
  93. placeholder='Email'
  94. ref={i => { this.emailInput = i }}
  95. required
  96. />
  97. </li>
  98.  
  99. <li>
  100. <label>Password:</label>
  101. <input
  102. type='password'
  103. name='password'
  104. placeholder='Password'
  105. ref={i => { this.passwordInput = i }}
  106. />
  107. </li>
  108.  
  109. </ul>
  110.  
  111. <input type='submit' onClick={this.handleClick} />
  112. </form>
  113. )
  114. }
  115. }
  116.  
  117. const mapStateToProps = state => ({
  118. token: state.getIn(['auth', 'token']),
  119. error: state.getIn(['auth', 'error'])
  120. })
  121.  
  122. const mapDispatchToProps = (dispatch) => ({
  123. hasBeenRedirected: () => dispatch(redirectedToLoginAction()),
  124. loginUser: params => dispatch(loginAction(params))
  125. })
  126.  
  127. export default connect(mapStateToProps, mapDispatchToProps)(Login)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement