index.js 3.14 KB
Newer Older
1
import React, { PureComponent } from 'react'
zhanghaozhe committed
2 3 4 5 6
import './examination.scss'
import classnames from 'classnames'

import { Tag } from '../../common'
import OpenApp from './OpenApp'
zhanghaozhe committed
7
import { http, html, htmlDecode} from '@/utils'
FE committed
8 9
import { config } from 'rxjs';
import { Object } from 'core-js';
zhanghaozhe committed
10

zhanghaozhe committed
11

12
export default class Examination extends PureComponent {
zhanghaozhe committed
13 14 15 16

    constructor(props) {
        super(props)
        this.state = {
17 18
            isShowAnswer: false,
            questionData: {}
zhanghaozhe committed
19 20 21 22
        }
    }

    componentDidMount() {
zhanghaozhe committed
23
        http.get(`${API.home}/m/dailyQuestion`)
24
            .then(res => {
FE committed
25 26 27 28 29 30 31
                const { data: { data, code } } = res;
                if(code === 200) {
                    const newData = Object.assign(data, {analysis: data.analysis.length> 0? data.analysis.split('\n'): []})
                    this.setState({
                        questionData: newData
                    })
                }
32
            })
zhanghaozhe committed
33 34 35 36 37 38 39
    }

    showAnswer = () => {
        this.setState({isShowAnswer: !this.state.isShowAnswer})
    }

    render() {
40 41
        let {
            questionData: {
zhanghaozhe committed
42
                ques, type_id, options, analysis, category
43 44 45
            },
            isShowAnswer
        } = this.state
zhanghaozhe committed
46 47 48 49
        return (
            <div className='examination'>
                <div className="question-container">
                    <div className="topic">
zhanghaozhe committed
50
                        <Tag className='category-tag'>{category}</Tag>
zhanghaozhe committed
51
                        <span dangerouslySetInnerHTML={{__html: ques}}/>
zhanghaozhe committed
52 53
                    </div>
                    {
54
                        type_id === 1 && <MultiChoice className='options' options={options}
zhanghaozhe committed
55
                                                      showCorrect={this.state.isShowAnswer}
56
                        />
zhanghaozhe committed
57 58
                    }
                    {
59
                        !isShowAnswer &&
zhanghaozhe committed
60 61 62 63 64 65 66 67
                        (
                            <div className="show-answer" onClick={this.showAnswer}>
                                <span>查看解析<i className='iconfont iconiconfront-69'></i></span>
                            </div>
                        )
                    }
                </div>
                {
68
                    isShowAnswer && <Answer content={analysis} isShowAnswer/>
zhanghaozhe committed
69
                }
70
                <OpenApp/>
zhanghaozhe committed
71 72 73 74 75
            </div>
        )
    }
}

76
const MultiChoice = React.memo(({options, showCorrect}) => {
zhanghaozhe committed
77 78 79 80
    return (
        <ul className='options'>
            {
                options.map((item, index) => (
81
                    <li key={index} className={classnames('option', {'active': item.is_ans === 1 && showCorrect})}>
zhanghaozhe committed
82
                        <span className="alphabet">{String.fromCharCode(65 + index)}</span>
83
                        {item.des}
zhanghaozhe committed
84 85 86 87 88
                    </li>
                ))
            }
        </ul>
    )
89
})
zhanghaozhe committed
90

91
const Answer = React.memo(({content, isShowAnswer}) => {
zhanghaozhe committed
92 93 94
    return (
        <div className={classnames('answer', {scale: isShowAnswer})}>
            <p className='legend'>解析</p>
FE committed
95
            {content.length>0 && content.map((item, index) => <p key={index} className='content' dangerouslySetInnerHTML={{__html: item}}/>)}
zhanghaozhe committed
96 97
        </div>
    )
98
})