Admin.js 3.12 KB
Newer Older
baesangjune's avatar
.    
baesangjune committed
1
import React, { useState, useEffect, useRef } from 'react';
baesangjune's avatar
.    
baesangjune committed
2
3
import AdminQuiz from './AdminQuiz'
import AdminSetting from './AdminSetting'
baesangjune's avatar
baesangjune committed
4

baesangjune's avatar
.    
baesangjune committed
5
let QuizBtn = []
baesangjune's avatar
baesangjune committed
6

baesangjune's avatar
push    
baesangjune committed
7
function Admin() {
baesangjune's avatar
.    
baesangjune committed
8
9
    const [result, setresult] = useState('')
    const [QuizBtnCount, setQuizBtnCount] = useState(4)
baesangjune's avatar
.    
baesangjune committed
10
11
    const indexRef = useRef(0)
    const dbRef = useRef([])
baesangjune's avatar
push    
baesangjune committed
12

baesangjune's avatar
.    
baesangjune committed
13
14
15
16
17
    useEffect(() => {
        if (JSON.parse(localStorage.getItem('QnA'))) {
            dbRef.current=JSON.parse(localStorage.getItem('QnA'))
        }
    }, [])
baesangjune's avatar
.    
baesangjune committed
18
19
    function handleClickSetting() {
        setresult(<AdminSetting />)
baesangjune's avatar
baesangjune committed
20
    }
baesangjune's avatar
push    
baesangjune committed
21

baesangjune's avatar
.    
baesangjune committed
22
    function handleClickQuiz(e) {
baesangjune's avatar
.    
baesangjune committed
23
24
        indexRef.current = Number(e.target.id) - 1
        // console.log(dbRef.current, indexRef.current)
baesangjune's avatar
ddd    
baesangjune committed
25

baesangjune's avatar
.    
baesangjune committed
26
        if(dbRef.current[e.target.id-1]){
baesangjune's avatar
.    
baesangjune committed
27
            setresult(<AdminQuiz quizNum={e.target.id} fn={handleClickSave} data={dbRef.current[e.target.id-1]} />)  //설명 부탇드립니다.
baesangjune's avatar
.    
baesangjune committed
28
29
        }
        else{
baesangjune's avatar
.    
baesangjune committed
30
            dbRef.current[e.target.id-1] = {'Q':'문제를 입력하세요', 'Choose':['0','0','0','0'], 'A':'정답을 입력하세요'}
baesangjune's avatar
.    
baesangjune committed
31
32
            setresult(<AdminQuiz quizNum={e.target.id} fn={handleClickSave} data={dbRef.current[e.target.id-1]} />)
        }
baesangjune's avatar
.    
baesangjune committed
33
    }
baesangjune's avatar
.    
baesangjune committed
34

baesangjune's avatar
.    
baesangjune committed
35
    function handleClickAddQuiz() {
baesangjune's avatar
.    
baesangjune committed
36
        QuizBtn.push(
baesangjune's avatar
.    
baesangjune committed
37
38
            <button type='button' className='btn btn-outline-primary' id={QuizBtnCount} onClick={handleClickQuiz}>Quiz {QuizBtnCount}</button>
        )
baesangjune's avatar
.    
baesangjune committed
39
        setQuizBtnCount(QuizBtnCount + 1)
baesangjune's avatar
.    
baesangjune committed
40
    } //퀴즈 추가버튼
baesangjune's avatar
baesangjune committed
41

baesangjune's avatar
.    
baesangjune committed
42
43
44
45
    function handleClickSave(QnAadmin) {
        dbRef.current[indexRef.current] = QnAadmin
        localStorage.setItem('QnA', JSON.stringify(dbRef.current))

baesangjune's avatar
.    
baesangjune committed
46
    } //설명 부탁드립니다.
baesangjune's avatar
baesangjune committed
47
48
    return (
        <>
baesangjune's avatar
.    
baesangjune committed
49
 <div className="container-fluid bg-light">
baesangjune's avatar
.    
baesangjune committed
50
                <div className="row justify-content-md-center mt-5">
baesangjune's avatar
.    
baesangjune committed
51
52
53
                    <div className="col-2 text-center border py-5 bg-white">
                        <div className="py-3 border-top border-bottom">
                            <h5 className="font-weight-bold">설정</h5>
baesangjune's avatar
.    
baesangjune committed
54
                        </div>
baesangjune's avatar
.    
baesangjune committed
55
56
                        <div className="border-bottom py-4">
                            <button type="button" className="btn btn-info" onClick={handleClickSetting}>Setting</button>
baesangjune's avatar
.    
baesangjune committed
57
                        </div>
baesangjune's avatar
.    
baesangjune committed
58
59
60
61
                        <div className="btn-group-vertical py-4">
                            <button type="button" className="btn btn-outline-primary" id={1} onClick={handleClickQuiz}>Quiz 1</button>
                            <button type="button" className="btn btn-outline-primary" id={2} onClick={handleClickQuiz}>Quiz 2</button>
                            <button type="button" className="btn btn-outline-primary" id={3} onClick={handleClickQuiz}>Quiz 3</button>
baesangjune's avatar
.    
baesangjune committed
62
63
64
65
                            {QuizBtn.map((element) => element)}
                            <button type="button" className="btn btn-outline-primary" onClick={handleClickAddQuiz}>+</button>

                        </div>
baesangjune's avatar
baesangjune committed
66
                    </div>
baesangjune's avatar
baesangjune committed
67

baesangjune's avatar
.    
baesangjune committed
68
69
                    <div className="col-8 ">
                        {result}
baesangjune's avatar
baesangjune committed
70
71
                    </div>
                </div>
baesangjune's avatar
baesangjune committed
72
            </div>
baesangjune's avatar
baesangjune committed
73

baesangjune's avatar
admin    
baesangjune committed
74

baesangjune's avatar
.    
baesangjune committed
75
        </>
baesangjune's avatar
baesangjune committed
76
77
78
79
    )
}

export default Admin