AdminQuiz.js 4.25 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { useState } from 'react';
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
2
// import Admin from './Admin'
Jiwon Yoon's avatar
Jiwon Yoon committed
3
4
5

let list = []

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
6
7
8
let QnAadmins = []
let QnAadmin = {}

Jiwon Yoon's avatar
asd    
Jiwon Yoon committed
9
let savechoose = {}
Jiwon Yoon's avatar
choose    
Jiwon Yoon committed
10

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
11

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
12
function AdminQuiz(props) {
Jiwon Yoon's avatar
Jiwon Yoon committed
13

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
14

Jiwon Yoon's avatar
Jiwon Yoon committed
15
16
17
18
    const [question, setQuestion] = useState('')
    const [choose, setChoose] = useState('')
    const [answer, setAnswer] = useState('')
    const [selectOption, setselectOption] = useState(1)
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
19
    const [v, setv] = useState('')
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21

    const handleChangeQuestion = (event) => {
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
22
        QnAadmin['Q'] = event.target.value
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
23
        setv(event.target.value)
Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
        setQuestion(event.target.value)
    }
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
26

Jiwon Yoon's avatar
Jiwon Yoon committed
27
    const handleChangeChoose = (event) => {
Jiwon Yoon's avatar
asd    
Jiwon Yoon committed
28
        savechoose[event.target.id] = event.target.value
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
29

Jiwon Yoon's avatar
choose    
Jiwon Yoon committed
30
        // QnAadmin['Choose'] = event.target.value
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
31

Jiwon Yoon's avatar
Jiwon Yoon committed
32
33
34
        setChoose(event.target.value)
    }
    const handleChangeanswer = (event) => {
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
35
36
37

        QnAadmin['A'] = event.target.value

Jiwon Yoon's avatar
Jiwon Yoon committed
38
39
40
41
42
43
44
45
        setAnswer(event.target.value)
    }

    function addSelectOption() {
        list.push(
            <div className="input-group">
                <div className="input-group-prepend">
                    <div className="input-group-text">
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
46
                        <input type="radio" checked={false} name="answer" />
Jiwon Yoon's avatar
Jiwon Yoon committed
47
48
                    </div>
                </div>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
49
                <input type="text" class="form-control" id={String(selectOption + 1)} placeholder="보기를 입력하세요" onChange={handleChangeChoose} />
Jiwon Yoon's avatar
Jiwon Yoon committed
50
51
52
53
54
            </div>
        )
        setselectOption(selectOption + 1)
    }

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
55

Jiwon Yoon's avatar
Jiwon Yoon committed
56
    function handleClick() {
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
57
58
59
60
61
62
63
64
65
66
        if (!question) {
            alert('문제가 입력되지 않았습니다.')
        }
        else if (!choose) {
            alert('보기가 입력되지 않았습니다.')
        }
        else if (!answer) {
            alert('정답이 입력되지 않았습니다.')
        }
        else {
Jiwon Yoon's avatar
l    
Jiwon Yoon committed
67
            // console.log(QnAadmin)
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
68
            QnAadmin['Choose'] = Object.values(savechoose)
Jiwon Yoon's avatar
l    
Jiwon Yoon committed
69
            QnAadmin['N'] = String(props.QuizNum)
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
70
            QnAadmins.push(QnAadmin)
Jiwon Yoon's avatar
asd    
Jiwon Yoon committed
71
            QnAadmin = {}
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
72
73
74
75
            localStorage.setItem('QnA', JSON.stringify(QnAadmins))
            console.log(QnAadmins)
            alert('입력이 완료되었습니다.')
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
76
77

    }
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
78
79
80
81
    function onFocusHandler(){
        document.getElementById("inputQuiz").value=""
        setv("")
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
82
83
84
85


    return (
        <div className="" id="Quiz1">
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
86
            <h2 className="p-3 border text-center">{String(props.QuizNum)} 문제만들기</h2>
Jiwon Yoon's avatar
Jiwon Yoon committed
87
88
            <div className="p-3 borber">
                <div className="p-3 ">
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
89
                    <span className="font-weight-bold mr-2 h3">1. 문제 입력하기</span>
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
90
                    <input type="text" id="inputQuiz" className="form-control" onFocus={onFocusHandler} value={props.QuizNum<4?props.data[props.QuizNum-1].Q:v} onChange={handleChangeQuestion} placeholder="문제를 입력하세요" />
Jiwon Yoon's avatar
Jiwon Yoon committed
91
92
                </div>
                <div className="p-3 ">
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
93
94
95
96
97
                    <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">
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
98
                                    <input type="radio" checked={false} name="answer" />
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
99
                                </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
100
                            </div>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
101
                            <input type="text" className="form-control" id='1' placeholder="보기를 입력하세요" onChange={handleChangeChoose} />
Jiwon Yoon's avatar
Jiwon Yoon committed
102
                        </div>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
103
104
                        {list.map((element) => element)}
                        <button className="btn btn-outline-secondary btn-sm" onClick={addSelectOption}>+</button>
Jiwon Yoon's avatar
Jiwon Yoon committed
105
106
107
                    </div>
                </div>

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
108

Jiwon Yoon's avatar
Jiwon Yoon committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
                <div className="p-3 ">
                    <span className="font-weight-bold mr-2 h3">정답:</span>
                    <input type="text" onChange={handleChangeanswer} placeholder="정답을 입력하세요" />
                </div>

                <label for="inputLogin" className="d-flex justify-content-center">
                    <button className="mt-4 btn btn-dark" onClick={handleClick}>작성완료</button>
                </label>
            </div>
        </div>
    )
}

export default AdminQuiz