Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3714 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React from 'react';
2
import { useNavigate } from 'react-router-dom';
3
import { useDispatch } from 'react-redux';
4
import {
5
  Box,
6
  Drawer as MuiDrawer,
7
  List,
8
  ListItem,
9
  ListItemButton,
10
  ListItemText,
11
  Accordion,
12
  AccordionSummary,
13
  AccordionDetails,
14
  Typography
15
} from '@mui/material';
16
import ExpandMore from '@mui/icons-material/ExpandMore';
17
 
18
import { axios } from '@app/utils';
19
import { addNotification } from '@app/redux/notification/notification.actions';
20
 
21
export const Drawer = ({ show = false, items = [], onClose = () => {} }) => {
22
  return (
23
    <MuiDrawer anchor='right' open={show} onClose={onClose}>
24
      <Box role='presentation' onClick={onClose}>
25
        <List>
26
          {items.map((item) => (
27
            <ListItem key={item.href}>
28
              <RenderMenuItem item={item} onClose={onClose} />
29
            </ListItem>
30
          ))}
31
        </List>
32
      </Box>
33
    </MuiDrawer>
34
  );
35
};
36
 
37
const RenderMenuItem = ({ item, onClose }) => {
38
  if (item.childs && item.childs.length > 0) {
39
    return <AccordionItem item={item} onClose={onClose} />;
40
  }
41
 
42
  return <MenuDrawerItem item={item} onClose={onClose} />;
43
};
44
 
45
const MenuDrawerItem = ({ item: { label, href, ajax }, onClose }) => {
46
  const navigate = useNavigate();
47
  const dispatch = useDispatch();
48
 
49
  const asyncNavigate = async (url) => {
50
    try {
51
      const { data } = await axios.get(url);
52
      if (!data.success) throw new Error('Ha ocurrido un error. Por favor, intente nuevamente.');
53
      navigate(data.data);
54
    } catch (error) {
55
      dispatch(addNotification({ style: 'danger', msg: error.message }));
56
    }
57
  };
58
 
59
  const handleClick = (e) => {
60
    e.stopPropagation();
61
    onClose();
62
 
63
    if (ajax) {
64
      e.preventDefault();
65
      asyncNavigate(href);
66
    } else {
67
      navigate(href);
68
    }
69
  };
70
 
71
  return (
72
    <ListItemButton onClick={handleClick}>
73
      <ListItemText primary={label} />
74
    </ListItemButton>
75
  );
76
};
77
 
78
const AccordionItem = ({ item: { label, childs }, onClose }) => {
79
  const handleAccordionClick = (e) => {
80
    e.stopPropagation();
81
  };
82
 
83
  return (
84
    <Accordion sx={{ width: '100%' }}>
85
      <AccordionSummary expandIcon={<ExpandMore />} onClick={handleAccordionClick}>
86
        <Typography>{label}</Typography>
87
      </AccordionSummary>
88
 
89
      <AccordionDetails>
90
        <List>
91
          {childs.map((child) => (
92
            <ListItem key={child.href}>
93
              <RenderMenuItem item={child} onClose={onClose} />
94
            </ListItem>
95
          ))}
96
        </List>
97
      </AccordionDetails>
98
    </Accordion>
99
  );
100
};
101
 
102
export default Drawer;