Quiz.js 2.9 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import React from 'react'
import { Link } from 'react-router-dom';
baesangjune's avatar
timer    
baesangjune committed
3
import Timer from 'react-compound-timer';
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
4
5

const question = [
Jiwon Yoon's avatar
Jiwon Yoon committed
6
7
8
    { Q: "6 X 4 ?", Choose: [6, 12, 18, 24], A: "" },
    { Q: "3 + 3 ?", Choose: [2, 4, 6, 8], A: "" },
    { Q: "3 - 1 ?", Choose: [1, 2, 3, 4], A: "" }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
9
10
11
]

class Quiz extends React.Component {
Jiwon Yoon's avatar
Jiwon Yoon committed
12
13
14
15
16
17
18
19
20
    constructor(props) {
        super(props)
        this.setQuestion = this.setQuestion.bind(this)
        this.answerbox = this.answerbox.bind(this)
        this.state = {
            ...question[0],
            i: 0,
            page: 0,
        }
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
21
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    setQuestion() {
        //값이 입력되지 않은채로 넘겨졌을 때 문제 해결 해야 함-sj-
        this.setState({ ...question[this.state.i + 1], i: this.state.i + 1, page: this.state.page + 1 })
    }
    //answerbox - answer박스의 값을 네임리스트로 받아와서 값을 localstorage에 저장 
    answerbox() {
        let answers = document.getElementsByName('answer');
        let count = answers.length
        // let checked_index = -1;
        let checked_value = '';
        for (let i = 0; i < count; i++) {
            if (answers[i].checked) {
                // checked_index = i;
                checked_value = answers[i].value;
                localStorage.setItem('answer-' + i, checked_value)
            }
        }
baesangjune's avatar
ing    
baesangjune committed
39
40
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
41
    render() {
Jiwon Yoon's avatar
Jiwon Yoon committed
42
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
43
44
            <div className="Quiz">
                <h2>Q:{this.state.Q}</h2>
baesangjune's avatar
timer    
baesangjune committed
45
                {this.answerbox()}
Jiwon Yoon's avatar
Jiwon Yoon committed
46
47
                {this.state.Choose.map((a) =>
                    <div>
baesangjune's avatar
timer    
baesangjune committed
48
                        <input type="radio" name='answer'  id={'anwer'+a} value={a} /*ref={this.textInput}  input 네임을 문제단위로 바꾸어주어야 함. */ />
Jiwon Yoon's avatar
Jiwon Yoon committed
49
                        <label for={a}>{a}</label>
baesangjune's avatar
timer    
baesangjune committed
50
51
                    </div>)                   

Jiwon Yoon's avatar
Jiwon Yoon committed
52
                }
baesangjune's avatar
timer    
baesangjune committed
53
54
                <div className="App" class='left'>정답을 입력하세요</div>

baesangjune's avatar
   
baesangjune committed
55
                
Jiwon Yoon's avatar
Jiwon Yoon committed
56
57
58
59
                {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
                {(this.state.page === question.length-1)
                    ? <Link to="/end">제출</Link>
                    : <button type="button" onClick={this.setQuestion}>다음</button>
baesangjune's avatar
timer    
baesangjune committed
60
                    
Jiwon Yoon's avatar
Jiwon Yoon committed
61
                }
baesangjune's avatar
timer    
baesangjune committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
                                
               <Timer
                    initialTime={5000}
                    direction="backward"
                    checkpoints={[
                        {
                            time: 0,
                            callback: () =>  this.setQuestion,
                        }
                    ]}
                >
                    {() => (
                        <>
                            <Timer.Seconds /> seconds
                        </>
                    )}
                </Timer> {/* npm i react-compound-timer */}
               
Jiwon Yoon's avatar
Jiwon Yoon committed
80
81
82
            </div>
        )
    }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
83
84
}

Jiwon Yoon's avatar
Jiwon Yoon committed
85
export default Quiz;