Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
707 stevensc 1
import React from 'react'
5 stevensc 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'
707 stevensc 9
import useFetch from '../../hooks/useFetch'
5 stevensc 10
 
11
const StyledContainer = styled(WidgetLayout)`
12
  padding: 1rem;
13
  ul {
14
    display: flex;
15
    flex-direction: column;
16
    gap: 0.5rem;
709 stevensc 17
    margin-top: 0.5rem;
5 stevensc 18
  }
19
`
20
 
709 stevensc 21
const StyledEventContainer = styled.div`
22
  display: flex;
23
  flex-direction: column;
24
  cursor: pointer;
25
  padding: 0.5rem;
26
  border-radius: ${(props) => props.theme.border.radius};
27
  background-color: ${(props) => props.background || 'transparent'};
711 stevensc 28
  h4 {
712 stevensc 29
    color: ${(props) => props.theme.font.color.title};
711 stevensc 30
  }
710 stevensc 31
  b {
32
    color: ${(props) => props.theme.font.color.title};
712 stevensc 33
    font-weight: 600;
710 stevensc 34
  }
35
  span {
36
    color: ${(props) => props.color || props.theme.font.color.primary};
711 stevensc 37
    font-weight: 400;
710 stevensc 38
  }
709 stevensc 39
`
40
 
5 stevensc 41
const EventsList = () => {
707 stevensc 42
  const { data: eventsAndTasks } = useFetch('/helpers/next-events', [])
5 stevensc 43
  const dispatch = useDispatch()
44
 
709 stevensc 45
  const getAdminAuth = (url) => {
5 stevensc 46
    axios
47
      .get(url)
706 stevensc 48
      .then(({ data: responseData }) => {
49
        const { data, success } = responseData
5 stevensc 50
 
51
        if (!success) {
707 stevensc 52
          throw new Error('Error interno. Por favor, intente más tarde.')
5 stevensc 53
        }
54
 
55
        window.open(data, '_blank')
56
      })
655 stevensc 57
      .catch((err) => {
707 stevensc 58
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 59
      })
60
  }
61
 
735 stevensc 62
  const formatDate = (date) => {
736 stevensc 63
    const dateObj = new Date(date)
64
 
65
    if (isNaN(dateObj)) {
66
      return date
67
    }
68
 
739 stevensc 69
    return new Intl.DateTimeFormat('en-US', {
70
      dateStyle: 'short',
71
      timeStyle: 'medium',
72
      hour12: false
73
    }).format(dateObj)
735 stevensc 74
  }
75
 
5 stevensc 76
  return (
77
    <StyledContainer>
78
      <h2>Eventos y Tareas</h2>
79
 
80
      <ul>
81
        {eventsAndTasks.length ? (
707 stevensc 82
          eventsAndTasks.map(
710 stevensc 83
            ({
84
              id,
85
              url,
86
              title,
87
              backgroundColor,
88
              start,
89
              end,
90
              textColor,
91
              source
92
            }) => {
707 stevensc 93
              return (
94
                <li key={id}>
709 stevensc 95
                  <StyledEventContainer
96
                    color={textColor}
97
                    background={backgroundColor}
710 stevensc 98
                    onClick={() =>
99
                      source === 'external'
100
                        ? window.open(url, '_blank')
101
                        : getAdminAuth(url)
102
                    }
5 stevensc 103
                  >
104
                    <h4>
707 stevensc 105
                      <b>Evento: </b>
106
                      {title}
5 stevensc 107
                    </h4>
108
                    <span>
109
                      <b>Inicio: </b>
735 stevensc 110
                      {formatDate(start)}
5 stevensc 111
                    </span>
707 stevensc 112
                    {end && (
5 stevensc 113
                      <span>
114
                        <b>Fin: </b>
735 stevensc 115
                        {formatDate(end)}
5 stevensc 116
                      </span>
117
                    )}
709 stevensc 118
                  </StyledEventContainer>
707 stevensc 119
                </li>
120
              )
121
            }
122
          )
5 stevensc 123
        ) : (
655 stevensc 124
          <EmptySection message='No hay eventos disponibles' />
5 stevensc 125
        )}
126
      </ul>
127
    </StyledContainer>
128
  )
129
}
130
 
131
export default EventsList