Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3658 | | 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 { axios } from '../../utils';
3
import { useDispatch } from 'react-redux';
4
import { styled, Typography } from '@mui/material';
5
import { addNotification } from '../../redux/notification/notification.actions';
6
 
7
import { useFetch } from '@hooks';
8
 
9
import WidgetLayout from '../widgets/WidgetLayout';
10
import EmptySection from '../UI/EmptySection';
11
 
12
const StyledContainer = styled(WidgetLayout)`
13
  padding: 1rem;
14
  ul {
15
    display: flex;
16
    flex-direction: column;
17
    gap: 0.5rem;
18
    margin-top: 0.5rem;
19
  }
20
`;
21
 
22
const StyledEventContainer = styled('div')(({ theme }) => ({
23
  display: 'flex',
24
  flexDirection: 'column',
25
  cursor: 'pointer',
26
  padding: '0.5rem',
27
  borderRadius: theme.shape.borderRadius
28
}));
29
 
30
const EventsList = () => {
31
  const { data: eventsAndTasks } = useFetch('/helpers/next-events', []);
32
  const dispatch = useDispatch();
33
 
34
  const getAdminAuth = (url) => {
35
    axios
36
      .get(url)
37
      .then((response) => {
38
        const { data, success } = response.data;
39
 
40
        if (!success) {
41
          throw new Error('Error interno. Por favor, intente más tarde.');
42
        }
43
 
44
        window.open(data, '_blank');
45
      })
46
      .catch((err) => {
47
        dispatch(addNotification({ style: 'danger', msg: err.message }));
48
      });
49
  };
50
 
51
  const formatDate = (date, allDay) => {
52
    const dateObj = new Date(date);
53
 
54
    if (isNaN(dateObj)) {
55
      return date;
56
    }
57
 
58
    return new Intl.DateTimeFormat('es', {
59
      dateStyle: 'medium',
60
      timeStyle: allDay ? undefined : 'short',
61
      timeZone: 'UTC',
62
      hour12: allDay ? undefined : true
63
    }).format(dateObj);
64
  };
65
 
66
  return (
67
    <StyledContainer>
68
      <h2>Eventos y Tareas</h2>
69
 
70
      <ul>
71
        {eventsAndTasks.length ? (
72
          eventsAndTasks.map(
73
            ({ id, allDay, url, title, backgroundColor, start, end, textColor, source }) => {
74
              return (
75
                <li key={id}>
76
                  <StyledEventContainer
77
                    sx={{ backgroundColor }}
78
                    onClick={() =>
79
                      source === 'external' ? window.open(url, '_blank') : getAdminAuth(url)
80
                    }
81
                  >
82
                    <Typography variant='h4' sx={{ color: textColor }}>
83
                      <b>Evento: </b>
84
                      {title}
85
                    </Typography>
86
                    <Typography variant='h4' sx={{ color: textColor }}>
87
                      <b>Inicio: </b>
88
                      {formatDate(start, allDay)}
89
                    </Typography>
90
                    {end && (
91
                      <Typography variant='h4' sx={{ color: textColor }}>
92
                        <b>Fin: </b>
93
                        {formatDate(end, allDay)}
94
                      </Typography>
95
                    )}
96
                    <Typography variant='h4' sx={{ color: textColor }}>
97
                      <b>Tipo: </b>
98
                      {source === 'external' ? 'Externo' : 'Interno'}
99
                    </Typography>
100
                  </StyledEventContainer>
101
                </li>
102
              );
103
            }
104
          )
105
        ) : (
106
          <EmptySection message='No hay eventos disponibles' />
107
        )}
108
      </ul>
109
    </StyledContainer>
110
  );
111
};
112
 
113
export default EventsList;