index.js 2.74 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,api } from '@/utils'
zhanghaozhe committed
8

9
export default class Examination extends PureComponent {
zhanghaozhe committed
10 11 12 13

    constructor(props) {
        super(props)
        this.state = {
14 15
            isShowAnswer: false,
            questionData: {}
zhanghaozhe committed
16 17 18 19
        }
    }

    componentDidMount() {
zhanghaozhe committed
20
        http.get(`${api.home}/m/dailyQuestion`)
21 22 23 24 25
            .then(res => {
                this.setState({
                    questionData: res.data.data
                })
            })
zhanghaozhe committed
26 27 28 29 30 31 32
    }

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

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

69
const MultiChoice = React.memo(({options, showCorrect}) => {
zhanghaozhe committed
70 71 72 73
    return (
        <ul className='options'>
            {
                options.map((item, index) => (
74
                    <li key={index} className={classnames('option', {'active': item.is_ans === 1 && showCorrect})}>
zhanghaozhe committed
75
                        <span className="alphabet">{String.fromCharCode(65 + index)}</span>
76
                        {item.des}
zhanghaozhe committed
77 78 79 80 81
                    </li>
                ))
            }
        </ul>
    )
82
})
zhanghaozhe committed
83

84
const Answer = React.memo(({content, isShowAnswer}) => {
zhanghaozhe committed
85 86 87 88 89 90 91 92
    return (
        <div className={classnames('answer', {scale: isShowAnswer})}>
            <p className='legend'>解析</p>
            <p className='content'>
                {content}
            </p>
        </div>
    )
93
})