'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapDispatchToProps = exports.mapStateToProps = exports.ModerateSession = 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;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

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

var _presenter2 = _interopRequireDefault(_presenter);

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

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

var _quizSessionEvents = require('../../../api/quizSessionEvents');

var _quizSessions2 = require('../../../../common/actions/quizSessions');

var _stateHelper = require('../../../../common/util/stateHelper');

var _stateHelper2 = _interopRequireDefault(_stateHelper);

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

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

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

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

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


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

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

  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var quizSessionId = this.props.quizSessionId;
      var prevQuizSessionId = prevProps.quizSessionId;


      if (quizSessionId !== prevQuizSessionId) this.fetchData();
    }
  }, {
    key: 'fetchData',
    value: function fetchData() {
      var _props = this.props,
          getQuizSession = _props.getQuizSession,
          getQuizSessionEvents = _props.getQuizSessionEvents,
          getSessionItems = _props.getSessionItems,
          quizSessionId = _props.quizSessionId;


      getQuizSession(quizSessionId);
      getQuizSessionEvents(quizSessionId);
      // NOTE: we fetch sessionItems so that we can render user responses
      getSessionItems(quizSessionId);
    }

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

  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(_presenter2.default, this.props);
    }
  }]);
  ModerateSession.displayName = 'ModerateSession'
  ;
  return ModerateSession;
}(_react.Component), _class.propTypes = {
  getQuizSession: _propTypes2.default.func.isRequired,
  getQuizSessionEvents: _propTypes2.default.func.isRequired,
  getSessionItems: _propTypes2.default.func.isRequired,
  quizSessionId: _propTypes2.default.string.isRequired }, _temp);
var mapStateToProps = exports.mapStateToProps = function mapStateToProps(state, ownProps) {
  var quizSessionId = ownProps.quizSessionId;

  var quizSession = _stateHelper2.default.findQuizSession(quizSessionId);
  var quizSessionEvents = quizSession && quizSession.getEvents() || (0, _immutable.List)();

  var isFetching = [state.getIn(['calls', _constants.QUIZ_SESSION_FETCH_CALL, 'requestStatus']), state.getIn(['calls', _constants.QUIZ_SESSION_EVENTS_FETCH_CALL, 'requestStatus']), state.getIn(['calls', _constants.SESSION_ITEMS_FETCH_CALL, 'requestStatus'])].includes('calling') || state.get('sessionItems').size === 0;

  return {
    isFetching: isFetching,
    quizSessionEvents: quizSessionEvents
  };
};

var mapDispatchToProps = exports.mapDispatchToProps = {
  getQuizSession: _quizSessions.getQuizSession, getQuizSessionEvents: _quizSessionEvents.getQuizSessionEvents, getSessionItems: _quizSessions2.getSessionItems
};

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