Quiz.js 3.3 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
baesangjune committed
3
import Timer from 'react-compound-timer'; // 타이머쓰기위해 import
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
4
5

const question = [
baesangjune's avatar
baesangjune committed
6
7
8
    { Q: "6 X 4 ?", Choose: [6, 12, 18, 24], A: "", N:1 },
    { Q: "3 + 3 ?", Choose: [2, 4, 6, 8], A: "", N:2 },
    { Q: "3 - 1 ?", Choose: [1, 2, 3, 4], A: "", N:3 }
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
    constructor(props) {
        super(props)
        this.setQuestion = this.setQuestion.bind(this)
        this.answerbox = this.answerbox.bind(this)
baesangjune's avatar
check    
baesangjune committed
16
        this.timer=this.timer.bind(this)
baesangjune's avatar
baesangjune committed
17
        // this.enterkey = this.enterkey(this)
Jiwon Yoon's avatar
Jiwon Yoon committed
18
19
20
21
        this.state = {
            ...question[0],
            i: 0,
            page: 0,
baesangjune's avatar
baesangjune committed
22
23
            question

Jiwon Yoon's avatar
Jiwon Yoon committed
24
        }
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
25
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    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
43
    }
baesangjune's avatar
check    
baesangjune committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    timer(){
        return(
        <Timer
            initialTime={10010}
            direction="backward"
            checkpoints={[
                {
                    time: 0,
                    callback: this.setQuestion
                    //  history.go(1)
                }
            ]}
        >
            {() => (
                <>
                    <Timer.Seconds /> seconds
                </>
            )}
        </Timer> /* npm i react-compound-timer */
       )
    }
baesangjune's avatar
baesangjune committed
65
66
67
68
69
    // enterkey() {
    //     if ( window.event === 13 ) {
    //         alert("Enter Key 입력 감지 \n함수 실행.");
    //     }
    // }
baesangjune's avatar
ing    
baesangjune committed
70

Jiwon Yoon's avatar
Jiwon Yoon committed
71
    render() {
Jiwon Yoon's avatar
Jiwon Yoon committed
72
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
73
74
            <div className="Quiz">
                <h2>Q:{this.state.Q}</h2>
baesangjune's avatar
timer    
baesangjune committed
75
                {this.answerbox()}
baesangjune's avatar
baesangjune committed
76
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
77
                {this.state.Choose.map((a) =>
baesangjune's avatar
baesangjune committed
78
                    <div>{this.state.N}
baesangjune's avatar
timer    
baesangjune committed
79
                        <input type="radio" name='answer'  id={'anwer'+a} value={a} /*ref={this.textInput}  input 네임을 문제단위로 바꾸어주어야 함. */ />
Jiwon Yoon's avatar
Jiwon Yoon committed
80
                        <label for={a}>{a}</label>
baesangjune's avatar
timer    
baesangjune committed
81
82
                    </div>)                   

Jiwon Yoon's avatar
Jiwon Yoon committed
83
                }
baesangjune's avatar
baesangjune committed
84
                </div>
baesangjune's avatar
timer    
baesangjune committed
85
86
                <div className="App" class='left'>정답을 입력하세요</div>

baesangjune's avatar
   
baesangjune committed
87
                
Jiwon Yoon's avatar
Jiwon Yoon committed
88
89
90
                {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
                {(this.state.page === question.length-1)
                    ? <Link to="/end">제출</Link>
baesangjune's avatar
baesangjune committed
91
                    : <button type="button" onClick={this.setQuestion} >다음</button>
baesangjune's avatar
timer    
baesangjune committed
92
                    
Jiwon Yoon's avatar
Jiwon Yoon committed
93
                }
baesangjune's avatar
baesangjune committed
94
                {/* <input onKeyPress="this.enterkey()"/> */}
baesangjune's avatar
timer    
baesangjune committed
95
                                
baesangjune's avatar
check    
baesangjune committed
96
                {this.timer()}
baesangjune's avatar
timer    
baesangjune committed
97
               
Jiwon Yoon's avatar
Jiwon Yoon committed
98
99
100
            </div>
        )
    }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
101
102
}

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