Menu.js 1.57 KB
Newer Older
우지원's avatar
우지원 committed
1
import axios from 'axios';
우지원's avatar
0113    
우지원 committed
2
import React, { useState, useEffect } from 'react';
Choi Ga Young's avatar
Choi Ga Young committed
3
import { Navbar, Nav, Button } from 'react-bootstrap';
우지원's avatar
우지원 committed
4
import { handleLogout, isAuthenticated } from '../utils/auth';
Choi Ga Young's avatar
Choi Ga Young committed
5

우지원's avatar
우지원 committed
6
7
8
const INIT_USER = {
  username: ''
}
우지원's avatar
0113    
우지원 committed
9
10


Choi Ga Young's avatar
Choi Ga Young committed
11
function Menu() {
우지원's avatar
우지원 committed
12
  const user = isAuthenticated()
우지원's avatar
우지원 committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  const [userName, setUsername] = useState(INIT_USER)

  async function getLoginedUser() {
    const userId = localStorage.getItem('user')
    const response = await axios.get(`/users/${userId}`)
    console.log(response.data.username)
    const resName = response.data.username
  
    // const resName = JSON.stringify(response.data.username)
    // console.log(resName)
    setUsername(resName)
  }
  console.log(userName)

  useEffect((user) => {
    if (localStorage.getItem('user')) {
      getLoginedUser(user)
    }
  }, [])
우지원's avatar
0113    
우지원 committed
32

Choi Ga Young's avatar
Choi Ga Young committed
33
34
35
36

  return (
    <Navbar bg="dark" variant="dark">
      <Navbar.Brand href="/home">YDK Messenger</Navbar.Brand>
우지원's avatar
우지원 committed
37
38
39

      {user ?
        <>
우지원's avatar
우지원 committed
40
          <div className='ml-1 mr-2' style={{ color: 'white' }}>{userName}  환영합니다</div>
우지원's avatar
우지원 committed
41
42
43
44
45
46
47
48
49
50
51
52
          <Nav className="mr-auto">
            <Nav.Link href="/home">Home</Nav.Link>
            <Nav.Link href="/profile">Profile</Nav.Link>
            <Nav.Link href="/hello">Hello</Nav.Link>
          </Nav>
          <Button className="ml-auto" onClick={() => handleLogout()} variant="light" className="ml-3">Logout</Button>
        </>
        : <Nav className="ml-auto">
          <Nav.Link href='/login'>로그인</Nav.Link>
          <Nav.Link href='/signup'>회원가입</Nav.Link>
        </Nav>}

Choi Ga Young's avatar
Choi Ga Young committed
53
54
55
56
57
    </Navbar>
  )
}

export default Menu