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

xuzhenghua committed
5
import {HeaderBar, Tag} from '../../common'
zhanghaozhe committed
6
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

xuzhenghua committed
12

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

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

    componentDidMount() {
zhanghaozhe committed
24
        http.get(`${API.home}/m/dailyQuestion`)
25
            .then(res => {
FE committed
26 27 28 29 30 31 32
                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
                    })
                }
33
            })
zhanghaozhe committed
34 35 36 37 38 39 40
    }

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

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

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

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