@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
218 lines (213 loc) • 7.59 kB
JavaScript
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, _TrueFalseExample;
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 TrueFalseEdit from '../Edit';
import TrueFalseShow from '../Show';
import TrueFalseTake from '../Take';
import TrueFalseResult from '../Result';
import generateStyle from '../../common/example/default';
import generateComponentTheme from '../../common/example/defaultTheme';
import { withStyleOverrides } from '@instructure/quiz-common';
/**
---
category: TrueFalse
---
TrueFalse Example.
```jsx_example
<SettingsSwitcher locales={LOCALES}>
<TrueFalseExample />
</SettingsSwitcher>
```
**/
var TrueFalseExample = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_TrueFalseExample = /*#__PURE__*/function (_Component) {
function TrueFalseExample(props) {
var _this2;
_classCallCheck(this, TrueFalseExample);
_this2 = _callSuper(this, TrueFalseExample);
_defineProperty(_this2, "handleChangeItemState", function (itemState) {
var newState = {};
if (itemState.scoringData) {
newState.scoredData = _this2.transformScoredData(itemState.scoringData);
newState.userResponse = _this2.transformScoredData(itemState.scoringData);
}
if (itemState.interactionData) {
newState.takeInteractionData = _this2.transformTakeInteractionData(itemState.interactionData);
}
var updatedState = Object.assign({}, itemState, newState);
_this2.setState(updatedState);
});
_defineProperty(_this2, "handleResponseUpdate", function (userResponse) {
var stateUserResponse = {
value: userResponse
};
_this2.setState({
userResponse: stateUserResponse,
scoredData: _this2.transformScoredData(_this2.state.scoringData, stateUserResponse)
});
});
_this2.state = {};
if (props.useDefaultData) {
Object.assign(_this2.state, {
itemBody: 'The sun is a star',
interactionData: {
trueChoice: 'True',
falseChoice: 'False'
},
scoringData: {
value: true
},
scoredData: {},
userResponse: {}
});
_this2.state.scoredData = _this2.transformScoredData(_this2.state.scoringData);
_this2.state.takeInteractionData = _this2.transformTakeInteractionData(_this2.state.interactionData);
}
return _this2;
}
_inherits(TrueFalseExample, _Component);
return _createClass(TrueFalseExample, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
scoredData: this.transformScoredData(this.state.scoringData)
});
}
/**
* Normally, this should be calling a service
*/
}, {
key: "transformScoredData",
value: function transformScoredData(scoringData) {
var userResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.state.userResponse;
return {
correct: scoringData.value === userResponse.value,
value: {
"true": {
resultScore: scoringData.value ? 1 : 0,
userResponded: userResponse.value === true
},
"false": {
resultScore: scoringData.value ? 0 : 1,
userResponded: userResponse.value === false
}
}
};
}
/**
* 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(TrueFalseEdit, {
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(TrueFalseShow, {
interactionData: this.state.interactionData,
itemBody: this.state.itemBody,
itemId: this.state.itemId,
properties: this.state.properties,
scoringData: this.state.scoringData
}));
}
}, {
key: "renderTake",
value: function renderTake() {
return jsx("div", {
title: "take"
}, jsx(Heading, {
level: "h2"
}, "Take"), jsx(TrueFalseTake, {
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(TrueFalseResult, {
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(_TrueFalseExample, "displayName", 'TrueFalseExample'), _defineProperty(_TrueFalseExample, "componentId", "Quizzes".concat(_TrueFalseExample.displayName)), _defineProperty(_TrueFalseExample, "propTypes", {
useDefaultData: PropTypes.bool,
styles: PropTypes.object
}), _defineProperty(_TrueFalseExample, "defaultProps", {
useDefaultData: true
}), _TrueFalseExample)) || _class);
export { TrueFalseExample as default };