'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 _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

var _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

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 Avatar = function (_React$Component) {
  _inherits(Avatar, _React$Component);

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

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Avatar).call(this, props));

    _this.state = {
      loaded: false
    };

    _this.componentWillReceiveProps = function (nextProps) {
      if (nextProps.src !== _this.props.src) {
        _this.setState({
          loaded: false
        });
      }
    };

    _this.shouldComponentUpdate = function (nextProps, nextState) {
      return nextProps.src !== _this.props.src || nextProps.size !== _this.props.size || nextState.loaded !== _this.state.loaded;
    };

    _this.inlineStyle = function () {
      return {
        width: _this.props.size + 'px',
        height: _this.props.size + 'px'
      };
    };

    _this.handleLoad = function () {
      _this.setState({
        loaded: true
      });
    };

    _this.loadAvatar = function () {
      return _react2.default.createElement('img', { src: _this.props.src, onLoad: _this.handleLoad, alt: _this.props.alt, height: _this.props.size });
    };

    return _this;
  }

  _createClass(Avatar, [{
    key: 'render',
    value: function render() {
      var cx = _bind2.default.bind(_style2.default);
      var avatarClasses = cx(_style2.default['avatar-wrapper'], this.state.loaded ? 'loaded' : null, this.props.optClass);

      return _react2.default.createElement(
        'div',
        { className: avatarClasses, style: this.props.size ? this.inlineStyle() : null },
        this.loadAvatar()
      );
    }
  }]);

  return Avatar;
}(_react2.default.Component);

Avatar.propTypes = {
  /**
   * The source of the image to load.
   */
  src: _react2.default.PropTypes.string.isRequired,
  /**
   * Optional alt text for the image
   */
  alt: _react2.default.PropTypes.string,
  /**
   * Optional size to constrain the image (only supports square images)
   */
  size: _react2.default.PropTypes.string,
  /**
   * Optional CSS class to pass to the badge.
   */
  optClass: _react2.default.PropTypes.string
};
exports.default = Avatar;