Quiz.js 3.98 KB
Newer Older
김민수's avatar
김민수 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React, { useState } from 'react'
import { Link } from 'react-router-dom';
import Timer from 'react-compound-timer'; // 타이머쓰기위해 import
import logo from './img_question.png'
import fight from './img_quiz.png'

const QnA = [
    { Q: "6 X 4 = ?", Choose: [6, 12, 18, 24], A: "4", N: 1 },
    { Q: "3 + 3 = ?", Choose: [2, 4, 6, 8], A: "3", N: 2 },
    { Q: "3 - 1 = ?", Choose: [1, 2, 3, 4], A: "2", N: 3 }
]



function Quiz() {
    let [question, setQuestion] = useState({
        ...QnA[0],
        i: 0,
        page: 0,
    })
    let [selected, setSelected] = useState("")  //선택한 답을 보여줄 것들

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

    let handleChange = (ev) => {
        ev.preventDefault()  //새로고침 안되도록
        setSelected(ev.target.value)  //selected값 변경

        let checked_number = ev.target.id;

                localStorage.setItem(''+(question.N), Number(checked_number)+1)
                localStorage.setItem('정답' + (question.N), QnA[question.N-1].A)
                
    }
    return (
        <>
김민수's avatar
김민수 committed
39
40
            <div className="Quiz-Main">미적분학 퀴즈</div>
            <img src={fight} className="Quiz-fitgh" alt="lion" />
김민수's avatar
김민수 committed
41
            <div className="Quiz" >
김민수's avatar
김민수 committed
42
                <h1><img src={logo} className="Quiz-logo" width='75' height='75' alt='question' /> <span className="Quiz-answer">{question.Q}</span></h1>
김민수's avatar
김민수 committed
43
44
45
46
47
48
                {/* <div style={{ marginTop: "30px", marginBottom: "30px", marginLeft:'450px', fontSize:'40px' }}>정답을 선택하세요</div> */}

                <div>
                    <form>
                        {question.Choose.map((a, index) =>
                            <div key={index}>
김민수's avatar
김민수 committed
49
                                <input type="radio" className='answer' id={index} value={a} onClick={handleChange}/>
김민수's avatar
김민수 committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
                                <label htmlFor={a} style={{ fontSize: "40px", marginLeft: "22px" }}>{a}</label>
                            </div>
                        )}
                        <input hidden type="submit" value="확인" />    {/*버튼 숨김*/}
                    </form>
                    <p style={{ fontSize: "40px", marginLeft: "480px" }}>Answer :<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={handleQuestion} style={{ width: "85px", height: "40px" }} >다음</button>

                    }</span>
                    </p>
                </div>





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

            } */}

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


                <p style={{ textAlign: "center", fontSize: "30px", color: "crimson" }}>
                    <Timer
                        initialTime={3600000}
                        direction="backward"
                        checkpoints={[
                            {
                                time: 0,
                                callback: <Link to="/end">제출</Link>
                                //  history.go(1)
                            }
                        ]}
                    >
                        {() => (
                            <>
                                <Timer.Minutes /> : <Timer.Seconds></Timer.Seconds> / 60 : 00                        </>
                        )}
                    </Timer>     {/* npm i react-compound-timer */}
                </p>

            </div>
        </>
    )



}


export default Quiz;