Quiz.js 3.33 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
baesangjune committed
16
        // this.enterkey = this.enterkey(this)
Jiwon Yoon's avatar
Jiwon Yoon committed
17
18
19
20
        this.state = {
            ...question[0],
            i: 0,
            page: 0,
baesangjune's avatar
baesangjune committed
21
22
            question

Jiwon Yoon's avatar
Jiwon Yoon committed
23
        }
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
24
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    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
42
    }
baesangjune's avatar
baesangjune committed
43
44
45
46
47
    // enterkey() {
    //     if ( window.event === 13 ) {
    //         alert("Enter Key 입력 감지 \n함수 실행.");
    //     }
    // }
baesangjune's avatar
ing    
baesangjune committed
48

Jiwon Yoon's avatar
Jiwon Yoon committed
49
    render() {
Jiwon Yoon's avatar
Jiwon Yoon committed
50
        return (
Jiwon Yoon's avatar
Jiwon Yoon committed
51
52
            <div className="Quiz">
                <h2>Q:{this.state.Q}</h2>
baesangjune's avatar
timer    
baesangjune committed
53
                {this.answerbox()}
baesangjune's avatar
baesangjune committed
54
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
55
                {this.state.Choose.map((a) =>
baesangjune's avatar
baesangjune committed
56
                    <div>{this.state.N}
baesangjune's avatar
timer    
baesangjune committed
57
                        <input type="radio" name='answer'  id={'anwer'+a} value={a} /*ref={this.textInput}  input 네임을 문제단위로 바꾸어주어야 함. */ />
Jiwon Yoon's avatar
Jiwon Yoon committed
58
                        <label for={a}>{a}</label>
baesangjune's avatar
timer    
baesangjune committed
59
60
                    </div>)                   

Jiwon Yoon's avatar
Jiwon Yoon committed
61
                }
baesangjune's avatar
baesangjune committed
62
                </div>
baesangjune's avatar
timer    
baesangjune committed
63
64
                <div className="App" class='left'>정답을 입력하세요</div>

baesangjune's avatar
   
baesangjune committed
65
                
Jiwon Yoon's avatar
Jiwon Yoon committed
66
67
68
                {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
                {(this.state.page === question.length-1)
                    ? <Link to="/end">제출</Link>
baesangjune's avatar
baesangjune committed
69
                    : <button type="button" onClick={this.setQuestion} >다음</button>
baesangjune's avatar
timer    
baesangjune committed
70
                    
Jiwon Yoon's avatar
Jiwon Yoon committed
71
                }
baesangjune's avatar
baesangjune committed
72
                {/* <input onKeyPress="this.enterkey()"/> */}
baesangjune's avatar
timer    
baesangjune committed
73
74
75
76
77
78
79
                                
               <Timer
                    initialTime={5000}
                    direction="backward"
                    checkpoints={[
                        {
                            time: 0,
baesangjune's avatar
baesangjune committed
80
81
                            callback: () => console.log('콜백실행')
                            //  history.go(1)
baesangjune's avatar
timer    
baesangjune committed
82
83
84
85
86
87
88
89
90
91
                        }
                    ]}
                >
                    {() => (
                        <>
                            <Timer.Seconds /> seconds
                        </>
                    )}
                </Timer> {/* npm i react-compound-timer */}
               
Jiwon Yoon's avatar
Jiwon Yoon committed
92
93
94
            </div>
        )
    }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
95
96
}

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