Advertisement
Guest User

Untitled

a guest
Jun 21st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import React, {Component, PropTypes} from 'react';
  4. import {connect} from 'react-redux';
  5.  
  6. import {changeRoute} from '../../common/actions/navigation.action';
  7. import {login, updateLoginField} from '../../common/actions/user.action';
  8. import {BG_IMAGES} from '../../common/constants/backgrounds';
  9. import Login from '../../components/Login';
  10.  
  11. class StartScreen extends Component {
  12. constructor(props) {
  13. super(props);
  14. this._handleLogin = this._handleLogin.bind(this);
  15. this._handleLoginFieldChange = this._handleLoginFieldChange.bind(this);
  16. }
  17.  
  18. componentWillReceiveProps(nextProps) {
  19. var user_reducer = nextProps.user_reducer;
  20. if (user_reducer.error) {
  21. this._showSnackBar(user_reducer.error.message);
  22. return;
  23. }
  24.  
  25. if (user_reducer.profile && user_reducer.profile.user.status === 'authenticated') {
  26. this.props.dispatch(changeRoute('/dashboard'));
  27. }
  28. }
  29.  
  30. componentDidMount() {
  31. this._setBackGround();
  32. componentHandler.upgradeDom();
  33. }
  34.  
  35. _handleLogin() {
  36. var login_profile = this.props.user_reducer.login_profile;
  37. if (!login_profile.username || !login_profile.password || !login_profile.phone_ext) {
  38. this._showSnackBar('Username, Password, and Phone Extension are required!');
  39. return;
  40. }
  41.  
  42. this.props.dispatch(login(login_profile.username, login_profile.password, login_profile.phone_ext));
  43. }
  44.  
  45. _handleLoginFieldChange(field, event) {
  46. this.props.dispatch(updateLoginField(field, event.target.value));
  47. }
  48.  
  49. _setBackGround() {
  50. function getRandomBGPhoto() {
  51. var photo_number = Math.floor(Math.random() * BG_IMAGES.length);
  52. return BG_IMAGES[photo_number];
  53. }
  54.  
  55. let login_container = document.getElementById('login-container');
  56. login_container.style.background = 'url(' + getRandomBGPhoto() + ') top center no-repeat';
  57. login_container.style.backgroundSize = 'cover';
  58. }
  59.  
  60. _showSnackBar(message) {
  61. var data = {
  62. message: message,
  63. timeout: 3500
  64. };
  65. var snackbarContainer = document.querySelector('#login-snack-bar');
  66. snackbarContainer.MaterialSnackbar.showSnackbar(data);
  67. }
  68.  
  69. render() {
  70. const {user_reducer} = this.props;
  71.  
  72. return (
  73. <div>
  74. <Login hotKeyMap={user_reducer.key_map}
  75. loading={user_reducer.loading}
  76. username={user_reducer.login_profile.username}
  77. password={user_reducer.login_profile.password}
  78. phone_ext={user_reducer.login_profile.phone_ext}
  79. handleLoginFieldChange={this._handleLoginFieldChange}
  80. handleLogin={this._handleLogin}/>
  81. </div>
  82. );
  83. }
  84. }
  85.  
  86. StartScreen.propTypes = {
  87. dispatch: PropTypes.func.isRequired
  88. };
  89.  
  90. function mapStateToProps(state) {
  91. const {user_reducer} = state;
  92. return {
  93. user_reducer
  94. };
  95. }
  96.  
  97. export default connect(mapStateToProps)(StartScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement