Projekt

Obecné

Profil

Stáhnout (2.91 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { styled, useTheme } from '@mui/material/styles'
2
import { FunctionComponent, useEffect, useState } from 'react'
3
import ChevronLeftIcon from '@mui/icons-material/ChevronLeft'
4
import ChevronRightIcon from '@mui/icons-material/ChevronRight'
5
import {
6
    Divider,
7
    Drawer,
8
    IconButton,
9
    Link,
10
    List,
11
    ListItem,
12
    ListItemIcon,
13
    ListItemText,
14
} from '@mui/material'
15
import { useSelector } from 'react-redux'
16
import { RootState } from '../redux/store'
17
import getNavigationItems from './navigationMenuItems'
18
import { Link as RouterLink } from 'react-router-dom'
19

    
20
export interface NavigationMenuProps {
21
    open: boolean
22
    drawerWidth: number
23
    setOpen: (open: boolean) => void
24
}
25

    
26
const DrawerHeader = styled('div')(({ theme }) => ({
27
    display: 'flex',
28
    alignItems: 'center',
29
    padding: theme.spacing(0, 1),
30
    // necessary for content to be below app bar
31
    ...theme.mixins.toolbar,
32
    justifyContent: 'flex-end',
33
}))
34

    
35
const NavigationMenu: FunctionComponent<NavigationMenuProps> = ({
36
    open,
37
    drawerWidth,
38
    setOpen,
39
}) => {
40
    const theme = useTheme()
41

    
42
    const onCloseDrawer = () => {
43
        setOpen(false)
44
    }
45

    
46
    // We need user roles to determine which pages will be accessible from the navigation menu
47
    const userRoles = useSelector((state: RootState) => state.user.roles)
48

    
49
    // List of all menu items
50
    const [menuItems, setMenuItems] = useState(getNavigationItems(userRoles))
51

    
52
    // Use effect to update menu items should the user roles change
53
    useEffect(() => {
54
        setMenuItems(getNavigationItems(userRoles))
55
    }, [userRoles])
56

    
57
    return (
58
        <Drawer
59
            sx={{
60
                width: drawerWidth,
61
                flexShrink: 0,
62
                '& .MuiDrawer-paper': {
63
                    width: drawerWidth,
64
                    boxSizing: 'border-box',
65
                },
66
            }}
67
            variant="persistent"
68
            anchor="left"
69
            open={open}
70
        >
71
            <DrawerHeader>
72
                <IconButton onClick={onCloseDrawer}>
73
                    {theme.direction === 'ltr' ? (
74
                        <ChevronLeftIcon />
75
                    ) : (
76
                        <ChevronRightIcon />
77
                    )}
78
                </IconButton>
79
            </DrawerHeader>
80
            <Divider />
81

    
82
            <List>
83
                {menuItems.map((item, idx) => (
84
                    <Link
85
                        underline="none"
86
                        color="inherit"
87
                        component={RouterLink}
88
                        to={item.path}
89
                        key={idx}
90
                    >
91
                        <ListItem button>
92
                            <ListItemIcon><item.icon /></ListItemIcon>
93
                            <ListItemText>{item.name}</ListItemText>
94
                        </ListItem>
95
                    </Link>
96
                ))}
97
            </List>
98
        </Drawer>
99
    )
100
}
101

    
102
export default NavigationMenu
(2-2/3)