Projekt

Obecné

Profil

Stáhnout (2.92 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

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

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

    
103
export default NavigationMenu
(2-2/3)