import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import './index.less'; import { Checkbox } from 'antd'; import Assets from '@src/components/Assets'; import { formatSeconds, formatSecond, getMap } from '@src/services/Tools'; import Icon from '../../../../components/Icon'; import Button from '../../../../components/Button'; import Navigation from '../../../../components/Navigation'; import Answer from '../../../../components/Answer'; import Calculator from '../../../../components/Calculator'; import AnswerSelect from '../../../../components/AnswerSelect'; import AnswerTable from '../../../../components/AnswerTable'; import Editor from '../../../../components/Editor'; import { QuestionType } from '../../../../../Constant'; const QuestionTypeMap = getMap(QuestionType, 'value'); export default class extends Component { constructor(props) { super(props); this.state = { showTime: true, showNo: true, showCalculator: false, disorder: false, order: [], step: 0, answer: {}, modal: null, }; } onChangeQuestion(index, value) { const { question } = this.props; const { content } = question; const { answer = {} } = this.state; const type = content.type === 'double' ? 'double' : 'single'; if (!answer.questions) { answer.questions = content.questions.map(() => { return {}; }); } answer.questions[index] = { [type]: value }; console.log(answer); this.setState({ answer }); } onChangeAwa(value) { const { answer = {} } = this.state; answer.awa = value; this.setState({ answer }); } showConfirm(title, desc, cb) { this.showModal('confirm', title, desc, cb); } showToast(title, desc, cb) { this.showModal('toast', title, desc, cb); } showModal(type, title, desc, cb) { this.setState({ modal: { type, title, desc, cb } }); } checkAnswer() { const { question } = this.props; const { answer } = this.state; let result = null; if (question.questionType === 'awa' && !answer.awa) result = 'Please answer the question first.'; if (result) return this.showToast(null, result); return true; } hideModal(b) { if (b) { const { modal = {} } = this.state; if (modal.cb) modal.cb(); } this.setState({ modal: null }); } formatStrem(text) { if (!text) return ''; const { question = { content: {} } } = this.props; const { table = {}, questions = [] } = question.content; text = text.replace(/#select#/g, ""); text = text.replace(/#table#/g, ""); setTimeout(() => { const selectList = document.getElementsByClassName('#select#'); const tableList = document.getElementsByClassName('#table#'); for (let i = 0; i < selectList.length; i += 1) { if (!questions[i]) break; ReactDOM.render( this.onChangeQuestion(i, v)} />, selectList[i], ); } if (table.row && table.col && table.header) { const columns = table.header.map((title, index) => { return { title, key: index }; }); for (let i = 0; i < tableList.length; i += 1) { ReactDOM.render(, tableList[i]); } } }, 1); return text; } next() { const { flow } = this.props; const { answer } = this.state; if (this.checkAnswer()) { flow.submit(answer) .then(() => { flow.next(); }); } } render() { const { modal } = this.state; const { scene, paper } = this.props; let content = null; switch (scene) { case 'start': content = paper.paperModule === 'examination' ? this.renderExaminationStart() : this.renderExerciseStart(); break; case 'relax': content = this.renderRelax(); break; default: content = this.renderDetail(); break; } return
{content} {modal ? this.renderModal() : ''}
; } renderContent() { const { question = { content: {} } } = this.props; const { step } = this.state; const { steps = [] } = question.content; return (
{steps.length > 0 && this.setState({ step: v })} />}
0 ? steps[step].stem : question.stem) }} />
); } renderAnswer() { const { question = { content: {} } } = this.props; const { questions = [], type } = question.content; if (type === 'inline') return ''; return (
{question.questionType === 'awa' && this.onChangeAwa(v)} />} {questions.map((item, index) => { return (
{item.description}
this.onChangeQuestion(index, v)} />
); })}
); } renderDetail() { const { paper, userQuestion, question = { content: {} }, singleTime, stageTime, flow } = this.props; if (!userQuestion.id) return null; const { showCalculator, showTime, showNo } = this.state; const { typeset = 'one' } = question.content; return (
{QuestionTypeMap[question.questionType].long} {question.questionType === 'ir' && this.setState({ showCalculator: !showCalculator })} />} {/* { flow.toggleCollect(); }} /> */}
flow.toggleCollect()} />
{paper.title}
{ this.setState({ showTime: !showTime }); }}> {showTime && stageTime && `Time left ${formatSecond(stageTime)}`} {showTime && singleTime && `Time cost ${formatSecond(singleTime)}`}
{ this.setState({ showNo: !showNo }); }}> {showNo && `${userQuestion.no} of ${paper.questionNumber}`}
{this.renderContent()} {this.renderAnswer()}
Help
flow.toggleFullscreen()} />
this.next()}> Next
); } renderExaminationStart() { const { disorder } = this.state; const { paper, flow } = this.props; return (
{paper.title}
题目总数
{paper.questionNumer}
建议用时
{formatSeconds(paper.time)}
this.setState({ disorder: !disorder })} /> 题目选项乱序显示
); } renderExerciseStart() { const { disorder } = this.state; const { paper, flow } = this.props; return (
{paper.title}
题目总数
{paper.questionNumber}
建议用时
{formatSeconds(paper.time)}
this.setState({ disorder: !disorder })} /> 题目选项乱序显示
); } renderRelax() { return
; } renderModal() { const { modal } = this.state; return (
{modal.title}
{modal.desc}
{modal.type === 'confirm' ? (
this.hideModal(true)}> Yes
this.hideModal(false)}> No
) : (
this.hideModal(true)}> Ok
)}
); } }