index.js 3.12 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react';
import './index.scss'
zhanghaozhe committed
3 4 5 6
import { HeaderBar } from "src/common/index"
import Question from "src/components/ai-test/common/question"
import Navigation from "src/components/ai-test/common/navigation"
import { html, http } from "src/utils"
zhanghaozhe committed
7
import { Toast } from "antd-mobile";
zhanghaozhe committed
8
import Recommends from 'src/components/ai-test/common/recommends'
zhanghaozhe committed
9 10 11 12 13 14 15 16 17


class Analysis extends Component {

  state = {
    questions: [],
    activeIndex: 0,
    userSelect: '',
    rightAnswer: '',
zhanghaozhe committed
18
    userUnselect: false,
zhanghaozhe committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  }

  componentDidMount() {
    this.getAnalysis()
  }

  getAnalysis = () => {
    http.post(`${API.home}/sys/get_analysis`, {
      record_id: this.props.match.params.recordId,
    }).then(res => {
      const {code, msg, data} = res.data
      if (code === 200) {
        this.setState({
          questions: data,
        }, () => {
          this.getAnswerInfo()
        });
      } else {
        Toast.fail(msg, 2, null, false)
      }
    })
  }

  getAnswerInfo = () => {
    const {questions, activeIndex} = this.state
    const question = questions[activeIndex]
    const userAnswerIndex = question.options.findIndex(item => item.user_select)
    const rightAnswerIndex = question.options.findIndex(item => item.is_ans)
    this.setState({
      userSelect: String.fromCharCode(65 + userAnswerIndex),
      rightAnswer: String.fromCharCode(65 + rightAnswerIndex),
zhanghaozhe committed
50
      userUnselect: userAnswerIndex < 0,
zhanghaozhe committed
51 52 53 54 55 56 57 58 59 60
    });
  }

  componentDidUpdate(prevProps, prevState) {
    if (prevState.activeIndex !== this.state.activeIndex) {
      this.getAnswerInfo()
    }
  }

  render() {
zhanghaozhe committed
61
    const {questions, activeIndex, userSelect, rightAnswer, userUnselect} = this.state
zhanghaozhe committed
62 63 64 65 66 67 68 69
    return (
      <div className={'analysis-container'}>
        <HeaderBar title={'AI水平测试'} arrow={true}/>
        {
          !!questions.length && <Question activeIndex={activeIndex} question={questions[activeIndex]}/>
        }
        <div style={{height: '8px', backgroundColor: '#f5f5f5'}}></div>
        <div className="analysis">
zhanghaozhe committed
70 71 72 73 74 75 76
          {
            userUnselect
              ? <div className={'info'}>您未作答</div>
              : <div className="info">
                您选择的是{userSelect},正确答案是{rightAnswer} 回答{userSelect === rightAnswer ? '正确' : '错误'}
              </div>
          }
zhanghaozhe committed
77 78 79 80 81 82 83 84 85 86 87 88
          <div className="content">
            <div className="head">
              <i className="icon"></i>
              <span>解析</span>
            </div>
            {
              !!questions.length &&
              <div className="analysis-content" dangerouslySetInnerHTML={html(questions[activeIndex].analysis)}></div>
            }
          </div>
        </div>
        {
zhanghaozhe committed
89 90
          !!questions.length && questions[activeIndex] && questions[activeIndex].commend_course && !!questions[activeIndex].commend_course.length &&
          <Recommends recommends={questions[activeIndex].commend_course}/>
zhanghaozhe committed
91
        }
zhanghaozhe committed
92 93 94 95 96
        <Navigation questions={questions} isAnalysis={true} handleClick={(index) => {
          this.setState({
            activeIndex: index,
          });
        }}/>
zhanghaozhe committed
97 98 99 100 101
      </div>
    );
  }
}

zhanghaozhe committed
102
export default Analysis;