"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getExistingQuiz = getExistingQuiz;
exports.getExistingQuizAndItems = getExistingQuizAndItems;
exports.quizFetchHandler = void 0;

var _Fetcher = _interopRequireDefault(require("../util/Fetcher"));

var _quizzes = require("../actions/quizzes");

var _callHandlers = require("./callHandlers");

var _interactionTypes = require("./interactionTypes");

var _quizEntries = require("../../building/api/quizEntries");

// ==== CALLBACKS ====
var quizFetchHandler = function quizFetchHandler(dispatch, quiz) {
  dispatch([(0, _callHandlers.handleQuizResponse)(quiz), (0, _quizzes.newActiveQuiz)("".concat(quiz.id))]);
}; // ====================================
// fetch the data for an existing quiz
// and add it as the active quiz
// ====================================


exports.quizFetchHandler = quizFetchHandler;

function getExistingQuiz(quizId) {
  var quizUrl = "/api/quizzes/".concat(quizId);
  return function (dispatch, getState) {
    var fetcher = new _Fetcher.default();
    return fetcher.get(quizUrl).then(function (quizData) {
      return quizFetchHandler(dispatch, quizData);
    });
  };
}

function getExistingQuizAndItems(quizId) {
  var quizUrl = "/api/quizzes/".concat(quizId);
  return function (dispatch, getState) {
    var fetcher = new _Fetcher.default();
    return fetcher.get(quizUrl).then(function (quizData) {
      return quizFetchHandler(dispatch, quizData);
    }).then(function () {
      return dispatch((0, _quizEntries.getQuizEntries)(quizId));
    }).then(function () {
      return dispatch((0, _interactionTypes.interactionTypeFetch)());
    });
  };
}