Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import { bindActionCreators } from 'redux'
  3. import { connect } from 'react-redux'
  4. import { createStructuredSelector } from 'reselect'
  5. import { withRouter } from 'react-router'
  6.  
  7. import * as actions from './actions'
  8. import selectors from './selectors'
  9.  
  10. import { Button } from 'components/button'
  11. import { Input } from 'components/input'
  12. import { View } from 'components/layout'
  13. import { Text } from 'components/text'
  14.  
  15. export class Auth extends Component {
  16.  
  17. constructor() {
  18.  
  19. super()
  20.  
  21. this.onPasswordKeyUp = this.onPasswordKeyUp.bind(this)
  22.  
  23. }
  24.  
  25. componentWillMount() {
  26.  
  27. const { authorised } = this.props
  28.  
  29. this.redirectToIndex(authorised)
  30.  
  31. }
  32.  
  33. componentWillUpdate(nextProps) {
  34.  
  35. const { authorised } = nextProps
  36.  
  37. this.redirectToIndex(authorised)
  38.  
  39. }
  40.  
  41. redirectToIndex(authorised) {
  42.  
  43. const { router } = this.props
  44.  
  45. // redirect authorised users to `/`
  46. if ( authorised ) return router.push('/')
  47.  
  48. }
  49.  
  50. onPasswordKeyUp(e) {
  51.  
  52. const { login } = this.props.actions
  53.  
  54. const key = e.keyCode
  55.  
  56. // submit login form if `Enter` key pressed
  57. if ( key === 13 ) return login({ username: this.email.value, password: this.password.value })
  58.  
  59. }
  60.  
  61. render() {
  62.  
  63. const onPasswordKeyUp = this.onPasswordKeyUp
  64.  
  65. const { login } = this.props.actions
  66.  
  67. return (
  68. <View>
  69.  
  70. <Text atomic={{ fs:6, fw:'b', ta:'c' }} color='primary'>
  71. Rooftop
  72. </Text>
  73.  
  74. <Text atomic={{ ta:'c' }}>
  75. a good place for an overview
  76. </Text>
  77.  
  78. <Input type='email' placeholder='Email' innerRef={ r => this.email = r } />
  79.  
  80. <Input type='password' placeholder='Password' innerRef={ r => this.password = r } onKeyUp={ onPasswordKeyUp } />
  81.  
  82. <Button atomic={{ mt:7 }} onClick={ () => login({ username: this.email.value, password: this.password.value }) }>
  83. Login
  84. </Button>
  85.  
  86. </View>
  87. )
  88.  
  89. }
  90.  
  91. }
  92.  
  93. export default withRouter(connect(
  94. createStructuredSelector({ ...selectors }),
  95. dispatch => ({
  96. actions: bindActionCreators(actions, dispatch)
  97. })
  98. )(Auth))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement