Menu.js 3.17 KB
Newer Older
Ha YeaJin's avatar
pages    
Ha YeaJin committed
1
import React, { useState, useEffect } from 'react';
CHAERIN KIM's avatar
CHAERIN KIM committed
2
import { Link, Redirect } from 'react-router-dom';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
3
import styled from 'styled-components';
Choi Ga Young's avatar
Choi Ga Young committed
4
import axios from 'axios';
Ha YeaJin's avatar
Ha YeaJin committed
5
import { Navbar, Nav, NavLink } from 'react-bootstrap';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
6

Ha YeaJin's avatar
Ha YeaJin committed
7
const MENU = styled(Navbar)`
Ha YeaJin's avatar
Ha YeaJin committed
8
9
10
    background-color: #7B031D;

    a {
Ha YeaJin's avatar
Ha YeaJin committed
11
        color : white;
12
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
13
14
15
`

function Menu() {
CHAERIN KIM's avatar
CHAERIN KIM committed
16
    const [state, setState] = useState()
17
    const [user, setUser] = useState({ role: "" })
CHAERIN KIM's avatar
CHAERIN KIM committed
18
    const name = localStorage.getItem('name');
Ha YeaJin's avatar
Ha YeaJin committed
19

CHAERIN KIM's avatar
CHAERIN KIM committed
20
    function logout() {
CHAERIN KIM's avatar
CHAERIN KIM committed
21
        localStorage.clear();
CHAERIN KIM's avatar
CHAERIN KIM committed
22
        alert("로그아웃 되었습니다.");
CHAERIN KIM's avatar
CHAERIN KIM committed
23
24
25
        setState(true);
    }

Ha YeaJin's avatar
Ha YeaJin committed
26
27
28
29
    useEffect(() => {
        acheck();
    }, [])

30
    function acheck() {
CHAERIN KIM's avatar
CHAERIN KIM committed
31
32
33
34
        axios.get(`/users/${localStorage.getItem('_id')}`, {
            headers: { authorization: localStorage.getItem('token') },
        }).then(res => {
                console.log(res.data)
35
36
37
38
39
40
41
42
                if (res.data.role == "admin") {
                    setUser(res.data)
                }
            }).catch(err => {
                alert(err.error)
            });
    }

Ha YeaJin's avatar
Ha YeaJin committed
43
    if (state) return <Redirect to="/" />
Ha YeaJin's avatar
pages    
Ha YeaJin committed
44
    return (
Ha YeaJin's avatar
Ha YeaJin committed
45
46
47
48
49
        <MENU expand="md" variant="dark">
            <Navbar.Brand href="#">대관 서비스</Navbar.Brand>
            <Navbar.Toggle aria-controls="basic-navbar-nav" />
            <Navbar.Collapse id="basic-navbar-nav">
                <Nav className="mr-auto">
Ha YeaJin's avatar
Ha YeaJin committed
50
51
52
53
                    <NavLink as={Link} to="/notice">공지사항</NavLink>
                    <NavLink as={Link} to="/home">대관 현황</NavLink>

                    <NavLink as={Link} to={{
Ha YeaJin's avatar
Ha YeaJin committed
54
55
                        pathname: `/apply/${localStorage.getItem('_id')}`,
                        state: { id: localStorage.getItem('_id') },
Ha YeaJin's avatar
Ha YeaJin committed
56
57
58
                    }} className="nav-link">대관 신청</NavLink>

                    <NavLink as={Link} to={{
Ha YeaJin's avatar
Ha YeaJin committed
59
60
61
                        pathname: `/check/${localStorage.getItem('_id')}`,
                        state: { id: localStorage.getItem('_id') },
                    }} className="nav-link">
Ha YeaJin's avatar
Ha YeaJin committed
62
63
                        대관 확인/취소</NavLink>

CHAERIN KIM's avatar
CHAERIN KIM committed
64
                    {/* {user.role === "admin" ? (
Ha YeaJin's avatar
Ha YeaJin committed
65
66
                        <NavLink as={Link} to={{
                            pathname: `/acheck/${localStorage.getItem('_id')}`,
67
68
                            state: { id: localStorage.getItem('_id') },
                        }} className="nav-link">
CHAERIN KIM's avatar
CHAERIN KIM committed
69
                            대관 확인/취소(관리자)</NavLink>) : null} */}
Ha YeaJin's avatar
Ha YeaJin committed
70
71
72
73
74
75
                </Nav>
                <Nav >
                    <NavLink>
                        <small className="d-flex flex-row justify-content-end">
                            <div className="text-white text-right font-weight-light pr-2">{name}</div>
                            <NavLink className="p-0" as={Link} to={{
Ha YeaJin's avatar
Ha YeaJin committed
76
                                pathname: `/change/${localStorage.getItem('_id')}`,
77
                                state: { id: localStorage.getItem('_id') },
Ha YeaJin's avatar
Ha YeaJin committed
78
79
                            }}>  비밀번호 변경 </NavLink> / <NavLink className="p-0" onClick={logout} > 로그아웃</NavLink></small>
                    </NavLink>
Ha YeaJin's avatar
Ha YeaJin committed
80
81
82
                </Nav>
            </Navbar.Collapse>
        </MENU >
Ha YeaJin's avatar
pages    
Ha YeaJin committed
83
84
85
86
    )
}

export default Menu