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

Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
6

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



baesangjune's avatar
..    
baesangjune committed
11
let Answers = []
baesangjune's avatar
0805    
baesangjune committed
12

baesangjune's avatar
home    
baesangjune committed
13
function Quiz() {
baesangjune's avatar
baesangjune committed
14
    const [question, setQuestion] = useState({
baesangjune's avatar
..    
baesangjune committed
15
        ...localQnA[0]
baesangjune's avatar
home    
baesangjune committed
16
    })
baesangjune's avatar
d    
baesangjune committed
17
    const [selected, setSelected] = useState("")  //선택한 답을 보여줄 것
baesangjune's avatar
baesangjune committed
18

baesangjune's avatar
baesangjune committed
19
    const [timeout, settimeout] = useState(false)
baesangjune's avatar
..    
baesangjune committed
20
21
//for each 사용하기
    let Solutions = []          //빈 배열
baesangjune's avatar
.    
baesangjune committed
22
23
    console.log(localQnA)
    localQnA.map((x) => {      //A만 꺼내서 q에 추가
baesangjune's avatar
..    
baesangjune committed
24
25
        // Answers.push(0)
        return Solutions.push(x.A)
baesangjune's avatar
.    
baesangjune committed
26
    })
baesangjune's avatar
..    
baesangjune committed
27
28
29
30
31
32
33
34
35
36
    useEffect(()=>{for (let i=1 ; i <= Solutions.length;i++){
        Answers.push(0)
        localStorage.setItem('Answers',JSON.stringify(Answers))
        localStorage.setItem("Solutions", JSON.stringify(Solutions)) 
    }},[])
    
      
  
  //그걸 로컬에 저장
//foreach 쓰기
baesangjune's avatar
.    
baesangjune committed
37
38
39
40
41
42
43
    let finalQnA = localQnA.map((x, index) => {
        //delete x.A                       //A 삭제
        return { ...x, N: index + 1 }    //N 추가
    })



baesangjune's avatar
0805    
baesangjune committed
44
    function handleQuestion() {
baesangjune's avatar
..    
baesangjune committed
45
        setQuestion({ ...localQnA[question.N] })
baesangjune's avatar
d    
baesangjune committed
46
        setSelected("") //페이지 넘어가면 selected 초기화
Jiwon Yoon's avatar
Jiwon Yoon committed
47
    }
baesangjune's avatar
0805    
baesangjune committed
48

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



Jiwon Yoon's avatar
quiz    
Jiwon Yoon committed
120
121
}

baesangjune's avatar
home    
baesangjune committed
122

baesangjune's avatar
baesangjune committed
123
export default Quiz;