Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7304 | | 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) => {
7305 stevensc 33
    axios
34
      .get(url)
35
      .then((response) => {
36
        const { data, success } = response.data
7304 stevensc 37
 
7305 stevensc 38
        if (!success) {
39
          dispatch(
40
            addNotification({
41
              style: 'danger',
42
              msg: 'Error interno. Por favor, intente más tarde.',
43
            })
44
          )
45
          return
46
        }
47
 
48
        window.open(data, '_blank')
49
      })
50
      .catch((error) => {
7304 stevensc 51
        dispatch(
7305 stevensc 52
          addNotification({ style: 'danger', message: 'Ha ocurrido un error' })
7304 stevensc 53
        )
7305 stevensc 54
        throw new Error(error)
55
      })
7304 stevensc 56
  }
57
 
7279 stevensc 58
  const getEvents = () => {
59
    axios
60
      .get('/helpers/next-events')
61
      .then((response) => {
62
        const { success, data } = response.data
63
 
64
        if (!success) {
65
          dispatch(addNotification({ style: 'danger', message: data }))
66
          return
67
        }
68
 
69
        setEventsAndTasks(data)
70
      })
71
      .catch((error) => {
72
        dispatch(
73
          addNotification({ style: 'danger', message: 'Ha ocurrido un error' })
74
        )
75
        throw new Error(error)
76
      })
77
  }
78
 
79
  useEffect(() => {
80
    getEvents()
81
  }, [])
82
 
83
  return (
84
    <StyledContainer>
85
      <h2>Eventos y Tareas</h2>
86
 
87
      <ul>
88
        {eventsAndTasks.length ? (
89
          eventsAndTasks.map((event) => {
90
            return (
91
              <li key={event.id}>
7304 stevensc 92
                <a
93
                  href={event.url}
94
                  onClick={(e) => {
95
                    e.preventDefault()
96
                    getBackendVarUrl(event.url)
97
                  }}
98
                >
7279 stevensc 99
                  <div
100
                    className="calendar-event"
101
                    style={{
102
                      color: event.textColor,
103
                      background: event.backgroundColor,
104
                    }}
105
                  >
106
                    <h4>
107
                      <b>Evento: </b> {event.title}
108
                    </h4>
109
                    <span>
110
                      <b>Inicio: </b>
7288 stevensc 111
                      {event.start}
7279 stevensc 112
                    </span>
113
                    {event.end && (
114
                      <span>
115
                        <b>Fin: </b>
7288 stevensc 116
                        {event.end}
7279 stevensc 117
                      </span>
118
                    )}
119
                  </div>
120
                </a>
121
              </li>
122
            )
123
          })
124
        ) : (
125
          <EmptySection message="No hay eventos disponibles" />
126
        )}
127
      </ul>
128
    </StyledContainer>
129
  )
130
}
131
 
132
export default EventsList