MenuBar.js
1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, { useState, useContext } from "react";
import { Menu } from "semantic-ui-react";
import { Link } from "react-router-dom";
import { AuthContext } from "../context/auth";
const MenuBar = () => {
const { user, logout } = useContext(AuthContext);
const pathname = window.location.pathname;
const path = pathname === "/" ? "home" : pathname.substr(1);
const [activeItem, setActiveItem] = useState(path);
const handleItemClick = (e, { name }) => setActiveItem(name);
const menuBar = user ? (
<Menu size="massive" color="teal" pointing secondary>
<Menu.Item name={user.username} active as={Link} to="/" />
<Menu.Menu position="right">
<Menu.Item name="logout" onClick={logout} />
</Menu.Menu>
</Menu>
) : (
<Menu size="massive" color="teal" pointing secondary>
<Menu.Item
name="home"
active={activeItem === "home"}
onClick={handleItemClick}
as={Link}
to="/"
/>
<Menu.Menu position="right">
<Menu.Item
name="login"
active={activeItem === "login"}
onClick={handleItemClick}
as={Link}
to="/login"
/>
<Menu.Item
name="register"
active={activeItem === "register"}
onClick={handleItemClick}
as={Link}
to="/register"
/>
</Menu.Menu>
</Menu>
);
return menuBar;
};
export default MenuBar;