|
|
|
@@ -1,169 +1,170 @@ |
|
|
|
import React, { useState, useMemo, useContext } from "react"; |
|
|
|
import { |
|
|
|
AppBar, |
|
|
|
Badge, |
|
|
|
Box, |
|
|
|
IconButton, |
|
|
|
Toolbar, |
|
|
|
Typography, |
|
|
|
List, |
|
|
|
ListItem, |
|
|
|
ListItemButton, |
|
|
|
ListItemIcon, |
|
|
|
ListItemText, |
|
|
|
useMediaQuery, |
|
|
|
} from "@mui/material"; |
|
|
|
import { useTheme } from "@mui/system"; |
|
|
|
import MenuOutlinedIcon from "@mui/icons-material/MenuOutlined"; |
|
|
|
import ShoppingBasketIcon from "@mui/icons-material/ShoppingBasket"; |
|
|
|
import LogoutIcon from "@mui/icons-material/Logout"; |
|
|
|
import Brightness4Icon from "@mui/icons-material/Brightness4"; |
|
|
|
import Brightness7Icon from "@mui/icons-material/Brightness7"; |
|
|
|
import MenuList from "../MenuList/MenuListComponent"; |
|
|
|
import Drawer from "../Drawer/DrawerComponent"; |
|
|
|
import { ColorModeContext } from "context/ColorModeContext"; |
|
|
|
import { useDispatch } from "react-redux"; |
|
|
|
import { logOut } from "features/auth/authSlice"; |
|
|
|
import React, { useState } from "react"; |
|
|
|
|
|
|
|
import AppBar from "@mui/material/AppBar"; |
|
|
|
import Box from "@mui/material/Box"; |
|
|
|
import Toolbar from "@mui/material/Toolbar"; |
|
|
|
import IconButton from "@mui/material/IconButton"; |
|
|
|
import Typography from "@mui/material/Typography"; |
|
|
|
import Menu from "@mui/material/Menu"; |
|
|
|
import MenuIcon from "@mui/icons-material/Menu"; |
|
|
|
import Container from "@mui/material/Container"; |
|
|
|
import Avatar from "@mui/material/Avatar"; |
|
|
|
import Button from "@mui/material/Button"; |
|
|
|
import Tooltip from "@mui/material/Tooltip"; |
|
|
|
import MenuItem from "@mui/material/MenuItem"; |
|
|
|
import AdbIcon from "@mui/icons-material/Adb"; |
|
|
|
import Link from "@mui/material/Link"; |
|
|
|
|
|
|
|
const pages = ["Profile", "Settings"]; |
|
|
|
const settings = ["Profile", "Logout"]; |
|
|
|
|
|
|
|
const NavbarComponent = () => { |
|
|
|
const dispatch = useDispatch(); |
|
|
|
const [openDrawer, setOpenDrawer] = useState(false); |
|
|
|
const theme = useTheme(); |
|
|
|
const matches = useMediaQuery(theme.breakpoints.down("sm")); |
|
|
|
const toggleColorMode = useContext(ColorModeContext); |
|
|
|
const [anchorElNav, setAnchorElNav] = useState(null); |
|
|
|
const [anchorElUser, setAnchorElUser] = useState(null); |
|
|
|
|
|
|
|
const handleToggleDrawer = () => { |
|
|
|
setOpenDrawer(!openDrawer); |
|
|
|
const handleOpenNavMenu = (event) => { |
|
|
|
setAnchorElNav(event.currentTarget); |
|
|
|
}; |
|
|
|
const handleOpenUserMenu = (event) => { |
|
|
|
setAnchorElUser(event.currentTarget); |
|
|
|
}; |
|
|
|
|
|
|
|
const handleLogout = () => { |
|
|
|
dispatch(logOut()); |
|
|
|
const handleCloseNavMenu = () => { |
|
|
|
setAnchorElNav(null); |
|
|
|
}; |
|
|
|
|
|
|
|
const drawerContent = useMemo( |
|
|
|
() => ( |
|
|
|
<List> |
|
|
|
<ListItemButton divider onClick={handleToggleDrawer}> |
|
|
|
<ListItemIcon> |
|
|
|
<ListItemText>Link 1</ListItemText> |
|
|
|
</ListItemIcon> |
|
|
|
</ListItemButton> |
|
|
|
<ListItem divider onClick={handleToggleDrawer}> |
|
|
|
<ListItemIcon> |
|
|
|
<ListItemText>Link 2</ListItemText> |
|
|
|
</ListItemIcon> |
|
|
|
</ListItem> |
|
|
|
<ListItem divider onClick={handleToggleDrawer}> |
|
|
|
<ListItemText>Link 3</ListItemText> |
|
|
|
</ListItem> |
|
|
|
<ListItem divider> |
|
|
|
<IconButton onClick={toggleColorMode}> |
|
|
|
<ListItemText>Toggle {theme.palette.mode} mode</ListItemText> |
|
|
|
{theme.palette.mode === "dark" ? ( |
|
|
|
<Brightness7Icon /> |
|
|
|
) : ( |
|
|
|
<Brightness4Icon /> |
|
|
|
)} |
|
|
|
</IconButton> |
|
|
|
</ListItem> |
|
|
|
</List> |
|
|
|
), |
|
|
|
[handleToggleDrawer] |
|
|
|
); |
|
|
|
const handleCloseUserMenu = () => { |
|
|
|
setAnchorElUser(null); |
|
|
|
}; |
|
|
|
|
|
|
|
return ( |
|
|
|
<AppBar |
|
|
|
elevation={2} |
|
|
|
sx={{ backgroundColor: "background.default", position: "relative" }} |
|
|
|
> |
|
|
|
<Toolbar> |
|
|
|
<Box |
|
|
|
component="div" |
|
|
|
sx={{ |
|
|
|
display: "flex", |
|
|
|
justifyContent: "space-between", |
|
|
|
alignItems: "center", |
|
|
|
width: "100%", |
|
|
|
}} |
|
|
|
> |
|
|
|
{matches ? ( |
|
|
|
<Drawer |
|
|
|
open={openDrawer} |
|
|
|
toggleOpen={handleToggleDrawer} |
|
|
|
content={drawerContent} |
|
|
|
/> |
|
|
|
) : ( |
|
|
|
<Box sx={{ display: "flex" }}> |
|
|
|
<Typography |
|
|
|
variant="h6" |
|
|
|
sx={{ |
|
|
|
marginRight: 3, |
|
|
|
cursor: "pointer", |
|
|
|
color: "text.primary", |
|
|
|
}} |
|
|
|
> |
|
|
|
Link 1 |
|
|
|
</Typography> |
|
|
|
<Typography |
|
|
|
variant="body1" |
|
|
|
sx={{ |
|
|
|
marginRight: 3, |
|
|
|
cursor: "pointer", |
|
|
|
color: "text.primary", |
|
|
|
}} |
|
|
|
> |
|
|
|
Link 2 |
|
|
|
</Typography> |
|
|
|
<Typography |
|
|
|
variant="subtitle1" |
|
|
|
sx={{ |
|
|
|
marginRight: 3, |
|
|
|
cursor: "pointer", |
|
|
|
color: "text.primary", |
|
|
|
}} |
|
|
|
> |
|
|
|
Link 3 |
|
|
|
</Typography> |
|
|
|
</Box> |
|
|
|
)} |
|
|
|
<Box> |
|
|
|
<MenuList /> |
|
|
|
<AppBar position="static"> |
|
|
|
<Container maxWidth="xl"> |
|
|
|
<Toolbar disableGutters> |
|
|
|
{/* <AdbIcon sx={{ display: { xs: "none", md: "flex" }, mr: 1 }} /> */} |
|
|
|
<Typography |
|
|
|
variant="h6" |
|
|
|
noWrap |
|
|
|
component="a" |
|
|
|
href="/" |
|
|
|
sx={{ |
|
|
|
mr: 2, |
|
|
|
display: { xs: "none", md: "flex" }, |
|
|
|
fontFamily: "monospace", |
|
|
|
fontWeight: 700, |
|
|
|
letterSpacing: ".3rem", |
|
|
|
color: "inherit", |
|
|
|
textDecoration: "none", |
|
|
|
}} |
|
|
|
> |
|
|
|
Home |
|
|
|
</Typography> |
|
|
|
|
|
|
|
<Box sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}> |
|
|
|
<IconButton |
|
|
|
size="large" |
|
|
|
aria-label="account of current user" |
|
|
|
aria-controls="menu-appbar" |
|
|
|
aria-haspopup="true" |
|
|
|
onClick={handleOpenNavMenu} |
|
|
|
color="inherit" |
|
|
|
> |
|
|
|
<MenuIcon /> |
|
|
|
</IconButton> |
|
|
|
<Menu |
|
|
|
id="menu-appbar" |
|
|
|
anchorEl={anchorElNav} |
|
|
|
anchorOrigin={{ |
|
|
|
vertical: "bottom", |
|
|
|
horizontal: "left", |
|
|
|
}} |
|
|
|
keepMounted |
|
|
|
transformOrigin={{ |
|
|
|
vertical: "top", |
|
|
|
horizontal: "left", |
|
|
|
}} |
|
|
|
open={Boolean(anchorElNav)} |
|
|
|
onClose={handleCloseNavMenu} |
|
|
|
sx={{ |
|
|
|
display: { xs: "block", md: "none" }, |
|
|
|
}} |
|
|
|
> |
|
|
|
{pages.map((page) => ( |
|
|
|
<MenuItem key={page} onClick={handleCloseNavMenu}> |
|
|
|
<Link |
|
|
|
href={`/${page.toLowerCase()}`} |
|
|
|
underline="none" |
|
|
|
textAlign="center" |
|
|
|
> |
|
|
|
{page} |
|
|
|
</Link> |
|
|
|
</MenuItem> |
|
|
|
))} |
|
|
|
</Menu> |
|
|
|
</Box> |
|
|
|
<Box |
|
|
|
<AdbIcon sx={{ display: { xs: "flex", md: "none" }, mr: 1 }} /> |
|
|
|
<Typography |
|
|
|
variant="h5" |
|
|
|
noWrap |
|
|
|
component="a" |
|
|
|
href="/" |
|
|
|
sx={{ |
|
|
|
display: "flex", |
|
|
|
justifyContent: "center", |
|
|
|
alignItems: "center", |
|
|
|
mr: 2, |
|
|
|
display: { xs: "flex", md: "none" }, |
|
|
|
flexGrow: 1, |
|
|
|
fontFamily: "monospace", |
|
|
|
fontWeight: 700, |
|
|
|
letterSpacing: ".3rem", |
|
|
|
color: "inherit", |
|
|
|
textDecoration: "none", |
|
|
|
}} |
|
|
|
> |
|
|
|
{matches ? ( |
|
|
|
<Box> |
|
|
|
<IconButton onClick={handleToggleDrawer}> |
|
|
|
<MenuOutlinedIcon /> |
|
|
|
</IconButton> |
|
|
|
</Box> |
|
|
|
) : ( |
|
|
|
<Box> |
|
|
|
<IconButton> |
|
|
|
<Badge badgeContent={3} color="primary"> |
|
|
|
<ShoppingBasketIcon color="action" /> |
|
|
|
</Badge> |
|
|
|
</IconButton> |
|
|
|
<IconButton sx={{ ml: 1 }} onClick={toggleColorMode}> |
|
|
|
{theme.palette.mode === "dark" ? ( |
|
|
|
<Brightness7Icon /> |
|
|
|
) : ( |
|
|
|
<Brightness4Icon /> |
|
|
|
)} |
|
|
|
</IconButton> |
|
|
|
<IconButton onClick={handleLogout}> |
|
|
|
<LogoutIcon /> |
|
|
|
</IconButton> |
|
|
|
</Box> |
|
|
|
)} |
|
|
|
HOME |
|
|
|
</Typography> |
|
|
|
<Box sx={{ flexGrow: 1, display: { xs: "none", md: "flex" } }}> |
|
|
|
{pages.map((page) => ( |
|
|
|
<Button |
|
|
|
key={page} |
|
|
|
onClick={handleCloseNavMenu} |
|
|
|
href={`/${page.toLowerCase()}`} |
|
|
|
sx={{ my: 2, color: "white", display: "block" }} |
|
|
|
> |
|
|
|
{page} |
|
|
|
</Button> |
|
|
|
))} |
|
|
|
</Box> |
|
|
|
|
|
|
|
<Box sx={{ flexGrow: 0 }}> |
|
|
|
<Tooltip title="Open settings"> |
|
|
|
<IconButton onClick={handleOpenUserMenu} sx={{ p: 0 }}> |
|
|
|
<Avatar alt="Remy Sharp" src="/static/images/avatar/2.jpg" /> |
|
|
|
</IconButton> |
|
|
|
</Tooltip> |
|
|
|
<Menu |
|
|
|
sx={{ mt: "45px" }} |
|
|
|
id="menu-appbar" |
|
|
|
anchorEl={anchorElUser} |
|
|
|
anchorOrigin={{ |
|
|
|
vertical: "top", |
|
|
|
horizontal: "right", |
|
|
|
}} |
|
|
|
keepMounted |
|
|
|
transformOrigin={{ |
|
|
|
vertical: "top", |
|
|
|
horizontal: "right", |
|
|
|
}} |
|
|
|
open={Boolean(anchorElUser)} |
|
|
|
onClose={handleCloseUserMenu} |
|
|
|
> |
|
|
|
{settings.map((setting) => ( |
|
|
|
<MenuItem key={setting} onClick={handleCloseUserMenu}> |
|
|
|
<Typography textAlign="center">{setting}</Typography> |
|
|
|
</MenuItem> |
|
|
|
))} |
|
|
|
</Menu> |
|
|
|
</Box> |
|
|
|
</Box> |
|
|
|
</Toolbar> |
|
|
|
</Toolbar> |
|
|
|
</Container> |
|
|
|
</AppBar> |
|
|
|
); |
|
|
|
}; |