@instructure/quiz-taking
Version:
288 lines (287 loc) • 13.5 kB
JavaScript
function _assert_this_initialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _call_super(_this, derived, args) {
derived = _get_prototype_of(derived);
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
}
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;
}
function _get_prototype_of(o) {
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _get_prototype_of(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _set_prototype_of(subClass, superClass);
}
function _object_spread(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_define_property(target, key, source[key]);
});
}
return target;
}
function _possible_constructor_return(self, call) {
if (call && (_type_of(call) === "object" || typeof call === "function")) {
return call;
}
return _assert_this_initialized(self);
}
function _set_prototype_of(o, p) {
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _set_prototype_of(o, p);
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _is_native_reflect_construct() {
try {
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
} catch (_) {}
return (_is_native_reflect_construct = function() {
return !!result;
})();
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Button } from '@instructure/ui-buttons';
import { Checkbox } from '@instructure/ui-checkbox';
import { View } from '@instructure/ui-view';
import { sessionStore } from '@instructure/quiz-core/common/util/sessionStore';
import { SecondaryNavBarButton } from '@instructure/quiz-core/common/components/layout/navbar/SecondaryNavBarButton/index';
import t from '@instructure/quiz-i18n/format-message';
import { PresentationContent, ScreenReaderContent } from '@instructure/ui-a11y-content';
import { TopNavBar } from '@instructure/ui-top-nav-bar';
import { withStyleOverrides } from '@instructure/quiz-common/util/withStyleOverrides';
import generateStyle from './style';
var BaseTakeButton = /*#__PURE__*/ function(Component) {
"use strict";
_inherits(BaseTakeButton, Component);
function BaseTakeButton() {
_class_call_check(this, BaseTakeButton);
var _this;
_this = _call_super(this, BaseTakeButton, arguments), _define_property(_this, "submitQuiz", function() {
_this.props.submitQuiz(_this.props.quizSessionId, _this.props.onSubmitQuizSession);
}), _define_property(_this, "confirmNextKey", function() {
return "quiz_session_".concat(_this.props.quizSessionId, "_confirm_next");
}), _define_property(_this, "confirmNext", function() {
return sessionStore.get(_this.confirmNextKey()) === 'false' ? false : true;
}), _define_property(_this, "toggleConfirmNext", function() {
sessionStore.set(_this.confirmNextKey(), String(!_this.confirmNext()));
}), _define_property(_this, "unsetConfirmNext", function() {
sessionStore.set(_this.confirmNextKey());
}), _define_property(_this, "withConfirmOptions", function() {
var confirmBody = /*#__PURE__*/ React.createElement(View, {
as: "div"
}, t('You have not answered this question and this quiz does not allow backtracking. Proceed anyway?'), /*#__PURE__*/ React.createElement(View, {
as: "div",
margin: "medium 0 0 0"
}, /*#__PURE__*/ React.createElement(Checkbox, {
label: t('Do not ask me again for this attempt'),
size: "small",
onChange: _this.toggleConfirmNext
})));
return {
title: t('Are you sure?'),
text: confirmBody,
continueText: t('Confirm'),
cancelText: t('Cancel'),
onCancel: _this.unsetConfirmNext
};
}), _define_property(_this, "requiredQuestionAlertOptions", function() {
return {
title: t('Required Question'),
text: t('An answer is required before proceeding.'),
continueText: t('Ok'),
hideCancelButton: true
};
}), _define_property(_this, "handleNextQuestionClick", function() {
var _this_props = _this.props, allowBacktracking = _this_props.allowBacktracking, currentQuestionIsLast = _this_props.currentQuestionIsLast, currentSessionItemPosition = _this_props.currentSessionItemPosition, quizSessionId = _this_props.quizSessionId, itemId = _this_props.itemId, userResponse = _this_props.userResponse, largeFormatResponse = _this_props.largeFormatResponse, screenreaderNotification = _this_props.screenreaderNotification, nextQuestion = _this_props.nextQuestion, onSubmitQuizSession = _this_props.onSubmitQuizSession, withConfirm = _this_props.withConfirm, isPassage = _this_props.isPassage, isRequiredItem = _this_props.isRequiredItem, isSurvey = _this_props.isSurvey;
if (!allowBacktracking && !currentQuestionIsLast) {
screenreaderNotification(t('Question submitted'));
} else if (!currentQuestionIsLast) {
screenreaderNotification(t('Moving to next question'));
}
var submitCb = allowBacktracking ? _this.submitQuiz : onSubmitQuizSession;
var hasResponded = Object.keys(userResponse).length > 0;
var goNextQuestion = function() {
nextQuestion(quizSessionId, allowBacktracking, currentQuestionIsLast, currentSessionItemPosition, itemId, userResponse, largeFormatResponse, submitCb, isPassage);
};
if (isPassage) {
goNextQuestion();
return;
}
if (isSurvey && isRequiredItem && !hasResponded && !allowBacktracking) {
withConfirm(function() {}, _this.requiredQuestionAlertOptions());
return;
}
if (!hasResponded && !allowBacktracking && _this.confirmNext()) {
withConfirm(goNextQuestion, _this.withConfirmOptions());
return;
}
goNextQuestion();
});
return _this;
}
_create_class(BaseTakeButton, [
{
key: "renderWithOneQuestionAtATime",
value: function renderWithOneQuestionAtATime() {
var _this_props = this.props, currentQuestionIsLast = _this_props.currentQuestionIsLast, disabled = _this_props.disabled;
var text = currentQuestionIsLast ? t('Submit') : t('Next');
var screenreaderText = currentQuestionIsLast ? t('Submit') : t('Next Question');
var btnType = currentQuestionIsLast ? 'primary' : 'primary-inverse';
if (this.props.iceTopNavBarEnabled && !this.props.isFooter) {
var buttonProps = _object_spread({
id: 'TakeButton',
onClick: this.handleNextQuestionClick,
disabled: disabled,
'data-automation': 'sdk-oqaat-next-or-submit-button'
}, this.props.styles.TopNavBarItemProps);
if (currentQuestionIsLast) {
return /*#__PURE__*/ React.createElement(TopNavBar.Item, buttonProps, text);
}
return /*#__PURE__*/ React.createElement(SecondaryNavBarButton, buttonProps, text);
}
return /*#__PURE__*/ React.createElement(Button, {
onClick: this.handleNextQuestionClick,
color: btnType,
disabled: disabled,
"data-automation": "sdk-oqaat-next-or-submit-button"
}, /*#__PURE__*/ React.createElement(ScreenReaderContent, null, screenreaderText), /*#__PURE__*/ React.createElement(PresentationContent, null, text));
}
},
{
key: "renderWithMultipleQuestionsAtATime",
value: function renderWithMultipleQuestionsAtATime() {
var disabled = this.props.disabled;
if (this.props.iceTopNavBarEnabled && !this.props.isFooter) {
return /*#__PURE__*/ React.createElement(TopNavBar.Item, _object_spread({
id: "TakeButton",
onClick: this.submitQuiz,
disabled: disabled || !this.props.quizSessionId,
"data-automation": "sdk-submit-button"
}, this.props.styles.TopNavBarItemProps), t('Submit'));
}
return /*#__PURE__*/ React.createElement(Button, {
onClick: this.submitQuiz,
color: "primary",
"data-automation": "sdk-submit-button",
disabled: disabled || !this.props.quizSessionId
}, t('Submit'));
}
},
{
key: "render",
value: function render() {
if (!this.props.styles.showTakeButton && !this.props.isFooter) {
return null;
}
if (this.props.isOneQuestionAtATime) {
return this.renderWithOneQuestionAtATime();
}
return this.renderWithMultipleQuestionsAtATime();
}
}
]);
return BaseTakeButton;
}(Component);
_define_property(BaseTakeButton, "displayName", 'TakeButton');
_define_property(BaseTakeButton, "componentId", "Quizzes".concat(BaseTakeButton.displayName));
_define_property(BaseTakeButton, "propTypes", {
allowBacktracking: PropTypes.bool.isRequired,
currentQuestionIsLast: PropTypes.bool.isRequired,
currentSessionItemPosition: PropTypes.number.isRequired,
isOneQuestionAtATime: PropTypes.bool.isRequired,
itemId: PropTypes.string,
largeFormatResponse: PropTypes.shape({
value: PropTypes.string
}),
onSubmitQuizSession: PropTypes.func,
nextQuestion: PropTypes.func.isRequired,
quizSessionId: PropTypes.string,
screenreaderNotification: PropTypes.func.isRequired,
submitQuiz: PropTypes.func.isRequired,
disabled: PropTypes.bool,
// This object is passed through; TakeButton does not care about shape
userResponse: PropTypes.object,
withConfirm: PropTypes.func.isRequired,
isFooter: PropTypes.bool,
iceTopNavBarEnabled: PropTypes.bool,
// eslint-disable-next-line react/forbid-prop-types,react/require-default-props
styles: PropTypes.object,
isPassage: PropTypes.bool.isRequired,
isRequiredItem: PropTypes.bool,
isSurvey: PropTypes.bool
});
_define_property(BaseTakeButton, "defaultProps", {
disabled: false,
itemId: null,
largeFormatResponse: null,
/* v8 ignore start */ onSubmitQuizSession: function() {},
/* v8 ignore end */ quizSessionId: null,
userResponse: {},
isFooter: false,
iceTopNavBarEnabled: false,
isRequiredItem: false,
isSurvey: false
});
export var TakeButton = withStyleOverrides(generateStyle, null)(BaseTakeButton);
export default TakeButton;