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

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

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

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

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

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

31
32
33
34
35
36
37
38
39
40
41
    function acheck() {
        axios.get(`/users/${localStorage.getItem('_id')}`)
            .then(res => {
                if (res.data.role == "admin") {
                    setUser(res.data)
                }
            }).catch(err => {
                alert(err.error)
            });
    }

Ha YeaJin's avatar
Ha YeaJin committed
42
    if (state) return <Redirect to="/" />
Ha YeaJin's avatar
pages    
Ha YeaJin committed
43
    return (
Ha YeaJin's avatar
Ha YeaJin committed
44
45
46
47
48
        <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
49
50
51
52
                    <NavLink as={Link} to="/notice">공지사항</NavLink>
                    <NavLink as={Link} to="/home">대관 현황</NavLink>

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

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

Ha YeaJin's avatar
Ha YeaJin committed
63
64
65
                    {user.role === "admin" ? (
                        <NavLink as={Link} to={{
                            pathname: `/acheck/${localStorage.getItem('_id')}`,
66
67
                            state: { id: localStorage.getItem('_id') },
                        }} className="nav-link">
Ha YeaJin's avatar
Ha YeaJin committed
68
                            대관 확인/취소(관리자)</NavLink>) : null}
Ha YeaJin's avatar
Ha YeaJin committed
69
70
71
72
73
74
                </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
75
                                pathname: `/change/${localStorage.getItem('_id')}`,
76
                                state: { id: localStorage.getItem('_id') },
Ha YeaJin's avatar
Ha YeaJin committed
77
78
                            }}>  비밀번호 변경 </NavLink> / <NavLink className="p-0" onClick={logout} > 로그아웃</NavLink></small>
                    </NavLink>
Ha YeaJin's avatar
Ha YeaJin committed
79
80
81
                </Nav>
            </Navbar.Collapse>
        </MENU >
Ha YeaJin's avatar
pages    
Ha YeaJin committed
82
83
84
85
    )
}

export default Menu