AdminQuiz.js 3.96 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
19
20
    const [question, setQuestion] = useState('')
    const [choose, setChoose] = useState('')
    const [answer, setAnswer] = useState('')
    const [selectOption, setselectOption] = useState(1)

    const handleChangeQuestion = (event) => {
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
21
22
23

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

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

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

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

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

Jiwon Yoon's avatar
Jiwon Yoon committed
37
38
39
40
41
42
43
44
        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
choose    
Jiwon Yoon committed
45
                        <input type="radio" name="answer" />
Jiwon Yoon's avatar
Jiwon Yoon committed
46
47
                    </div>
                </div>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
48
                <input type="text" class="form-control" id={String(selectOption + 1)} placeholder="보기를 입력하세요" onChange={handleChangeChoose} />
Jiwon Yoon's avatar
Jiwon Yoon committed
49
50
51
52
53
            </div>
        )
        setselectOption(selectOption + 1)
    }

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
54

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

    }

    // function handleChangeQuestion(){}


    return (
        <div className="" id="Quiz1">
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
82
            <h2 className="p-3 border text-center">{String(props.QuizNum)} 문제만들기</h2>
Jiwon Yoon's avatar
Jiwon Yoon committed
83
84
            <div className="p-3 borber">
                <div className="p-3 ">
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
85
                    <span className="font-weight-bold mr-2 h3">1. 문제 입력하기</span>
Jiwon Yoon's avatar
Jiwon Yoon committed
86
87
88
                    <input type="text" className="form-control" onChange={handleChangeQuestion} placeholder="문제를 입력하세요" />
                </div>
                <div className="p-3 ">
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
89
90
91
92
93
94
95
                    <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" id={selectOption} name="answer" />
                                </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
96
                            </div>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
97
                            <input type="text" className="form-control" id='1' placeholder="보기를 입력하세요" onChange={handleChangeChoose} />
Jiwon Yoon's avatar
Jiwon Yoon committed
98
                        </div>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
99
100
                        {list.map((element) => element)}
                        <button className="btn btn-outline-secondary btn-sm" onClick={addSelectOption}>+</button>
Jiwon Yoon's avatar
Jiwon Yoon committed
101
102
103
                    </div>
                </div>

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
104

Jiwon Yoon's avatar
Jiwon Yoon committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
                <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