Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7288 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7279 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { axios } from '../../utils'
3
import { useDispatch } from 'react-redux'
4
import { addNotification } from '../../redux/notification/notification.actions'
5
import styled from 'styled-components'
6
 
7
import WidgetLayout from '../widgets/WidgetLayout'
8
import EmptySection from '../UI/EmptySection'
9
 
10
const StyledContainer = styled(WidgetLayout)`
11
  padding: 1rem;
12
  h2 {
13
    font-size: 1.1rem;
14
    font-weight: 600;
15
    color: var(--title-color);
16
  }
7280 stevensc 17
  ul {
18
    display: flex;
19
    flex-direction: column;
7281 stevensc 20
    gap: 0.5rem;
7280 stevensc 21
    margin-top: 1rem;
22
  }
23
  a {
24
    display: block;
25
  }
7279 stevensc 26
`
27
 
28
const EventsList = () => {
29
  const [eventsAndTasks, setEventsAndTasks] = useState([])
30
  const dispatch = useDispatch()
31
 
7304 stevensc 32
  const getBackendVarUrl = (url) => {
33
    axios.get(url).then((response) => {
34
      const { data, success } = response.data
35
 
36
      if (!success) {
37
        dispatch(
38
          addNotification({
39
            style: 'danger',
40
            msg: 'Error interno. Por favor, intente más tarde.',
41
          })
42
        )
43
        return
44
      }
45
 
46
      window.open(data, '_blank')
47
    })
48
  }
49
 
7279 stevensc 50
  const getEvents = () => {
51
    axios
52
      .get('/helpers/next-events')
53
      .then((response) => {
54
        const { success, data } = response.data
55
 
56
        if (!success) {
57
          dispatch(addNotification({ style: 'danger', message: data }))
58
          return
59
        }
60
 
61
        setEventsAndTasks(data)
62
      })
63
      .catch((error) => {
64
        dispatch(
65
          addNotification({ style: 'danger', message: 'Ha ocurrido un error' })
66
        )
67
        throw new Error(error)
68
      })
69
  }
70
 
71
  useEffect(() => {
72
    getEvents()
73
  }, [])
74
 
75
  return (
76
    <StyledContainer>
77
      <h2>Eventos y Tareas</h2>
78
 
79
      <ul>
80
        {eventsAndTasks.length ? (
81
          eventsAndTasks.map((event) => {
82
            return (
83
              <li key={event.id}>
7304 stevensc 84
                <a
85
                  href={event.url}
86
                  onClick={(e) => {
87
                    e.preventDefault()
88
                    getBackendVarUrl(event.url)
89
                  }}
90
                >
7279 stevensc 91
                  <div
92
                    className="calendar-event"
93
                    style={{
94
                      color: event.textColor,
95
                      background: event.backgroundColor,
96
                    }}
97
                  >
98
                    <h4>
99
                      <b>Evento: </b> {event.title}
100
                    </h4>
101
                    <span>
102
                      <b>Inicio: </b>
7288 stevensc 103
                      {event.start}
7279 stevensc 104
                    </span>
105
                    {event.end && (
106
                      <span>
107
                        <b>Fin: </b>
7288 stevensc 108
                        {event.end}
7279 stevensc 109
                      </span>
110
                    )}
111
                  </div>
112
                </a>
113
              </li>
114
            )
115
          })
116
        ) : (
117
          <EmptySection message="No hay eventos disponibles" />
118
        )}
119
      </ul>
120
    </StyledContainer>
121
  )
122
}
123
 
124
export default EventsList