UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

246 lines (240 loc) • 8.15 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, _MultipleChoiceExample; 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)); } /** @jsx jsx */ /* eslint-disable react/jsx-no-literals */ import { Component } from 'react'; import PropTypes from 'prop-types'; import { cloneDeep } from 'lodash'; import { Heading } from '@instructure/ui-heading'; import { jsx } from '@instructure/emotion'; import MultipleChoiceEdit from '../Edit'; import MultipleChoiceShow from '../Show'; import MultipleChoiceTake from '../Take'; import MultipleChoiceResult from '../Result'; import generateStyle from '../../common/example/default'; import generateComponentTheme from '../../common/example/defaultTheme'; import { withStyleOverrides } from '@instructure/quiz-common'; /** --- category: MultipleChoice --- MultipleChoice Example. ```jsx_example <SettingsSwitcher locales={LOCALES}> <MultipleChoiceExample /> </SettingsSwitcher> ``` **/ var MultipleChoiceExample = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_MultipleChoiceExample = /*#__PURE__*/function (_Component) { function MultipleChoiceExample(props) { var _this2; _classCallCheck(this, MultipleChoiceExample); _this2 = _callSuper(this, MultipleChoiceExample); _defineProperty(_this2, "handleChangeItemState", function (itemState) { var newState = Object.assign({}, itemState, itemState.scoringData ? { scoredData: _this2.transformScoredData(itemState.scoringData) } : null, itemState.interactionData ? { takeInteractionData: _this2.transformTakeInteractionData(itemState.interactionData) } : null); _this2.setState(newState); }); _defineProperty(_this2, "handleResponseUpdate", function (userResponse) { _this2.setState({ userResponse: { value: userResponse }, scoredData: _this2.transformScoredData(_this2.state.scoringData) }); }); _this2.state = {}; if (props.useDefaultData) { Object.assign(_this2.state, { interactionData: { choices: [{ id: 'uuid1', itemBody: 'Yellow', position: 1 }, { id: 'uuid2', itemBody: 'Red', position: 2 }, { id: 'uuid4', itemBody: 'Green', position: 4 }, { id: 'uuid3', itemBody: 'Blue', position: 3 }] }, itemId: '123', itemBody: 'What color is the sun?', properties: { varyPointsByAnswer: false, shuffleRules: { choices: { shuffled: false } } }, scoringData: { value: 'uuid1', values: [{ value: 'uuid1', points: 0 }, { value: 'uuid2', points: 1 }, { value: 'uuid3', points: 3 }, { value: 'uuid4', points: -1 }] }, scoredData: {}, takeInteractionData: {}, userResponse: { value: '' } }); _this2.state.scoredData = _this2.transformScoredData(_this2.state.scoringData); _this2.state.takeInteractionData = _this2.transformTakeInteractionData(_this2.state.interactionData); } return _this2; } /** * Normally, this should be calling a service */ _inherits(MultipleChoiceExample, _Component); return _createClass(MultipleChoiceExample, [{ key: "transformScoredData", value: function transformScoredData(scoringData) { var _this3 = this; return { correct: this.state.userResponse === scoringData.value, value: scoringData.values.reduce(function (value, currVal) { var ret = value; ret[currVal.value] = { resultScore: currVal.points, userResponded: currVal.value === _this3.state.userResponse, correct: currVal.value === scoringData.value }; return ret; }, {}) }; } /** * Normally, this should be calling a service */ }, { key: "transformTakeInteractionData", value: function transformTakeInteractionData(interactionData) { return cloneDeep(interactionData); } }, { key: "renderEdit", value: function renderEdit() { return jsx("div", { title: "edit" }, jsx(Heading, { level: "h2" }, "Edit"), jsx(MultipleChoiceEdit, { changeItemState: this.handleChangeItemState, interactionData: this.state.interactionData, itemBody: this.state.itemBody, itemId: this.state.itemId, properties: this.state.properties, scoringData: this.state.scoringData })); } }, { key: "renderShow", value: function renderShow() { return jsx("div", { title: "show" }, jsx(Heading, { level: "h2" }, "Show"), jsx(MultipleChoiceShow, { itemId: this.state.itemId, interactionData: this.state.interactionData, itemBody: this.state.itemBody, properties: this.state.properties, scoringData: this.state.scoringData })); } }, { key: "renderTake", value: function renderTake() { return jsx("div", { title: "take" }, jsx(Heading, { level: "h2" }, "Take"), jsx(MultipleChoiceTake, { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, handleResponseUpdate: this.handleResponseUpdate })); } // FIXME: the props should be scoredData here }, { key: "renderResult", value: function renderResult() { return jsx("div", { title: "result" }, jsx(Heading, { level: "h2" }, "Result"), jsx(MultipleChoiceResult, { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, scoredData: this.state.scoredData })); } }, { key: "render", value: function render() { return jsx("div", null, jsx("div", { css: this.props.styles.row }, jsx("div", { css: this.props.styles.panel }, this.renderEdit()), jsx("div", { css: this.props.styles.panel }, this.renderShow())), jsx("div", { css: this.props.styles.row }, jsx("div", { css: this.props.styles.panel }, this.renderTake()), jsx("div", { css: this.props.styles.panel }, this.renderResult()))); } }]); }(Component), _defineProperty(_MultipleChoiceExample, "displayName", 'MultipleChoiceExample'), _defineProperty(_MultipleChoiceExample, "componentId", "Quizzes".concat(_MultipleChoiceExample.displayName)), _defineProperty(_MultipleChoiceExample, "propTypes", { useDefaultData: PropTypes.bool, styles: PropTypes.object }), _defineProperty(_MultipleChoiceExample, "defaultProps", { useDefaultData: true }), _MultipleChoiceExample)) || _class); export { MultipleChoiceExample as default };