{"size": 2184, "ext": "js", "max_stars_repo_path": "assets/app/EmailLoginComponent/components/EmailLoginButtonComponent.js", "max_stars_repo_name": "mrdrewgurley/nopass-sails-react-webpack-seed", "max_stars_repo_stars_event_min_datetime": "2018-06-20T17:47:36.000Z", "max_stars_repo_stars_event_max_datetime": "2018-06-27T14:43:49.000Z", "max_issues_repo_path": "assets/app/EmailLoginComponent/components/EmailLoginButtonComponent.js", "max_issues_repo_name": "mrdrewgurley/nopass-sails-react-webpack-seed", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "assets/app/EmailLoginComponent/components/EmailLoginButtonComponent.js", "max_forks_repo_name": "mrdrewgurley/nopass-sails-react-webpack-seed", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.3, "max_line_length": 122, "alphanum_fraction": 0.6039377289}
import axios from 'axios'
import classNames from 'classnames'
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'

/**
 * EmailLoginButtonComponent
 * @desc React component for the Email Login Button
 */
export default class EmailLoginButtonComponent extends PureComponent {
  /**
   * requestAccess
   * @desc sends an ajax request to the auth controller
   */
  requestAccess = () => {
    var that = this
    axios
      .put('/auth/request', {
        email: document.getElementById('form-input-email').value
      })
      .then(res => {
        if (res.status == 200) {
          that.props.Actions.handshakeSuccess()
          window.uikit.notification({
            message: "<span uk-icon='icon: info'></span><span class='text'>Success, please check your email inbox</span>",
            status: 'success'
          })
          document.getElementById('EmailLoginInput').remove();
        }
      })
      .catch(function (err) {
        if (err.response.status == 400) {
          that.props.Actions.handshakeFailure()
          err.response.data.forEach(message => {
            window.uikit.notification({
              message: "<span uk-icon='icon: warning'></span><span class='text'>" + message + "</span>",
              status: 'danger'
            })
          })
        }
      })
  }

  /**
   * handleClick
   * @desc Actions to be undertaken when the login button is clicked
   */
  handleClick = (e) => {
    e.preventDefault()
    this.props.Actions.attemptHandshake()
    this.requestAccess()
  }

  /**
   * render
   * @desc renders the default login button code
   */
  render () {
    return (
      <div className="uk-button-group">
        <button
          className={classNames('uk-button', this.props.EmailLoginButtonReducer.class)}
          onClick={(e) => this.handleClick(e)}
          disabled={!this.props.EmailLoginButtonReducer.state}
        >
          {this.props.EmailLoginButtonReducer.label}
        </button>
      </div>
    )
  }
}

/**
 * PropTypes Definitions
 */
EmailLoginButtonComponent.propTypes = {
  Actions: PropTypes.object.isRequired,
  EmailLoginButtonReducer: PropTypes.object.isRequired
}
