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

let list = []
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
4
let QnAadmin = {}
Jiwon Yoon's avatar
asd    
Jiwon Yoon committed
5
let savechoose = {}
Jiwon Yoon's avatar
choose    
Jiwon Yoon committed
6

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
7
function AdminQuiz(props) {
Jiwon Yoon's avatar
Jiwon Yoon committed
8
9
10
11
12

    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
13
14
15
16
    const [q, setq] = useState('')
    const [c, setc] = useState([])
    const [a, seta] = useState('')

17
18
    // useEffect(()=>{
    //     setQuestion('')
Jiwon Yoon's avatar
splice    
Jiwon Yoon committed
19
    //     if(JSON.parse(localStorage.getItem('QnA'))[props.QuizNum]){
20
21
22
    //         setq(props.data[props.QuizNum-1].Q)
    //         setc(props.data[props.QuizNum-1].Choose)
    //         seta(props.data[props.QuizNum-1].A)
Jiwon Yoon's avatar
splice    
Jiwon Yoon committed
23
            
24
25
26
27
28
    //     }
    //     else {
    //         setq('보기를 입력하세요')
    //         setc(['보기를 입력하세요','보기를 입력하세요','보기를 입력하세요','보기를 입력하세요'])
    //         seta('보기를 입력하세요')
Jiwon Yoon's avatar
splice    
Jiwon Yoon committed
29
30
            
           
31
32
    //     }
    // },[props.QuizNum])
Jiwon Yoon's avatar
Jiwon Yoon committed
33
34

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

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

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

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
63

Jiwon Yoon's avatar
Jiwon Yoon committed
64
    function handleClick() {
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
65
66
67
68
69
70
71
72
73
74
        if (!question) {
            alert('문제가 입력되지 않았습니다.')
        }
        else if (!choose) {
            alert('보기가 입력되지 않았습니다.')
        }
        else if (!answer) {
            alert('정답이 입력되지 않았습니다.')
        }
        else {
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
75
            QnAadmin['Choose'] = Object.values(savechoose)
Jiwon Yoon's avatar
l    
Jiwon Yoon committed
76
            QnAadmin['N'] = String(props.QuizNum)
Jiwon Yoon's avatar
splice    
Jiwon Yoon committed
77
            props.fn(QnAadmin)
Jiwon Yoon's avatar
asd    
Jiwon Yoon committed
78
            QnAadmin = {}
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
79
80
            alert('입력이 완료되었습니다.')
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
81
82
83

    }

baesangjune's avatar
.    
baesangjune committed
84
85


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

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