'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 _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

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 messages = (0, _reactIntl.defineMessages)({
  pending: {
    'id': 'logoutIndicator.pending',
    'defaultMessage': 'Signing out...'
  },
  success: {
    'id': 'logoutIndicator.success',
    'defaultMessage': 'Success!'
  }
});

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

  function LogoutIndicator() {
    _classCallCheck(this, LogoutIndicator);

    return _possibleConstructorReturn(this, (LogoutIndicator.__proto__ || Object.getPrototypeOf(LogoutIndicator)).apply(this, arguments));
  }

  _createClass(LogoutIndicator, [{
    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: 'renderPending',
    value: function renderPending() {
      var isPending = this.props.isPending;


      if (!isPending) {
        return null;
      }

      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_reactIntl.FormattedMessage, messages.pending)
      );
    }
  }, {
    key: 'renderSuccess',
    value: function renderSuccess() {
      var response = this.props.response;


      if (!response) {
        return null;
      }

      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          'h2',
          null,
          _react2.default.createElement('br', null)
        ),
        _react2.default.createElement(
          'p',
          null,
          _react2.default.createElement(_reactIntl.FormattedMessage, messages.success)
        )
      );
    }
  }, {
    key: 'renderError',
    value: function renderError() {
      var error = this.props.error;


      if (!error) {
        return null;
      }

      return _react2.default.createElement(
        'div',
        null,
        error.message
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return this.renderPending() || this.renderSuccess() || this.renderError();
    }
  }]);

  return LogoutIndicator;
}(_react.Component);

exports.default = LogoutIndicator;


LogoutIndicator.propTypes = {
  isPending: _propTypes2.default.bool,
  response: _propTypes2.default.object,
  error: _propTypes2.default.object,
  onSuccess: _propTypes2.default.func
};