AdminQuiz.js 5.01 KB
Newer Older
baesangjune's avatar
ddd    
baesangjune committed
1
import React, { useState } from 'react';
baesangjune's avatar
.    
baesangjune committed
2

baesangjune's avatar
.    
baesangjune committed
3
4
let list = []

baesangjune's avatar
baesangjune committed
5
6
let QnAadmins = []
let QnAadmin = {}
baesangjune's avatar
.    
baesangjune committed
7
let savechoose = {}
baesangjune's avatar
baesangjune committed
8

baesangjune's avatar
.    
baesangjune committed
9
function AdminQuiz(props) {
baesangjune's avatar
ddd    
baesangjune committed
10
11
12
13
14
15
16
17
18
    const [question, setQuestion] = useState(props.data.Q)
    // setQuestion(props.data.Q)
    // question = props.data.Q
    const [choose, setChoose] = useState(props.data.Choose)
    // setChoose(props.data.Choose)
    // choose = props.data.Choose
    const [answer, setAnswer] = useState(props.data.A)
    // setAnswer(props.data.A)
    // answer = props.data.A
baesangjune's avatar
.    
baesangjune committed
19
20
    const [selectOption, setselectOption] = useState(1)

baesangjune's avatar
.    
baesangjune committed
21
22
23
24
25
26
27
28
29
30
31

    // 만약 업데이트가 되었으면 question이랑 props.data.Q랑 값이 같아야 하는데 
    // admin페이지 가서 quiz 1에 값들 넣은 다음 콘솔찍어보고 quiz 2에도 값을 넣고 콘솔보면 어떤게 다른지 알 수 있을거야
    // 마지막으로 quiz3 눌러보면 확실하게 알 수 있을거고 값이 처음만 업데이트되고 나중에는 업데이트가 안되고있어
    console.log(question) 
    console.log(props.data.Q)
    console.log(choose)
    console.log(props.data.Choose)
    console.log(answer)
    console.log(props.data.A)

baesangjune's avatar
.    
baesangjune committed
32
33
34
35
    const handleChangeQuestion = (event) => {
        QnAadmin['Q'] = event.target.value
        setQuestion(event.target.value)
    }
baesangjune's avatar
baesangjune committed
36
    const handleChangeChoose = (event) => {
baesangjune's avatar
.    
baesangjune committed
37
        savechoose[event.target.id] = event.target.value
baesangjune's avatar
ddd    
baesangjune committed
38
        setChoose(event.target.value)
baesangjune's avatar
.    
baesangjune committed
39
40
41
42
43
44
45
46
47
48
49
    }
    const handleChangeanswer = (event) => {
        QnAadmin['A'] = event.target.value
        setAnswer(event.target.value)
    }

    function addSelectOption() {
        list.push(
            <div className="input-group">
                <div className="input-group-prepend">
                    <div className="input-group-text">
baesangjune's avatar
.    
baesangjune committed
50
                        <input type="radio" checked={false} name="answer" />
baesangjune's avatar
.    
baesangjune committed
51
52
                    </div>
                </div>
baesangjune's avatar
ddd    
baesangjune committed
53
                <input type="text" class="form-control" id={String(selectOption + 1)} value={choose[selectOption]} placeholder={(selectOption + 1) + '번 보기를 입력하세요'} onChange={handleChangeChoose} />
baesangjune's avatar
.    
baesangjune committed
54
55
56
57
58
            </div>
        )
        setselectOption(selectOption + 1)
    }

baesangjune's avatar
choose    
baesangjune committed
59

baesangjune's avatar
baesangjune committed
60
    function handleClick() {
baesangjune's avatar
.    
baesangjune committed
61
62
63
64
65
66
67
68
69
70
        if (!question) {
            alert('문제가 입력되지 않았습니다.')
        }
        else if (!choose) {
            alert('보기가 입력되지 않았습니다.')
        }
        else if (!answer) {
            alert('정답이 입력되지 않았습니다.')
        }
        else {
baesangjune's avatar
.    
baesangjune committed
71
72
73
            // console.log(QnAadmin)
            QnAadmin['Choose'] = Object.values(savechoose)
            QnAadmin['N'] = String(props.QuizNum)
baesangjune's avatar
.    
baesangjune committed
74
            QnAadmins.push(QnAadmin)
baesangjune's avatar
choose    
baesangjune committed
75
            QnAadmin = {}
baesangjune's avatar
.    
baesangjune committed
76
            localStorage.setItem('QnA', JSON.stringify(QnAadmins))
baesangjune's avatar
.    
baesangjune committed
77
            // console.log(QnAadmins)
baesangjune's avatar
.    
baesangjune committed
78
79
80
81
82
83
84
85
86
            alert('입력이 완료되었습니다.')
        }

    }

    // function handleChangeQuestion(){}


    return (
baesangjune's avatar
.    
baesangjune committed
87
88
89
90
91
92
93
94
        <>
            <div className="" id="Quiz1">
                <h2 className="p-3 border text-center">{String(props.QuizNum)} 문제만들기</h2>
                <div className="p-3 borber">
                    <div className="p-3 ">
                        <span className="font-weight-bold mr-2 h3">1. 문제 입력하기</span>
                        <input type="text" id="inputQuiz" className="form-control" onChange={handleChangeQuestion} value={question} placeholder={'문제를 입력하세요'} ></input>
                    </div>
baesangjune's avatar
.    
baesangjune committed
95
                    <div className="p-3 ">
baesangjune's avatar
.    
baesangjune committed
96
97
98
99
100
101
102
                        <span className="font-weight-bold mr-2 h3">2. 보기 입력하기</span>
                        <div className="p-3 ">
                            <div className="input-group">
                                <div className="input-group-prepend">
                                    <div className="input-group-text">
                                        <input type="radio" checked={false} name="answer" />
                                    </div>
baesangjune's avatar
ddd    
baesangjune committed
103
                                </div>
baesangjune's avatar
.    
baesangjune committed
104
                                <input type="text" className="form-control" id='1' value={choose} placeholder={'1번 보기를 입력하세요'} onChange={handleChangeChoose} />
baesangjune's avatar
.    
baesangjune committed
105
                            </div>
baesangjune's avatar
.    
baesangjune committed
106
107
                            {list.map((element) => element)}
                            <button className="btn btn-outline-secondary btn-sm" onClick={addSelectOption}>+</button>
baesangjune's avatar
choose    
baesangjune committed
108
                        </div>
baesangjune's avatar
.    
baesangjune committed
109
110
                    </div>

baesangjune's avatar
.    
baesangjune committed
111
112
113
114
                    <div className="p-3 ">
                        <span className="font-weight-bold mr-2 h3">정답:</span>
                        <input type="text" onChange={handleChangeanswer} value={answer} placeholder={'정답를 입력하세요'} />
                    </div>
baesangjune's avatar
.    
baesangjune committed
115

baesangjune's avatar
.    
baesangjune committed
116
117
118
119
                    <label for="inputLogin" className="d-flex justify-content-center">
                        <button className="mt-4 btn btn-dark" onClick={handleClick}>작성완료</button>
                    </label>
                </div>
baesangjune's avatar
.    
baesangjune committed
120
            </div>
baesangjune's avatar
ddd    
baesangjune committed
121
        </>
baesangjune's avatar
.    
baesangjune committed
122
123
124
125
    )
}

export default AdminQuiz