'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactIntl = require('react-intl');

var _cspaceInput = require('cspace-input');

var _LoginForm = require('../../../styles/cspace-ui/LoginForm.css');

var _LoginForm2 = _interopRequireDefault(_LoginForm);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Button = _cspaceInput.components.Button,
    LineInput = _cspaceInput.components.LineInput,
    PasswordInput = _cspaceInput.components.PasswordInput;


var messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'loginForm.title',
    'defaultMessage': 'Sign In'
  },
  prompt: {
    'id': 'loginForm.prompt',
    'defaultMessage': 'Please sign in to continue.'
  },
  pending: {
    'id': 'loginForm.pending',
    'defaultMessage': 'Signing in...'
  },
  success: {
    'id': 'loginForm.success',
    'defaultMessage': 'Sign in complete.'
  },
  error: {
    'id': 'loginForm.error',
    'defaultMessage': 'Sign in failed.'
  },
  badCredentialsError: {
    'id': 'loginForm.error.badCredentials',
    'defaultMessage': 'Incorrect username/password. Please try again.'
  },
  username: {
    'id': 'loginForm.label.username',
    'defaultMessage': 'Email'
  },
  password: {
    'id': 'loginForm.label.password',
    'defaultMessage': 'Password'
  },
  submit: {
    'id': 'loginForm.label.submit',
    'defaultMessage': 'Sign in'
  }
});

/**
 * Map client error descriptions to keys in the above messages object.
 */
var errorMessageMap = {
  'Bad credentials': 'badCredentialsError'
};

var LoginForm = function (_Component) {
  _inherits(LoginForm, _Component);

  function LoginForm(props) {
    _classCallCheck(this, LoginForm);

    var _this = _possibleConstructorReturn(this, (LoginForm.__proto__ || Object.getPrototypeOf(LoginForm)).call(this, props));

    _this.handleSubmit = _this.handleSubmit.bind(_this);
    return _this;
  }

  _createClass(LoginForm, [{
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var _props = this.props,
          onSuccess = _props.onSuccess,
          isPending = _props.isPending,
          response = _props.response;


      if (onSuccess) {
        var isSuccess = prevProps.isPending && !isPending && response;

        if (isSuccess) {
          onSuccess();
        }
      }
    }
  }, {
    key: 'handleSubmit',
    value: function handleSubmit(event) {
      event.preventDefault();

      var onSubmit = this.props.onSubmit;


      if (onSubmit) {
        var form = event.target;
        var username = form.username.value;
        var password = form.password.value;

        onSubmit(username, password);
      }
    }
  }, {
    key: 'renderMessage',
    value: function renderMessage() {
      var _props2 = this.props,
          isPending = _props2.isPending,
          response = _props2.response,
          error = _props2.error;


      var messageKey = 'prompt';

      if (isPending) {
        messageKey = 'pending';
      } else if (response) {
        messageKey = 'success';
      } else if (error) {
        messageKey = 'error';

        if (error.response && error.response.data) {
          var desc = error.response.data.error_description;
          messageKey = errorMessageMap[desc];
        }
      }

      return _react2.default.createElement(
        'p',
        null,
        _react2.default.createElement(_reactIntl.FormattedMessage, messages[messageKey])
      );
    }
  }, {
    key: 'renderForm',
    value: function renderForm() {
      var _props3 = this.props,
          intl = _props3.intl,
          isPending = _props3.isPending,
          username = _props3.username;


      if (isPending) {
        return null;
      }

      return _react2.default.createElement(
        'form',
        { onSubmit: this.handleSubmit },
        _react2.default.createElement(LineInput, {
          name: 'username',
          placeholder: intl.formatMessage(messages.username),
          type: 'text',
          value: username
        }),
        _react2.default.createElement(PasswordInput, {
          name: 'password',
          placeholder: intl.formatMessage(messages.password),
          type: 'password'
        }),
        _react2.default.createElement(
          Button,
          { type: 'submit' },
          _react2.default.createElement(_reactIntl.FormattedMessage, messages.submit)
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { className: _LoginForm2.default.common },
        _react2.default.createElement(
          'h2',
          null,
          _react2.default.createElement(_reactIntl.FormattedMessage, messages.title)
        ),
        this.renderMessage(),
        this.renderForm()
      );
    }
  }]);

  return LoginForm;
}(_react.Component);

LoginForm.propTypes = {
  intl: _react.PropTypes.object.isRequired,
  isPending: _react.PropTypes.bool,
  username: _react.PropTypes.string,
  response: _react.PropTypes.object,
  error: _react.PropTypes.object,
  onSubmit: _react.PropTypes.func,
  onSuccess: _react.PropTypes.func
};

exports.default = (0, _reactIntl.injectIntl)(LoginForm);