Guest User

Untitled

a guest
Apr 19th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import Paper from 'material-ui/Paper';
  3. import TextField from 'material-ui/TextField';
  4. import RaisedButton from 'material-ui/RaisedButton'
  5. import CircularProgress from 'material-ui/CircularProgress'
  6. import { Link } from 'react-router'
  7. import { connect } from 'react-redux'
  8. import { userActions } from './../../actions/userActions'
  9.  
  10. import './LoginComponent.css'
  11.  
  12. class LoginComponent extends Component {
  13. constructor (props) {
  14. super(props)
  15. // bind the methods to this component
  16. this.changeUsername = this.changeUsername.bind(this)
  17. this.changePassword = this.changePassword.bind(this)
  18. this.handleSigninSubmit = this.handleSigninSubmit.bind(this)
  19.  
  20. // set initial state
  21. this.state = {
  22. username: '',
  23. password: '',
  24. signup: false,
  25. }
  26. }
  27.  
  28. changeUsername (e) {
  29. this.setState({ username: e.target.value })
  30. }
  31.  
  32. changePassword (e) {
  33. this.setState({ password: e.target.value })
  34. }
  35.  
  36. handleSigninSubmit (e) {
  37. e.preventDefault()
  38. // redux part. Calls the login method with parameters as username and password
  39. const { dispatch } = this.props;
  40. dispatch(userActions.login(this.state.username, this.state.password));
  41. }
  42.  
  43. render () {
  44. return (
  45. <div style={{display: 'flex', alignItems: 'space-between'}}>
  46. <form onSubmit={this.handleSigninSubmit}>
  47. <h2 className="paperTitle">Sign In</h2>
  48. <div className="fieldsContainer">
  49. <TextField hintText="Email" value={this.state.username} onChange={this.changeUsername} required />
  50. <TextField type="password" hintText="Password" value={this.state.password} onChange={this.changePassword} required />
  51.  
  52. <Link to="/auth/forgot" style={{fontSize: 12, float: 'right', textDecoration: 'underline'}}>Forgot Password?</Link> <br /><br />
  53.  
  54. <RaisedButton className="submitButton" type="submit" label="Sign In" primary={true} disabled={this.props.logging} />
  55.  
  56. <CircularProgress style={{position: 'absolute', padding: '27px 5px'}} size={20} hidden={!this.props.logging}/>
  57. </div>
  58.  
  59. {this.props.error && <p>{this.props.error.message}</p>}
  60.  
  61. <br /><br /><br />Don't have an account yet? <Link className="bottomAlign" to="/auth/signup">Signup here</Link>
  62. </form>
  63. </div>
  64. )
  65. }
  66. }
  67.  
  68. // map redux global state to the props of this component
  69. function mapStateToProps(state) {
  70. const { logging, user, error } = state.authentication
  71. return {
  72. logging,
  73. user,
  74. error
  75. }
  76. }
  77.  
  78. // connect this component to be able to access the global redux store
  79. export default connect(mapStateToProps)(LoginComponent)
Add Comment
Please, Sign In to add comment