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

let list = []

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

Jiwon Yoon's avatar
choose    
Jiwon Yoon committed
8
9
let c = {}

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
10

Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
function AdminQuiz() {

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
13

Jiwon Yoon's avatar
Jiwon Yoon committed
14
15
16
17
18
19
    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
20
21
22

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

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

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

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

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

Jiwon Yoon's avatar
Jiwon Yoon committed
36
37
38
        setAnswer(event.target.value)
    }

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
39
40


Jiwon Yoon's avatar
Jiwon Yoon committed
41
42
43
44
45
    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
46
                        <input type="radio" name="answer" />
Jiwon Yoon's avatar
Jiwon Yoon committed
47
48
                    </div>
                </div>
Jiwon Yoon's avatar
choose    
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
67
        if (!question) {
            alert('문제가 입력되지 않았습니다.')
        }
        else if (!choose) {
            alert('보기가 입력되지 않았습니다.')
        }
        else if (!answer) {
            alert('정답이 입력되지 않았습니다.')
        }
        else {
            console.log(QnAadmin)
Jiwon Yoon's avatar
choose    
Jiwon Yoon committed
68
69
            QnAadmin['Choose']=Object.values(c)                                                                                                                                                                                                           

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
70
71
72
73
74
            QnAadmins.push(QnAadmin)
            localStorage.setItem('QnA', JSON.stringify(QnAadmins))
            console.log(QnAadmins)
            alert('입력이 완료되었습니다.')
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

    }

    // function handleChangeQuestion(){}


    return (
        <div className="" id="Quiz1">
            <h2 className="p-3 border text-center">문제만들기</h2>
            <div className="p-3 borber">
                <div className="p-3 ">
                    <span className="font-weight-bold mr-2 h3">1.</span>
                    <input type="text" className="form-control" onChange={handleChangeQuestion} placeholder="문제를 입력하세요" />
                </div>
                <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>
                        </div>
Jiwon Yoon's avatar
choose    
Jiwon Yoon committed
96
                        <input type="text" className="form-control" id='1' placeholder="보기를 입력하세요" onChange={handleChangeChoose} />
Jiwon Yoon's avatar
Jiwon Yoon committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
                    </div>
                    {list.map((element) => element)}
                    <button className="btn btn-outline-secondary btn-sm" onClick={addSelectOption}>+</button>
                </div>

                <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