Quiz.js 4.36 KB
Newer Older
baesangjune's avatar
home    
baesangjune committed
1
import React, { useState } from 'react'
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { Link } from 'react-router-dom';
baesangjune's avatar
baesangjune committed
3
import Timer from 'react-compound-timer'; // 타이머쓰기위해 import
baesangjune's avatar
home    
baesangjune committed
4
5
import logo from './img_question.png'
import fight from './img_quiz.png'
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
6

baesangjune's avatar
home    
baesangjune committed
7
8
9
10
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 }
Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
11
12
]

baesangjune's avatar
home    
baesangjune committed
13
14
15
16
17
18
19
function Quiz() {
    let [question, setQuestionss] = useState({
        ...QnA[0],
        i: 0,
        page: 0,
    })
    let [selected, setSelected] = useState("")  //선택한 답을 보여줄 것들
baesangjune's avatar
baesangjune committed
20

baesangjune's avatar
home    
baesangjune committed
21
22
    function setQuestion() {
        setQuestionss({ ...QnA[question.i + 1], i: question.i + 1, page: question.page + 1 })
Jiwon Yoon's avatar
Jiwon Yoon committed
23
    }
baesangjune's avatar
home    
baesangjune committed
24
25
26
27
28
29
30
    let handleChange = (ev) => {
        ev.preventDefault()  //새로고침 안되도록
        setSelected(ev.target.value)  //selected값 변경
        let slt = ev.target.value  //slt에 선택한값 받아옴

        let count = question.Choose.length  //이거 정확히 뭘로할지 모르겠어요 ㅜㅜ

Jiwon Yoon's avatar
Jiwon Yoon committed
31
32
        // let checked_index = -1;
        let checked_value = '';
baesangjune's avatar
home    
baesangjune committed
33
34
35
        checked_value = slt;

        //localStorage.setItem('번문제 답' + checked_value, checked_value)
Jiwon Yoon's avatar
Jiwon Yoon committed
36
        for (let i = 0; i < count; i++) {
baesangjune's avatar
home    
baesangjune committed
37
            if (ev.target.checked) {  //이거 맞는지도 잘..
Jiwon Yoon's avatar
Jiwon Yoon committed
38
                // checked_index = i;
baesangjune's avatar
home    
baesangjune committed
39
40
                checked_value = slt;
                localStorage.setItem(question.i + 1 + '번문제 답', checked_value)
Jiwon Yoon's avatar
Jiwon Yoon committed
41
42
            }
        }
baesangjune's avatar
ing    
baesangjune committed
43
    }
baesangjune's avatar
home    
baesangjune committed
44
45
46
    return (
        <>
            <img src={fight} style={{ position: "absolute", top: "65px",left: "1050px" }} alt="lion" />
Jiwon Yoon's avatar
Jiwon Yoon committed
47
            <div className="Quiz">
baesangjune's avatar
home    
baesangjune committed
48
49
50
                <h1><img src={logo} style={{ marginLeft: "450px" }} width='75' height='75' alt='question' /> <span style={{ fontSize: "75px", marginLeft: "30px" }}>{question.Q}</span></h1>
                <div style={{ textAlign: "center", marginTop: "30px", marginBottom: "30px" }}>정답을 선택하세요</div>

baesangjune's avatar
baesangjune committed
51
                <div>
baesangjune's avatar
home    
baesangjune committed
52
53
54
55
56
57
58
59
60
61
62
63
                    <form>
                        {question.Choose.map((a, index) =>
                            <div key={index}>
                                <input type="radio" name='answer' id={'anwer' + a} value={a} onClick={handleChange} style={{ marginLeft: "475px", width: "25px", height: "25px" }} />
                                <label htmlFor={a} style={{ fontSize: "40px", marginLeft: "22px" }}>{a}</label>
                            </div>
                        )}
                        <input hidden type="submit" value="확인" />    {/*버튼 숨김*/}
                    </form>
                    <p style={{ fontSize: "20px", marginLeft: "600px" }}>선택한  :<span style={{ color: "green", fontWeight: "bold", fontSize: "90px", marginLeft: "30px" }}> {selected}</span>  {/* 선택한  보여줌 */}<span style={{ marginLeft: "50px" }}>            {(question.page === QnA.length - 1)
                        ? <Link to="/end">제출</Link>
                        : <button type="button" onClick={setQuestion} style={{ width: "85px", height: "40px" }} >다음</button>
baesangjune's avatar
timer    
baesangjune committed
64

baesangjune's avatar
home    
baesangjune committed
65
66
                    }</span>
                    </p>
baesangjune's avatar
baesangjune committed
67
                </div>
baesangjune's avatar
timer    
baesangjune committed
68

baesangjune's avatar
home    
baesangjune committed
69
70
71
72




Jiwon Yoon's avatar
Jiwon Yoon committed
73
                {/* 마지막 질문일 경우 /end페이지로 이동, 그렇지 않을경우는 this.setQuestion발생 */}
baesangjune's avatar
home    
baesangjune committed
74
75
76
77
78
79
                {/* {(question.page === QnA.length - 1)
                ? <Link to="/end">제출</Link>
                : <button type="button" onClick={setQuestion} >다음</button>

            } */}

baesangjune's avatar
baesangjune committed
80
                {/* <input onKeyPress="this.enterkey()"/> */}
baesangjune's avatar
home    
baesangjune committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102


                <p style={{ textAlign: "center", fontSize: "30px", color: "crimson" }}>
                    <Timer
                        initialTime={3050}
                        direction="backward"
                        checkpoints={[
                            {
                                time: 0,
                                callback: setQuestion
                                //  history.go(1)
                            }
                        ]}
                    >
                        {() => (
                            <>
                                <Timer.Seconds /> seconds
                        </>
                        )}
                    </Timer>     {/* npm i react-compound-timer */}
                </p>

Jiwon Yoon's avatar
Jiwon Yoon committed
103
            </div>
baesangjune's avatar
home    
baesangjune committed
104
105
106
107
108
        </>
    )



Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
109
110
}

baesangjune's avatar
home    
baesangjune committed
111
112

export default Quiz;