| 1 | import React from "react";
|
|---|
| 2 | import {useAuthContext} from "../configurations/AuthContext"
|
|---|
| 3 | import {AppBar, Box, Button, Container, IconButton, Menu, MenuItem, Toolbar, Typography} from "@mui/material";
|
|---|
| 4 | import MenuIcon from '@mui/icons-material/Menu';
|
|---|
| 5 | import {Link, useNavigate} from "react-router-dom";
|
|---|
| 6 | import LocalDiningIcon from '@mui/icons-material/LocalDining';
|
|---|
| 7 | import FaceIcon from '@mui/icons-material/Face';
|
|---|
| 8 | import {UserRole} from "../services/user-service";
|
|---|
| 9 |
|
|---|
| 10 | const Header = props => {
|
|---|
| 11 | const {loggedUser, loggedUserRole, logout} = useAuthContext();
|
|---|
| 12 | const [anchorElNav, setAnchorElNav] = React.useState(null);
|
|---|
| 13 | const [anchorElUser, setAnchorElUser] = React.useState(null);
|
|---|
| 14 | const navigate = useNavigate();
|
|---|
| 15 |
|
|---|
| 16 | const handleOpenNavMenu = (event) => {
|
|---|
| 17 | setAnchorElNav(event.currentTarget);
|
|---|
| 18 | };
|
|---|
| 19 | const handleOpenUserMenu = (event) => {
|
|---|
| 20 | setAnchorElUser(event.currentTarget);
|
|---|
| 21 | };
|
|---|
| 22 |
|
|---|
| 23 | const handleCloseNavMenu = () => {
|
|---|
| 24 | setAnchorElNav(null);
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | const handleCloseUserMenu = () => {
|
|---|
| 28 | setAnchorElUser(null);
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | const handleLogout = () => {
|
|---|
| 32 | logout();
|
|---|
| 33 | navigate("/");
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | return <AppBar position="static">
|
|---|
| 37 | <Container maxWidth="xl">
|
|---|
| 38 | <Toolbar disableGutters>
|
|---|
| 39 | <LocalDiningIcon sx={{display: {xs: 'none', md: 'flex'}, mr: 1}}/>
|
|---|
| 40 | <Typography
|
|---|
| 41 | variant="h6"
|
|---|
| 42 | noWrap
|
|---|
| 43 | component={Link}
|
|---|
| 44 | to='/'
|
|---|
| 45 | sx={{
|
|---|
| 46 | mr: 2,
|
|---|
| 47 | display: {xs: 'none', md: 'flex'},
|
|---|
| 48 | fontFamily: 'monospace',
|
|---|
| 49 | fontWeight: 700,
|
|---|
| 50 | letterSpacing: '.3rem',
|
|---|
| 51 | color: 'inherit',
|
|---|
| 52 | textDecoration: 'none',
|
|---|
| 53 | }}
|
|---|
| 54 | >
|
|---|
| 55 | All Restaurants
|
|---|
| 56 | </Typography>
|
|---|
| 57 |
|
|---|
| 58 | <Box sx={{flexGrow: 1, display: {xs: 'flex', md: 'none'}}}>
|
|---|
| 59 | <IconButton
|
|---|
| 60 | size="large"
|
|---|
| 61 | aria-label="account of current user"
|
|---|
| 62 | aria-controls="menu-appbar"
|
|---|
| 63 | aria-haspopup="true"
|
|---|
| 64 | onClick={handleOpenNavMenu}
|
|---|
| 65 | color="inherit"
|
|---|
| 66 | >
|
|---|
| 67 | <MenuIcon/>
|
|---|
| 68 | </IconButton>
|
|---|
| 69 | <Menu
|
|---|
| 70 | id="menu-appbar"
|
|---|
| 71 | anchorEl={anchorElNav}
|
|---|
| 72 | anchorOrigin={{
|
|---|
| 73 | vertical: 'bottom',
|
|---|
| 74 | horizontal: 'left',
|
|---|
| 75 | }}
|
|---|
| 76 | keepMounted
|
|---|
| 77 | transformOrigin={{
|
|---|
| 78 | vertical: 'top',
|
|---|
| 79 | horizontal: 'left',
|
|---|
| 80 | }}
|
|---|
| 81 | open={Boolean(anchorElNav)}
|
|---|
| 82 | onClose={handleCloseNavMenu}
|
|---|
| 83 | sx={{
|
|---|
| 84 | display: {xs: 'block', md: 'none'},
|
|---|
| 85 | }}
|
|---|
| 86 | >
|
|---|
| 87 | <MenuItem onClick={() => navigate("/restaurants")}>
|
|---|
| 88 | <Typography textAlign="center">Restaurants</Typography>
|
|---|
| 89 | </MenuItem>
|
|---|
| 90 |
|
|---|
| 91 | <MenuItem onClick={() => navigate("/menuItems")}>
|
|---|
| 92 | <Typography textAlign="center">Menu Items</Typography>
|
|---|
| 93 | </MenuItem>
|
|---|
| 94 |
|
|---|
| 95 | </Menu>
|
|---|
| 96 | </Box>
|
|---|
| 97 | <LocalDiningIcon sx={{display: {xs: 'flex', md: 'none'}, mr: 1}}/>
|
|---|
| 98 | <Typography
|
|---|
| 99 | variant="h5"
|
|---|
| 100 | noWrap
|
|---|
| 101 | component="a"
|
|---|
| 102 | to="/"
|
|---|
| 103 | sx={{
|
|---|
| 104 | mr: 2,
|
|---|
| 105 | display: {xs: 'flex', md: 'none'},
|
|---|
| 106 | flexGrow: 1,
|
|---|
| 107 | fontFamily: 'monospace',
|
|---|
| 108 | fontWeight: 700,
|
|---|
| 109 | letterSpacing: '.3rem',
|
|---|
| 110 | color: 'inherit',
|
|---|
| 111 | textDecoration: 'none',
|
|---|
| 112 | }}
|
|---|
| 113 | >
|
|---|
| 114 | All Restaurants
|
|---|
| 115 | </Typography>
|
|---|
| 116 | <Box sx={{flexGrow: 1, display: {xs: 'none', md: 'flex'}}}>
|
|---|
| 117 |
|
|---|
| 118 | <Button onClick={() => navigate("/restorants")}
|
|---|
| 119 | sx={{my: 2, color: 'white', display: 'block'}}>
|
|---|
| 120 | Restaurants
|
|---|
| 121 | </Button>
|
|---|
| 122 | <Button
|
|---|
| 123 | onClick={() => navigate("/menu-items")}
|
|---|
| 124 | sx={{my: 2, color: 'white', display: 'block'}}
|
|---|
| 125 | >
|
|---|
| 126 | Menu Items
|
|---|
| 127 | </Button>
|
|---|
| 128 | {[UserRole.Vozac, UserRole.Admin].includes(loggedUserRole?.role) && <Button
|
|---|
| 129 | onClick={() => navigate("/orders?active=true")}
|
|---|
| 130 | sx={{my: 2, color: 'white', display: 'block'}}
|
|---|
| 131 | >
|
|---|
| 132 | Active Orders
|
|---|
| 133 | </Button>}
|
|---|
| 134 | {[UserRole.Admin].includes(loggedUserRole?.role) && <Button
|
|---|
| 135 | onClick={() => navigate("/orders?active=false")}
|
|---|
| 136 | sx={{my: 2, color: 'white', display: 'block'}}
|
|---|
| 137 | >
|
|---|
| 138 | Finished Orders
|
|---|
| 139 | </Button>}
|
|---|
| 140 | </Box>
|
|---|
| 141 | <Box sx={{flexGrow: 0}}>
|
|---|
| 142 | {!loggedUser ? <div className={"d-flex"}>
|
|---|
| 143 | <Link to="/login" className={"nav-link h6 m-2"}>LOGIN</Link>
|
|---|
| 144 | <Link to="/register" className={"nav-link h6 m-2"}>REGISTER</Link>
|
|---|
| 145 | </div> :
|
|---|
| 146 | <>
|
|---|
| 147 | {[UserRole.Admin, UserRole.Menager].includes(loggedUserRole.role) &&
|
|---|
| 148 | < Button onClick={() => navigate(loggedUserRole.activeOwnershipId
|
|---|
| 149 | ? `/restorants/${loggedUserRole.activeOwnershipId}`
|
|---|
| 150 | : "restorants/create")}
|
|---|
| 151 | sx={{my: 2, color: 'white'}}>
|
|---|
| 152 | {loggedUserRole.activeOwnershipId ? "Manage" : "Create"} Restaurant
|
|---|
| 153 | </Button>}
|
|---|
| 154 | <IconButton onClick={handleOpenUserMenu} sx={{p: 0}}>
|
|---|
| 155 | <FaceIcon style={{color: 'white'}}/>
|
|---|
| 156 | </IconButton>
|
|---|
| 157 | <Menu
|
|---|
| 158 | sx={{mt: '45px'}}
|
|---|
| 159 | id="menu-appbar"
|
|---|
| 160 | anchorEl={anchorElUser}
|
|---|
| 161 | anchorOrigin={{
|
|---|
| 162 | vertical: 'top',
|
|---|
| 163 | horizontal: 'right',
|
|---|
| 164 | }}
|
|---|
| 165 | keepMounted
|
|---|
| 166 | transformOrigin={{
|
|---|
| 167 | vertical: 'top',
|
|---|
| 168 | horizontal: 'right',
|
|---|
| 169 | }}
|
|---|
| 170 | open={Boolean(anchorElUser)}
|
|---|
| 171 | onClose={handleCloseUserMenu}
|
|---|
| 172 | >
|
|---|
| 173 | <MenuItem onClick={() => navigate(`/users/${loggedUser}`)}>
|
|---|
| 174 | <Typography textAlign="center">Profile</Typography>
|
|---|
| 175 | </MenuItem>
|
|---|
| 176 | {loggedUserRole.role === UserRole.Potrosuvac &&
|
|---|
| 177 | <MenuItem
|
|---|
| 178 | onClick={() => navigate(`/users/${loggedUserRole.roleId}/orders`, {state: {loggedUserId: loggedUserRole.roleId}})}>
|
|---|
| 179 | < Typography textAlign="center"> My Orders</Typography>
|
|---|
| 180 | </MenuItem>}
|
|---|
| 181 | {loggedUserRole.role === UserRole.Potrosuvac && <MenuItem
|
|---|
| 182 | onClick={() => navigate(`/users/${loggedUserRole.roleId}/transactions-history`, {state: {loggedUserId: loggedUserRole.roleId}})}>
|
|---|
| 183 | < Typography textAlign="center"> Transaction History</Typography>
|
|---|
| 184 | </MenuItem>
|
|---|
| 185 | }
|
|---|
| 186 | <MenuItem onClick={handleLogout}>
|
|---|
| 187 | <Typography textAlign="center">Logout</Typography>
|
|---|
| 188 | </MenuItem>
|
|---|
| 189 | </Menu>
|
|---|
| 190 | </>
|
|---|
| 191 | }
|
|---|
| 192 | </Box>
|
|---|
| 193 | </Toolbar>
|
|---|
| 194 | </Container>
|
|---|
| 195 | </AppBar>
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | export default Header; |
|---|