Quiz.js 9.98 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
    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;
baesangjune's avatar
good    
baesangjune committed
40
                localStorage.setItem(this.state.i+'번문제 답' + checked_value, checked_value)
Jiwon Yoon's avatar
Jiwon Yoon committed
41
42
            }
        }
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
good    
baesangjune committed
79
                        <input type="radio" name='answer'  id={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
}

baesangjune's avatar
.    
baesangjune committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
export default Quiz;


import React, { useState, useRef } from 'react'
import { Link } from 'react-router-dom';
import Timer from 'react-compound-timer'; // 타이머쓰기위해 import


const QnA = [
    { 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 }
]


// function 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,checked_value)
//         }
//     }
// }

//************** */
// function 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(question.i+'번문제 답' + checked_value, checked_value)
//         }
//     }
// }

function Quiz() {
    let [question, setQuestionss] = useState({
        ...QnA[0],
        i: 0,
        page: 0,
    })

    function setQuestion() {
        setQuestionss({ ...QnA[question.i + 1], i: question.i + 1, page: question.page + 1 })
    }

    function answerbox() {

        let get=useRef(null);
        let get2=()=>{
            localStorage.setItem(get.current.focus())
        }
        let count = get2.length
        // let checked_index = -1;
        let checked_value = '';
        for (let i = 0; i < count; i++) {
            if (get2[i].checked) {
                // checked_index = i;
                checked_value = get2[i];
                localStorage.setItem(question.i+'번문제 답' + checked_value, checked_value)
            }
        }
        
            
        

    }
    return (
        <div className="Quiz">
            <h2>Q:{question.Q}</h2>
            {answerbox}
            <div>
                {question.Choose.map((a) =>
                    <div> {/*this.state.N*/}
                        <input type="radio" name='answer' id={'anwer' + a} value={a} ref={get} />
                        <label for={a}> {a}</label>
                    </div>)

                }
            </div>
            <div className="App" class='left'>정답을 입력하세요</div>


            {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
            {(question.page === QnA.length - 1)
                ? <Link to="/end">제출</Link>
                : <button type="button" onClick={setQuestion} >다음</button>

            }
            {/* <input onKeyPress="this.enterkey()"/> */}



            <Timer
                initialTime={3010}
                direction="backward"
                checkpoints={[
                    {
                        time: 0,
                        callback: setQuestion
                        //  history.go(1)
                    }
                ]}
            >
                {() => (
                    <>
                        <Timer.Seconds /> seconds
                        </>
                )}
            </Timer>
            {/* npm i react-compound-timer */}

        </div>
    )



}

// class Quiz extends React.Component {
//     constructor(props) {
//         super(props)
//         this.setQuestion = this.setQuestion.bind(this)
//         this.answerbox = this.answerbox.bind(this)
//         // this.enterkey = this.enterkey(this)
//         this.state = {
//             ...question[0],
//             i: 0,
//             page: 0,
//             question

//         }
//     }
//     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)
//             }
//         }
//     }
//     // enterkey() {
//     //     if ( window.event === 13 ) {
//     //         alert("Enter Key 입력 감지 \n함수 실행.");
//     //     }
//     // }

//     render() {
//         return (
//             <div className="Quiz">
//                 <h2>Q:{this.state.Q}</h2>
//                 {this.answerbox()}
//                 <div>
//                     {this.state.Choose.map((a) =>
//                         <div> {/*this.state.N*/}
//                             <input type="radio" name='answer' id={'anwer' + a} value={a} /*ref={this.textInput}  input 네임을 문제단위로 바꾸어주어야 함. */ />
//                             <label for={a}> {a}</label>
//                         </div>)

//                     }
//                 </div>
//                 <div className="App" class='left'>정답을 입력하세요</div>


//                 {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
//                 {(this.state.page === question.length - 1)
//                     ? <Link to="/end">제출</Link>
//                     : <button type="button" onClick={this.setQuestion} >다음</button>

//                 }
//                 {/* <input onKeyPress="this.enterkey()"/> */}



//                 <Timer
//                     initialTime={3010}
//                     direction="backward"
//                     checkpoints={[
//                         {
//                             time: 0,
//                             callback: this.setQuestion
//                             //  history.go(1)
//                         }
//                     ]}
//                 >
//                     {() => (
//                         <>
//                             <Timer.Seconds /> seconds
//                         </>
//                     )}
//                 </Timer>
//                 {/* npm i react-compound-timer */}

//             </div>
//         )
//     }
// }

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