Guest User

Untitled

a guest
Sep 20th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import PropTypes from 'prop-types'
  3. import { connect } from 'react-redux'
  4. import queryString from 'query-string'
  5. import { Helmet } from 'react-helmet'
  6. import Particles from 'react-particles-js'
  7.  
  8. import { NetworkOperation } from 'lib'
  9. import { setCredentials } from 'actions'
  10. import * as particleParams from 'lib/particlesjs-config.json'
  11. import logo from 'assets/logo.svg'
  12.  
  13. class Login extends Component {
  14. constructor(props) {
  15. super(props)
  16.  
  17. const { return: returnUrl = null } = queryString.parse(location.search)
  18.  
  19. this.state = {
  20. email: '',
  21. password: '',
  22. return: returnUrl,
  23. error: null
  24. }
  25.  
  26. this.onSubmit = this.onSubmit.bind(this)
  27. this.onChange = this.onChange.bind(this)
  28. }
  29.  
  30. onChange(event) {
  31. const { value, name } = event.target
  32.  
  33. this.setState({
  34. [name]: value,
  35. error: null
  36. })
  37. }
  38.  
  39. onSubmit(event) {
  40. event.preventDefault()
  41.  
  42. const { email, password } = this.state
  43.  
  44. NetworkOperation.login({ email, password })
  45. .then(({ data }) => {
  46. localStorage.setItem('token', data.token)
  47.  
  48. this.props.setCredentials({ ...data.user, token: data.token })
  49.  
  50. this.props.history.replace(this.state.return || '/')
  51. })
  52. .catch(({ response = {} }) => {
  53. const { status = 500 } = response
  54. switch (status) {
  55. case 400:
  56. case 401:
  57. this.setState({
  58. error: 'Correo o contraseña incorrectos'
  59. })
  60. break
  61. default:
  62. this.setState({
  63. error: 'Problemas al iniciar sesión, intenta nuevamente'
  64. })
  65. }
  66. })
  67. }
  68.  
  69. render() {
  70. const { state } = this
  71. return (
  72. <div className="login">
  73. <Helmet>
  74. <title>Connus | Login</title>
  75. </Helmet>
  76. <Particles
  77. style={{
  78. width: '100%',
  79. height: '100%',
  80. position: 'fixed',
  81. top: 0,
  82. left: 0,
  83. right: 0,
  84. bottom: 0,
  85. zIndex: -1
  86. }}
  87. params={particleParams}
  88. />
  89. <div id="logo">
  90. <img src={logo} alt="Connus" className="iso" />
  91. </div>
  92. <form onSubmit={this.onSubmit}>
  93. <input
  94. type="text"
  95. name="email"
  96. onChange={this.onChange}
  97. value={state.email}
  98. placeholder="Usuario"
  99. required
  100. />
  101. <input
  102. type="password"
  103. name="password"
  104. onChange={this.onChange}
  105. value={state.password}
  106. placeholder="Contraseña"
  107. required
  108. />
  109. <input
  110. type="submit"
  111. value="Iniciar sesión"
  112. className={`button action ${(!state.email || !state.password) &&
  113. 'disabled'}`}
  114. />
  115. {state.error && (
  116. <div className="error">
  117. <p>{state.error}</p>
  118. </div>
  119. )}
  120. </form>
  121. </div>
  122. )
  123. }
  124. }
  125.  
  126. Login.propTypes = {
  127. setCredentials: PropTypes.func,
  128. history: PropTypes.object
  129. }
  130.  
  131. function mapDispatchToProps(dispatch) {
  132. return {
  133. setCredentials: user => {
  134. dispatch(setCredentials(user))
  135. }
  136. }
  137. }
  138.  
  139. export default connect(null, mapDispatchToProps)(Login)
Add Comment
Please, Sign In to add comment