'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapDispatchToProps = exports.ModerateTable = undefined;

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _class, _temp;

exports.mapStateToProps = mapStateToProps;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactRedux = require('../../../../common/react-redux');

var _constants = require('../../../../constants');

var _quizSessions = require('../../../api/quizSessions');

var _moderation = require('../../../../common/actions/moderation');

var _presenter = require('./presenter');

var _presenter2 = _interopRequireDefault(_presenter);

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

var ModerateTable = exports.ModerateTable = (_temp = _class = function (_Component) {
  (0, _inherits3.default)(ModerateTable, _Component);

  function ModerateTable() {
    (0, _classCallCheck3.default)(this, ModerateTable);
    return (0, _possibleConstructorReturn3.default)(this, (ModerateTable.__proto__ || Object.getPrototypeOf(ModerateTable)).apply(this, arguments));
  }

  (0, _createClass3.default)(ModerateTable, [{
    key: 'componentDidMount',


    // =============
    //   LIFECYCLE
    // =============

    value: function componentDidMount() {
      this.fetchData();
    }
    // ===========
    //   CONFIG
    // ===========

  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var quizSessionIds = this.quizSessionIds(this.props.tableData);
      var prevQuizSessionIds = this.quizSessionIds(prevProps.tableData);

      var skipFetchData = quizSessionIds.every(function (id, index) {
        return id === prevQuizSessionIds[index];
      });
      if (skipFetchData) return;

      this.fetchData();
    }
  }, {
    key: 'quizSessionIds',
    value: function quizSessionIds(tableData) {
      return tableData.reduce(function (ids, _ref) {
        var quizSessionIds = _ref.quizSessionIds;
        return ids.concat(quizSessionIds);
      }, []).sort();
    }
  }, {
    key: 'fetchData',
    value: function fetchData() {
      var _props = this.props,
          getQuizSessions = _props.getQuizSessions,
          quizId = _props.quizId,
          tableData = _props.tableData;

      var quizSessionIds = this.quizSessionIds(tableData);

      // NOTE: The consumer provided no quizSessionIds. Don't fetch
      if (!quizSessionIds.length) return;

      getQuizSessions(quizId, { ids: quizSessionIds });
    }

    // =============
    //   RENDERING
    // =============

  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(_presenter2.default, this.props);
    }
  }]);
  ModerateTable.displayName = 'ModerateTable'
  ;
  return ModerateTable;
}(_react.Component), _class.propTypes = Object.assign({}, _presenter2.default.propTypes, {
  getQuizSessions: _propTypes2.default.func.isRequired,
  quizId: _propTypes2.default.string.isRequired }), _temp);
function mapStateToProps(state) {
  var isFetching = state.getIn(['calls', _constants.QUIZ_SESSIONS_FETCH_CALL, 'requestStatus']) === 'calling';

  return { isFetching: isFetching };
}

var mapDispatchToProps = exports.mapDispatchToProps = { getQuizSessions: _quizSessions.getQuizSessions, openTray: _moderation.openTray };

exports.default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(ModerateTable);