AdminQuiz.js 4.58 KB
Newer Older
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
1
import React, { useState, useEffect } 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
14
15
16
17

    const [question, setQuestion] = useState('')
    const [choose, setChoose] = useState('')
    const [answer, setAnswer] = useState('')
    const [selectOption, setselectOption] = useState(1)
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    const [q, setq] = useState('')
    const [c, setc] = useState([])
    const [a, seta] = useState('')

    useEffect(()=>{
        setQuestion('')
        if(props.data[props.QuizNum-1]){
            setq(props.data[props.QuizNum-1].Q)
            setc(props.data[props.QuizNum-1].Choose)
            seta(props.data[props.QuizNum-1].A)
        }
        else {
            setq('보기를 입력하세요')
            setc(['보기를 입력하세요','보기를 입력하세요','보기를 입력하세요','보기를 입력하세요'])
            seta('보기를 입력하세요')
        }
    },[props.QuizNum])
Jiwon Yoon's avatar
Jiwon Yoon committed
35
36

    const handleChangeQuestion = (event) => {
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
37
        QnAadmin['Q'] = event.target.value
Jiwon Yoon's avatar
Jiwon Yoon committed
38
39
        setQuestion(event.target.value)
    }
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
40

Jiwon Yoon's avatar
Jiwon Yoon committed
41
    const handleChangeChoose = (event) => {
Jiwon Yoon's avatar
asd    
Jiwon Yoon committed
42
        savechoose[event.target.id] = event.target.value
Jiwon Yoon's avatar
Jiwon Yoon committed
43
44
        setChoose(event.target.value)
    }
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
45

Jiwon Yoon's avatar
a    
Jiwon Yoon committed
46
    const handleChangeanswer = (event) => {
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
47
        QnAadmin['A'] = event.target.value
Jiwon Yoon's avatar
Jiwon Yoon committed
48
49
50
51
52
53
54
55
        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
56
                        <input type="radio" checked={false} name="answer" />
Jiwon Yoon's avatar
Jiwon Yoon committed
57
58
                    </div>
                </div>
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
59
                <input type="text" class="form-control" id={String(selectOption + 1)} placeholder={c[1]} onChange={handleChangeChoose} />
Jiwon Yoon's avatar
Jiwon Yoon committed
60
61
62
63
64
            </div>
        )
        setselectOption(selectOption + 1)
    }

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
65

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

    }

    return (
        <div className="" id="Quiz1">
Jiwon Yoon's avatar
ㅠㅠ    
Jiwon Yoon committed
92
            <h2 className="p-3 border text-center bg-white font-weight-bold">{String(props.QuizNum)} 문제만들기</h2>
Jiwon Yoon's avatar
Jiwon Yoon committed
93
94
            <div className="p-3 borber">
                <div className="p-3 ">
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
95
                    <span className="font-weight-bold mr-2 h3">1. 문제 입력하기</span>
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
96
                    <input type="text" id="inputQuiz" className="form-control" value={question} onChange={handleChangeQuestion} placeholder={q} />
Jiwon Yoon's avatar
Jiwon Yoon committed
97
98
                </div>
                <div className="p-3 ">
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
99
100
101
102
103
                    <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
104
                                    <input type="radio" checked={false} name="answer" />
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
105
                                </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
106
                            </div>
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
107
                            <input type="text" className="form-control" id='1' value={choose} placeholder={c[0]} onChange={handleChangeChoose} />
Jiwon Yoon's avatar
Jiwon Yoon committed
108
                        </div>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
109
110
                        {list.map((element) => element)}
                        <button className="btn btn-outline-secondary btn-sm" onClick={addSelectOption}>+</button>
Jiwon Yoon's avatar
Jiwon Yoon committed
111
112
113
                    </div>
                </div>

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
114

Jiwon Yoon's avatar
Jiwon Yoon committed
115
116
                <div className="p-3 ">
                    <span className="font-weight-bold mr-2 h3">정답:</span>
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
117
                    <input type="text" value={answer} onChange={handleChangeanswer} placeholder={a} />
Jiwon Yoon's avatar
Jiwon Yoon committed
118
119
120
121
122
123
124
125
126
127
128
                </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