"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapStateToProps = mapStateToProps;
exports.default = exports.mapDispatchToProps = exports.ModerateTable = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _constants = require("@instructure/quiz-common/lib/constants");

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

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

var _presenter = _interopRequireDefault(require("./presenter"));

var ModerateTable =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(ModerateTable, _Component);

  function ModerateTable() {
    (0, _classCallCheck2.default)(this, ModerateTable);
    return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(ModerateTable).apply(this, arguments));
  }

  (0, _createClass2.default)(ModerateTable, [{
    key: "componentDidMount",
    // ===========
    //   CONFIG
    // ===========
    value: function componentDidMount() {
      this.fetchData();
    }
  }, {
    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 _this$props = this.props,
          getQuizSessions = _this$props.getQuizSessions,
          quizId = _this$props.quizId,
          tableData = _this$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 _react.default.createElement(_presenter.default, this.props);
    }
  }]);
  ModerateTable.displayName = "ModerateTable";
  return ModerateTable;
}(_react.Component);

exports.ModerateTable = ModerateTable;
ModerateTable.propTypes = (0, _objectSpread2.default)({}, _presenter.default.propTypes, {
  getQuizSessions: _propTypes.default.func.isRequired,
  quizId: _propTypes.default.string.isRequired // =============
  //   LIFECYCLE
  // =============

});

function mapStateToProps(state, ownProps) {
  var isFetching = state.getIn(['calls', _constants.QUIZ_SESSIONS_FETCH_CALL, 'requestStatus']) === 'calling';
  return {
    isFetching: isFetching,
    hasSessionTimeLimit: state.getIn(['quizzes', ownProps.quizId, 'hasTimeLimit']),
    sessionTimeLimitInSeconds: state.getIn(['quizzes', ownProps.quizId, 'sessionTimeLimitInSeconds'])
  };
}

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

var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(ModerateTable);

exports.default = _default;