@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
251 lines (250 loc) • 9.6 kB
JavaScript
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a 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);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
import update from 'immutability-helper';
import findIndex from 'lodash/findIndex';
import omit from 'lodash/omit';
import without from 'lodash/without';
var CategorizationPresenter = /*#__PURE__*/ function() {
"use strict";
function CategorizationPresenter(props) {
var _this = this;
_class_call_check(this, CategorizationPresenter);
// ===========
// ACTIONS - CATEGORIES
// ===========
_define_property(this, "onCategoryInputChange", function(itemId, e) {
var categories = update(_this.props.interactionData.categories, _define_property({}, itemId, {
itemBody: {
$set: e.target.value
}
}));
_this.updateInteractionData({
categories: categories
});
});
_define_property(this, "onCreateCategory", function() {
var newItemId = _this.props.newId();
var newItem = {
id: newItemId,
itemBody: ''
};
var newCategories = update(_this.props.interactionData.categories, _define_property({}, newItemId, {
$set: newItem
}));
var newCategoryOrder = update(_this.getCategoryOrder(), {
$push: [
newItemId
]
});
var newCategoryScoringData = {
id: newItemId,
scoringAlgorithm: 'AllOrNothing',
scoringData: {
value: []
}
};
var interactionData = update(_this.props.interactionData, {
categories: {
$set: newCategories
},
categoryOrder: {
$set: newCategoryOrder
}
});
var scoringData = update(_this.props.scoringData, {
value: {
$push: [
newCategoryScoringData
]
}
});
_this.props.changeItemState({
interactionData: interactionData,
scoringData: scoringData
});
});
_define_property(this, "onRemoveCategory", function(categoryId) {
var newCategories = omit(_this.props.interactionData.categories, categoryId);
var newCategoryOrder = _this.getCategoryOrder().filter(function(c) {
return c !== categoryId;
});
var newDistractors = omit(_this.props.interactionData.distractors, _this.props.scoringData.value.filter(function(item) {
return item.id === categoryId;
})[0].scoringData.value);
var newScoringData = _this.props.scoringData.value.filter(function(item) {
return item.id !== categoryId;
});
var interactionData = update(_this.props.interactionData, {
categoryOrder: {
$set: newCategoryOrder
},
categories: {
$set: newCategories
},
distractors: {
$set: newDistractors
}
});
var scoringData = update(_this.props.scoringData, {
value: {
$set: newScoringData
}
});
_this.props.changeItemState({
interactionData: interactionData,
scoringData: scoringData
});
});
// ===========
// ACTIONS - ANSWERS
// ===========
_define_property(this, "onCreateAnswer", function(categoryId) {
var newItemId = _this.props.newId();
var newItem = {
id: newItemId,
itemBody: ''
};
var newDistractors = update(_this.props.interactionData.distractors, _define_property({}, newItemId, {
$set: newItem
}));
var scoringDataCategoryIndex = findIndex(_this.props.scoringData.value, function(item) {
return item.id === categoryId;
});
var interactionData = update(_this.props.interactionData, {
distractors: {
$set: newDistractors
}
});
var scoringData = update(_this.props.scoringData, {
value: _define_property({}, scoringDataCategoryIndex, {
scoringData: {
value: {
// because kinesis does not save empty arrays well,
// we need to guard against this being null
$apply: function(answersList) {
return (answersList || []).concat([
newItemId
]);
}
}
}
})
});
_this.props.changeItemState({
interactionData: interactionData,
scoringData: scoringData
});
});
_define_property(this, "onRemoveAnswer", function(itemId, categoryId) {
var newDistractors = omit(_this.props.interactionData.distractors, itemId);
var scoringDataCategoryIndex = findIndex(_this.props.scoringData.value, function(item) {
return item.id === categoryId;
});
var scoringDataCategory = _this.props.scoringData.value[scoringDataCategoryIndex];
var newCategScoringDataValues = without(scoringDataCategory.scoringData.value, itemId);
var interactionData = update(_this.props.interactionData, {
distractors: {
$set: newDistractors
}
});
var scoringData = update(_this.props.scoringData, {
value: _define_property({}, scoringDataCategoryIndex, {
scoringData: {
value: {
$set: newCategScoringDataValues
}
}
})
});
_this.props.changeItemState({
interactionData: interactionData,
scoringData: scoringData
});
});
// ===========
// ACTIONS - DISTRACTORS
// ===========
_define_property(this, "onDistractorInputChange", function(itemId, e) {
var distractors = update(_this.props.interactionData.distractors, _define_property({}, itemId, {
itemBody: {
$set: e.target.value
}
}));
_this.updateInteractionData({
distractors: distractors
});
});
_define_property(this, "onRemoveDistractor", function(itemId) {
var newList = omit(_this.props.interactionData.distractors, itemId);
var newInteractionData = update(_this.props.interactionData, {
distractors: {
$set: newList
}
});
_this.props.changeItemState({
interactionData: newInteractionData
});
});
this.props = props;
}
_create_class(CategorizationPresenter, [
{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(props) {
this.props = props;
}
},
{
// =============
// HELPERS
// =============
key: "getCategoryOrder",
value: function getCategoryOrder() {
// This is to protect against quizzes which were
// created before categoryOrder was introduced
var intData = this.props.interactionData;
return intData.categoryOrder || Object.keys(intData.categories);
}
},
{
key: "updateInteractionData",
value: function updateInteractionData(updates) {
this.props.changeItemState({
interactionData: Object.assign({}, this.props.interactionData || {}, updates)
});
}
}
]);
return CategorizationPresenter;
}();
export { CategorizationPresenter as default };