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

const QnA = [
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
7
8
9
    { Q: "6 X 4 = ?", Choose: [6, 12, 18, 24], N: 1 },
    { Q: "3 + 3 = ?", Choose: [2, 4, 6, 8], N: 2 },
    { Q: "3 - 1 = ?", Choose: [1, 2, 3, 4], N: 3 }
10
11
]

JeongYeonwoo's avatar
JeongYeonwoo committed
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
let a = localStorage.getItem('QnA')
let b = JSON.parse(a)

// let q=[]
// let p =b.map((x)=>{
//     q.push(x.A)
// })
// localStorage.setItem("userAnswer",JSON.stringify(q))

// let c = b.map((x,index) =>{
//     delete x.A
//     return {...x , N : index+1}
// })



// const [z, setz] = useState({
//     ...c[0]
// })





let Answers = [0, 0, 0]
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
37
localStorage.setItem('Answers', JSON.stringify(Answers))
JeongYeonwoo's avatar
JeongYeonwoo committed
38
let Solutions = [4, 3, 2]
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
39
localStorage.setItem('Solutions', JSON.stringify(Solutions))
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
40

41
function Quiz() {
JeongYeonwoo's avatar
JeongYeonwoo committed
42
    const [question, setQuestion] = useState({
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
43
        ...QnA[0]
44
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
45
    const [selected, setSelected] = useState("")  //선택한 답을 보여줄 것
46

JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
47
48
    const [timeout, settimeout] = useState(false)

JeongYeonwoo's avatar
JeongYeonwoo committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    let q = []          //빈 배열
    console.log(b)
    b.map((x) => {      //A만 꺼내서 q에 추가
        q.push(x.A)
    })
    console.log(q, b)
    localStorage.setItem("userAnswer", JSON.stringify(q))  //그걸 로컬에 저장

    let c = b.map((x, index) => {
        //delete x.A                       //A 삭제
        return { ...x, N: index + 1 }    //N 추가
    })
    console.log(c)



JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
65
    function handleQuestion() {
JeongYeonwoo's avatar
JeongYeonwoo committed
66
        setQuestion({ ...QnA[question.N] })
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
67
        setSelected("") //페이지 넘어가면 selected 초기화
68
    }
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
69

70
71
    let handleChange = (ev) => {
        setSelected(ev.target.value)  //selected값 변경
JeongYeonwoo's avatar
JeongYeonwoo committed
72
        Answers[question.N - 1] = Number(ev.target.id) + 1
JeongYeonwoo's avatar
JeongYeonwoo committed
73
        localStorage.setItem('Answers', JSON.stringify(Answers))
74
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
75
    return (
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
76
        <>
JeongYeonwoo's avatar
JeongYeonwoo committed
77
78
79
80
81
82
83
            <div className="container-fluid">
                <div className="text-center h2 font-weight-bold bg-warning py-2">미적분학 퀴즈</div>
                <div className="row justify-content-md-center" >
                    <div className="col text-right">
                        <h1>
                            <img src={logo} width='50' height='50' alt='question' />
                        </h1>
JeongYeonwoo's avatar
JeongYeonwoo committed
84
                    </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
85
86
87
                    <div className="col-md-auto">
                        <div className="h2 mt-2">
                            {question.Q} {b[0].Q}
JeongYeonwoo's avatar
JeongYeonwoo committed
88
                        </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
89
90
91
92
93
94
95
                        <div className="mt-2">
                            <form>
                                {question.Choose.map((a, index) =>
                                    <div>
                                        <input type="radio" name='answer' id={index} value={a} onChange={handleChange} checked={selected === String(a)} />
                                        <label className="font-weight-bold" htmlFor={a}>{a}</label>
                                    </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
96
                                )}
JeongYeonwoo's avatar
JeongYeonwoo committed
97
98
99
100
101
102
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
                            </form>
                            <span className="h5 font-weight-bold"> Your Answer :</span>
                            <span className="h2 font-weight-bold text-danger"> {selected}</span>  {/* 선택한  보여줌 */}
                            <div className="text-center my-3"> {(question.N - 1 === QnA.length - 1)
                                ? <Link to="/end">
                                    <button className="btn btn-outline-success" onClick={localStorage.setItem('Solutions', JSON.stringify(Solutions))}>제출</button>
                                </Link>
                                : <button type="button" className="btn btn-outline-dark" onClick={handleQuestion}>다음</button>
                            }
                            </div>
                            <p className="h3 text-center text-danger ">
                                <Timer
                                    initialTime={3001000}
                                    direction="backward"
                                    checkpoints={[
                                        {
                                            time: 1,
                                            callback: () => alert('시간이 초과되었습니다.'),
                                        },
                                        {
                                            time: 0,
                                            callback: () => settimeout(true),

                                        }
                                    ]}
                                >
                                    {() => (
                                        <>
                                            <Timer.Minutes /> : <Timer.Seconds></Timer.Seconds> / 30 : 00                        </>
                                    )}
                                </Timer>     {/* npm i react-compound-timer */}
                            </p>
                        </div>
                    </div>
                    <div className="col">
JeongYeonwoo's avatar
JeongYeonwoo committed
132
133
134
                    </div>
                </div>
            </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
135
            {timeout ? <Redirect to='/end' /> : ''}
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
136
        </>
JeongYeonwoo's avatar
JeongYeonwoo committed
137
    )
138

JeongYeonwoo's avatar
JeongYeonwoo committed
139
140


141
142
143
}


JeongYeonwoo's avatar
JeongYeonwoo committed
144
export default Quiz;