Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @flow
  2. // Third party.
  3. import React, { Component } from 'react'
  4. import { bindActionCreators } from 'redux'
  5. import { connect } from 'react-redux'
  6.  
  7. // Session store.
  8. import type { SessionState } from '../../../constants/types'
  9. import { logIn } from '../../../store/session'
  10.  
  11. // Login component.
  12. import Login from './Login'
  13.  
  14. // Login Container.
  15. class LoginContainer extends Component {
  16.   props: {
  17.     session: SessionState,
  18.     logIn: Function
  19.   }
  20.  
  21.   state: {
  22.     username: string,
  23.     password: string
  24.   }
  25.  
  26.   constructor (props) {
  27.     super(props)
  28.     this.state = {
  29.       username: '',
  30.       password: ''
  31.     }
  32.   }
  33.  
  34.   render () {
  35.     return (
  36.       <Login
  37.         disabled={this._isFormDisabled()}
  38.         idle={this.props.session.idle}
  39.         error={this.props.session.error}
  40.         onUsernameChange={this._handleUsernameChange}
  41.         onPasswordChange={this._handlePasswordChange}
  42.         onSubmit={this._handleFormSubmit} />
  43.     )
  44.   }
  45.  
  46.   _isFormDisabled = () => {
  47.     return !(this.state.username.trim().length && this.state.password.trim().length)
  48.   }
  49.  
  50.   _handleUsernameChange = ({ target: { value } }) => {
  51.     this.setState({ username: value })
  52.   }
  53.  
  54.   _handlePasswordChange = ({ target: { value } }) => {
  55.     this.setState({ password: value })
  56.   }
  57.  
  58.   _handleFormSubmit = (e) => {
  59.     e.preventDefault()
  60.     this.props.logIn(this.state.username, this.state.password)
  61.   }
  62. }
  63.  
  64. // Map state and actions.
  65. const mapStateToProps = ({ session }) => ({ session })
  66. const mapDispatchToProps = (dispatch) => bindActionCreators({ logIn }, dispatch)
  67.  
  68. export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement