compare.js 3.05 KB
Newer Older
baesangjune's avatar
.    
baesangjune committed
1
import React, { useState, useEffect, useRef } from 'react';
baesangjune's avatar
.    
baesangjune committed
2
3
4
5
6
7
8
9
10
import AdminQuiz from './AdminQuiz'
import AdminSetting from './AdminSetting'

let QuizBtn = []

function Admin() {

    const [result, setresult] = useState('')
    const [QuizBtnCount, setQuizBtnCount] = useState(4)
baesangjune's avatar
.    
baesangjune committed
11
12
    const indexRef = useRef(0)
    const dbRef = useRef([])
baesangjune's avatar
.    
baesangjune committed
13

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

    function handleClickQuiz(e) {
baesangjune's avatar
.    
baesangjune committed
24
25
26
27
28
29
30
31
32
33
        indexRef.current = Number(e.target.id) - 1
        // console.log(dbRef.current, indexRef.current)

        if(dbRef.current[e.target.id-1]){
            setresult(<AdminQuiz quizNum={e.target.id} fn={handleClickSave} data={dbRef.current[e.target.id-1]} />)
        }
        else{
            dbRef.current[e.target.id-1] = {'Q':'x', 'Choose':[0,0,0,0], 'A':'x'}
            setresult(<AdminQuiz quizNum={e.target.id} fn={handleClickSave} data={dbRef.current[e.target.id-1]} />)
        }
baesangjune's avatar
.    
baesangjune committed
34
35
36
37
38
39
40
41
42
    }

    function handleClickAddQuiz() {
        QuizBtn.push(
            <button type='button' className='btn btn-outline-primary' id={QuizBtnCount} onClick={handleClickQuiz}>Quiz {QuizBtnCount}</button>
        )
        setQuizBtnCount(QuizBtnCount + 1)
    }

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

    }
baesangjune's avatar
.    
baesangjune committed
49
50
    return (
        <>
baesangjune's avatar
.    
baesangjune committed
51
            <div className="container-fluid bg-light">
baesangjune's avatar
.    
baesangjune committed
52
                <div className="row justify-content-md-center mt-5">
baesangjune's avatar
.    
baesangjune committed
53
54
55
                    <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
56
                        </div>
baesangjune's avatar
.    
baesangjune committed
57
58
                        <div className="border-bottom py-4">
                            <button type="button" className="btn btn-info" onClick={handleClickSetting}>Setting</button>
baesangjune's avatar
.    
baesangjune committed
59
                        </div>
baesangjune's avatar
.    
baesangjune committed
60
61
62
63
                        <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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
                            {QuizBtn.map((element) => element)}
                            <button type="button" className="btn btn-outline-primary" onClick={handleClickAddQuiz}>+</button>

                        </div>
                    </div>

                    <div className="col-8 ">
                        {result}
                    </div>
                </div>
            </div>


        </>
    )
baesangjune's avatar
admin    
baesangjune committed
79
80
}

baesangjune's avatar
.    
baesangjune committed
81
export default Admin