UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

225 lines (220 loc) • 7.8 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, _MultipleAnswerExample; 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 MultipleAnswerEdit from '../Edit'; import MultipleAnswerShow from '../Show'; import MultipleAnswerTake from '../Take'; import MultipleAnswerResult from '../Result'; import generateStyle from '../../common/example/default'; import generateComponentTheme from '../../common/example/defaultTheme'; import { withStyleOverrides } from '@instructure/quiz-common'; /** --- category: MultipleAnswer --- MultipleAnswer Example. ```jsx_example <SettingsSwitcher locales={LOCALES}> <MultipleAnswerExample /> </SettingsSwitcher> ``` **/ var MultipleAnswerExample = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_MultipleAnswerExample = /*#__PURE__*/function (_Component) { function MultipleAnswerExample(props) { var _this2; _classCallCheck(this, MultipleAnswerExample); _this2 = _callSuper(this, MultipleAnswerExample); _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: 'Hillary Clinton', position: 1 }, { id: 'uuid2', itemBody: 'Mike Nomitch', position: 2 }, { id: 'uuid3', itemBody: 'Marc Phillips', position: 3 }, { id: 'uuid4', itemBody: 'Donald Trump', position: 4 }, { id: 'uuid5', itemBody: 'Hannah Bottalla', position: 5 }] }, itemBody: 'Who is on the Quizzes Team?', properties: {}, scoringData: { value: ['uuid2', 'uuid3', 'uuid5'] }, scoredData: {}, 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(MultipleAnswerExample, _Component); return _createClass(MultipleAnswerExample, [{ key: "transformScoredData", value: function transformScoredData(scoringData) { var _this3 = this; return { correct: this.state.userResponse.value.sort().join() === scoringData.value.sort().join(), value: scoringData.value.reduce(function (value, currVal) { var ret = value; ret[currVal] = { resultScore: scoringData.value.includes(currVal) ? 1 : 0, userResponded: _this3.state.userResponse.value.includes(currVal) }; 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(MultipleAnswerEdit, { 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(MultipleAnswerShow, { 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(MultipleAnswerTake, { 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(MultipleAnswerResult, { 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(_MultipleAnswerExample, "displayName", 'MultipleAnswerExample'), _defineProperty(_MultipleAnswerExample, "componentId", "Quizzes".concat(_MultipleAnswerExample.displayName)), _defineProperty(_MultipleAnswerExample, "propTypes", { useDefaultData: PropTypes.bool, styles: PropTypes.object }), _defineProperty(_MultipleAnswerExample, "defaultProps", { useDefaultData: true }), _MultipleAnswerExample)) || _class); export { MultipleAnswerExample as default };