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

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
5
let QuizBtn = []
Jiwon Yoon's avatar
ref    
Jiwon Yoon committed
6

baesangjune's avatar
baesangjune committed
7
function Admin() {
baesangjune's avatar
baesangjune committed
8

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
9
10
    const [result, setresult] = useState('')
    const [QuizBtnCount, setQuizBtnCount] = useState(4)
Jiwon Yoon's avatar
ref    
Jiwon Yoon committed
11
12
13
    const indexRef = useRef(0)
    const dbRef = useRef([])

baesangjune's avatar
baesangjune committed
14

Jiwon Yoon's avatar
ref    
Jiwon Yoon committed
15
16
17
18
19
    useEffect(() => {
        if (JSON.parse(localStorage.getItem('QnA'))) {
            dbRef.current=JSON.parse(localStorage.getItem('QnA'))
        }
    }, [])
Jiwon Yoon's avatar
Jiwon Yoon committed
20

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
21
22
    function handleClickSetting() {
        setresult(<AdminSetting />)
baesangjune's avatar
baesangjune committed
23
    }
Jiwon Yoon's avatar
h    
Jiwon Yoon committed
24

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
25
    function handleClickQuiz(e) {
Jiwon Yoon's avatar
ref    
Jiwon Yoon committed
26
27
28
29
        indexRef.current = Number(e.target.id) - 1
        console.log(dbRef.current, indexRef.current)
        setresult(<AdminQuiz quizNum={e.target.id} fn={handleClickSave} data={dbRef.current} />)

baesangjune's avatar
baesangjune committed
30
    }
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
31
32
33

    function handleClickAddQuiz() {
        QuizBtn.push(
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
34
            <button type='button' className='btn btn-outline-primary' id={QuizBtnCount} onClick={handleClickQuiz}>Quiz {QuizBtnCount}</button>
Jiwon Yoon's avatar
d    
Jiwon Yoon committed
35
        )
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
36
        setQuizBtnCount(QuizBtnCount + 1)
Jiwon Yoon's avatar
d    
Jiwon Yoon committed
37
38
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
39
    function handleClickSave(QnAadmin) {
Jiwon Yoon's avatar
ref    
Jiwon Yoon committed
40
41
42
43
        console.log(QnAadmin, indexRef.current)
        dbRef.current[indexRef.current] = QnAadmin
        localStorage.setItem('QnA', JSON.stringify(dbRef.current))

Jiwon Yoon's avatar
splice    
Jiwon Yoon committed
44
45
    }

baesangjune's avatar
baesangjune committed
46
47
    return (
        <>
Jiwon Yoon's avatar
ref    
Jiwon Yoon committed
48
            <div className="container-fluid bg-light">
Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
49
                <div className="row justify-content-md-center mt-5">
Jiwon Yoon's avatar
ㅠㅠ    
Jiwon Yoon committed
50
51
                    <div className="col-2 text-center border py-5 bg-white">
                        <div className="py-3 border-top border-bottom">
Jiwon Yoon's avatar
ref    
Jiwon Yoon committed
52
                            <h5 className="font-weight-bold">설정</h5>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
53
                        </div>
Jiwon Yoon's avatar
ㅠㅠ    
Jiwon Yoon committed
54
55
                        <div className="border-bottom py-4">
                            <button type="button" className="btn btn-info" onClick={handleClickSetting}>Setting</button>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
56
                        </div>
Jiwon Yoon's avatar
ㅠㅠ    
Jiwon Yoon committed
57
                        <div className="btn-group-vertical py-4">
Jiwon Yoon's avatar
ref    
Jiwon Yoon committed
58
59
60
                            <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>
Jiwon Yoon's avatar
A    
Jiwon Yoon committed
61
62
                            {QuizBtn.map((element) => element)}
                            <button type="button" className="btn btn-outline-primary" onClick={handleClickAddQuiz}>+</button>
baesangjune's avatar
baesangjune committed
63

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
64
                        </div>
Jiwon Yoon's avatar
h    
Jiwon Yoon committed
65
                    </div>
Jiwon Yoon's avatar
d    
Jiwon Yoon committed
66

Jiwon Yoon's avatar
ha..    
Jiwon Yoon committed
67
                    <div className="col-8 ">
Jiwon Yoon's avatar
a    
Jiwon Yoon committed
68
                        {result}
Jiwon Yoon's avatar
h    
Jiwon Yoon committed
69
70
71
72
                    </div>
                </div>
            </div>

baesangjune's avatar
admin    
baesangjune committed
73

Jiwon Yoon's avatar
A    
Jiwon Yoon committed
74
        </>
baesangjune's avatar
baesangjune committed
75
76
77
78
    )
}

export default Admin