AdminQuiz.js 4.48 KB
Newer Older
baesangjune's avatar
.    
baesangjune committed
1
import React, { useState, useEffect } from 'react';
baesangjune's avatar
.    
baesangjune committed
2

baesangjune's avatar
.    
baesangjune committed
3
4
let list = []

baesangjune's avatar
baesangjune committed
5
6
let QnAadmins = []
let QnAadmin = {}
baesangjune's avatar
.    
baesangjune committed
7
let savechoose = {}
baesangjune's avatar
baesangjune committed
8

baesangjune's avatar
.    
baesangjune committed
9
function AdminQuiz(props) {
baesangjune's avatar
.    
baesangjune committed
10
11
12
13
14

    const [question, setQuestion] = useState('')
    const [choose, setChoose] = useState('')
    const [answer, setAnswer] = useState('')
    const [selectOption, setselectOption] = useState(1)
baesangjune's avatar
.    
baesangjune committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    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])
baesangjune's avatar
.    
baesangjune committed
32
33
34
35
36

    const handleChangeQuestion = (event) => {
        QnAadmin['Q'] = event.target.value
        setQuestion(event.target.value)
    }
baesangjune's avatar
baesangjune committed
37
    const handleChangeChoose = (event) => {
baesangjune's avatar
.    
baesangjune committed
38
        savechoose[event.target.id] = event.target.value
baesangjune's avatar
.    
baesangjune committed
39
          setChoose(event.target.value)
baesangjune's avatar
.    
baesangjune committed
40
41
42
43
44
45
46
47
48
49
50
    }
    const handleChangeanswer = (event) => {
        QnAadmin['A'] = event.target.value
        setAnswer(event.target.value)
    }

    function addSelectOption() {
        list.push(
            <div className="input-group">
                <div className="input-group-prepend">
                    <div className="input-group-text">
baesangjune's avatar
.    
baesangjune committed
51
                        <input type="radio" checked={false} name="answer" />
baesangjune's avatar
.    
baesangjune committed
52
53
                    </div>
                </div>
baesangjune's avatar
.    
baesangjune committed
54
                <input type="text" class="form-control" id={String(selectOption + 1)} placeholder={c[1]} onChange={handleChangeChoose} />
baesangjune's avatar
.    
baesangjune committed
55
56
57
58
59
            </div>
        )
        setselectOption(selectOption + 1)
    }

baesangjune's avatar
choose    
baesangjune committed
60

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

    }

    // function handleChangeQuestion(){}


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

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