UNPKG

@instructure/quiz-taking

Version:

204 lines • 10.2 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; var _dec, _class, _TakeButton; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _callSuper(_this, derived, args) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (e) { return false; } } derived = _getPrototypeOf(derived); return _possibleConstructorReturn(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], _getPrototypeOf(_this).constructor) : derived.apply(_this, args)); } 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, SecondaryNavBarButton } from '@instructure/quiz-core'; import t from '@instructure/quiz-i18n/es/format-message'; import { PresentationContent, ScreenReaderContent } from '@instructure/ui-a11y-content'; import { TopNavBar } from '@instructure/ui-top-nav-bar'; import { withStyleOverrides } from '@instructure/quiz-common'; import generateStyle from './style'; export var TakeButton = (_dec = withStyleOverrides(generateStyle, null), _dec(_class = (_TakeButton = /*#__PURE__*/function (_Component) { function TakeButton() { var _this2; _classCallCheck(this, TakeButton); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, TakeButton, [].concat(args)); _defineProperty(_this2, "submitQuiz", function () { _this2.props.submitQuiz(_this2.props.quizSessionId, _this2.props.onSubmitQuizSession); }); _defineProperty(_this2, "confirmNextKey", function () { return "quiz_session_".concat(_this2.props.quizSessionId, "_confirm_next"); }); _defineProperty(_this2, "confirmNext", function () { return sessionStore.get(_this2.confirmNextKey()) === 'false' ? false : true; }); _defineProperty(_this2, "toggleConfirmNext", function () { sessionStore.set(_this2.confirmNextKey(), String(!_this2.confirmNext())); }); _defineProperty(_this2, "unsetConfirmNext", function () { sessionStore.set(_this2.confirmNextKey()); }); _defineProperty(_this2, "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: _this2.toggleConfirmNext }))); return { title: t('Are you sure?'), text: confirmBody, continueText: t('Confirm'), cancelText: t('Cancel'), onCancel: _this2.unsetConfirmNext }; }); _defineProperty(_this2, "handleNextQuestionClick", function () { var _this2$props = _this2.props, allowBacktracking = _this2$props.allowBacktracking, currentQuestionIsLast = _this2$props.currentQuestionIsLast, currentSessionItemPosition = _this2$props.currentSessionItemPosition, quizSessionId = _this2$props.quizSessionId, itemId = _this2$props.itemId, userResponse = _this2$props.userResponse, largeFormatResponse = _this2$props.largeFormatResponse, screenreaderNotification = _this2$props.screenreaderNotification, nextQuestion = _this2$props.nextQuestion, onSubmitQuizSession = _this2$props.onSubmitQuizSession, withConfirm = _this2$props.withConfirm, isPassage = _this2$props.isPassage; if (!allowBacktracking && !currentQuestionIsLast) { screenreaderNotification(t('Question submitted')); } else if (!currentQuestionIsLast) { screenreaderNotification(t('Moving to next question')); } var submitCb = allowBacktracking ? _this2.submitQuiz : onSubmitQuizSession; var hasResponded = Object.keys(userResponse).length > 0; var goNextQuestion = function goNextQuestion() { nextQuestion(quizSessionId, allowBacktracking, currentQuestionIsLast, currentSessionItemPosition, itemId, userResponse, largeFormatResponse, submitCb, isPassage); }; if (!allowBacktracking && !hasResponded && _this2.confirmNext() && !isPassage) { withConfirm(goNextQuestion, _this2.withConfirmOptions()); } else { goNextQuestion(); } }); return _this2; } _inherits(TakeButton, _Component); return _createClass(TakeButton, [{ 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 = _objectSpread({ 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.assign({ 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(); } }]); }(Component), _defineProperty(_TakeButton, "displayName", 'TakeButton'), _defineProperty(_TakeButton, "componentId", "Quizzes".concat(_TakeButton.displayName)), _defineProperty(_TakeButton, "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, // consumer provided 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, // eslint-disable-line react/forbid-prop-types 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 }), _defineProperty(_TakeButton, "defaultProps", { disabled: false, itemId: null, largeFormatResponse: null, onSubmitQuizSession: function onSubmitQuizSession() {}, quizSessionId: null, userResponse: {}, isFooter: false, iceTopNavBarEnabled: false }), _TakeButton)) || _class); export default TakeButton;