Quiz.js 5.14 KB
Newer Older
JeongYeonwoo's avatar
JeongYeonwoo committed
1
import React, { useState, useEffect } 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


JeongYeonwoo's avatar
JeongYeonwoo committed
7
let localQnA = JSON.parse(localStorage.getItem('QnA'))
JeongYeonwoo's avatar
JeongYeonwoo committed
8
9
10



JeongYeonwoo's avatar
JeongYeonwoo committed
11
let Answers = []
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
12

13
function Quiz() {
JeongYeonwoo's avatar
JeongYeonwoo committed
14
15
16
17
18
19
20
    let Time = 0
    if (JSON.parse(localStorage.getItem("Set")) === null) {
        Time = 30010
    }
    else {
        Time = Number(JSON.parse(localStorage.getItem("Set"))[0].Time)
    } //수정. 추가. 새로 설정한 시간으로 설정
JeongYeonwoo's avatar
JeongYeonwoo committed
21
    const [question, setQuestion] = useState({
JeongYeonwoo's avatar
JeongYeonwoo committed
22
        ...localQnA[0]
23
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
24
    const [selected, setSelected] = useState("")  //선택한 답을 보여줄 것
25

JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
26
    const [timeout, settimeout] = useState(false)
JeongYeonwoo's avatar
JeongYeonwoo committed
27
28
29
    //for each 사용하기
    let Solutions = []          //빈 배열
    console.log(localQnA)
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
30

JeongYeonwoo's avatar
JeongYeonwoo committed
31
32
33
34
35
36
37
38
39
40
41
    localQnA.forEach((element) => {
        Solutions.push(Number(element.A))
    });
    useEffect(() => {
        for (let i = 1; i <= Solutions.length; i++) {
            Answers.push(0)
            localStorage.setItem('Answers', JSON.stringify(Answers))
            localStorage.setItem("Solutions", JSON.stringify(Solutions))
        }
        // eslint-disable-next-line
    }, [])
JeongYeonwoo's avatar
JeongYeonwoo committed
42
43
44



JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
45
    function handleQuestion() {
JeongYeonwoo's avatar
JeongYeonwoo committed
46
        setQuestion({ ...localQnA[question.N] })
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
47
        setSelected("") //페이지 넘어가면 selected 초기화
48
    }
JeongYeonwoo's avatar
updated    
JeongYeonwoo committed
49

50
51
    let handleChange = (ev) => {
        setSelected(ev.target.value)  //selected값 변경
JeongYeonwoo's avatar
JeongYeonwoo committed
52
        Answers[question.N - 1] = Number(ev.target.id) + 1
JeongYeonwoo's avatar
JeongYeonwoo committed
53
        localStorage.setItem('Answers', JSON.stringify(Answers))
54
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
55
    return (
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
56
        <>
JeongYeonwoo's avatar
JeongYeonwoo committed
57
58
59
60
61
62
63
            <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
64
                    </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
65
66
                    <div className="col-md-auto">
                        <div className="h2 mt-2">
JeongYeonwoo's avatar
JeongYeonwoo committed
67
                            {question.Q}
JeongYeonwoo's avatar
JeongYeonwoo committed
68
                        </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
69
70
71
72
73
74
75
                        <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
76
                                )}
JeongYeonwoo's avatar
JeongYeonwoo committed
77
78
79
                            </form>
                            <span className="h5 font-weight-bold"> Your Answer :</span>
                            <span className="h2 font-weight-bold text-danger"> {selected}</span>  {/* 선택한  보여줌 */}
JeongYeonwoo's avatar
JeongYeonwoo committed
80
                            <div className="text-center my-3"> {(question.N - 1 === localQnA.length - 1)
JeongYeonwoo's avatar
JeongYeonwoo committed
81
                                ? <Link to="/end">
JeongYeonwoo's avatar
JeongYeonwoo committed
82
                                    <button className="btn btn-outline-success" /*onClick={localStorage.setItem('Solutions', JSON.stringify(Solutions))}*/>제출</button>
JeongYeonwoo's avatar
JeongYeonwoo committed
83
84
85
86
87
88
                                </Link>
                                : <button type="button" className="btn btn-outline-dark" onClick={handleQuestion}>다음</button>
                            }
                            </div>
                            <p className="h3 text-center text-danger ">
                                <Timer
JeongYeonwoo's avatar
JeongYeonwoo committed
89
90
                                    initialTime={Time} // 수정. 추가. 새로설정한 시간으로 설정. 하지만 새로고침해야 적용됨
                                    //    initialTime={Number(JSON.parse(localStorage.getItem("Set"))[0].Time)}
JeongYeonwoo's avatar
JeongYeonwoo committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
                                    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
113
114
115
                    </div>
                </div>
            </div>
JeongYeonwoo's avatar
JeongYeonwoo committed
116
            {timeout ? <Redirect to='/end' /> : ''}
JeongYeonwoo's avatar
quiz    
JeongYeonwoo committed
117
        </>
JeongYeonwoo's avatar
JeongYeonwoo committed
118
    )
119

JeongYeonwoo's avatar
JeongYeonwoo committed
120
121


122
123
124
}


JeongYeonwoo's avatar
JeongYeonwoo committed
125
export default Quiz;