Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
3503 stevensc 1
/* eslint-disable react/prop-types */
5104 stevensc 2
import React, { useEffect, useState } from 'react'
3
import { addNotification } from '../../../redux/notification/notification.actions'
4
import EmptySection from '../../../shared/empty-section/EmptySection'
5
import { axios } from '../../../utils'
5438 stevensc 6
import { useSelector } from 'react-redux'
1386 steven 7
 
5944 stevensc 8
export default function HomeNews({ currentPost }) {
5104 stevensc 9
  const [news, setNews] = useState([])
5944 stevensc 10
  const labels = useSelector((state) => state.labels)
5438 stevensc 11
 
5104 stevensc 12
  const loadNews = () => {
5944 stevensc 13
    axios
14
      .get('/helpers/posts')
5104 stevensc 15
      .then(({ data: response }) => {
16
        if (!response.success) {
17
          addNotification({
18
            style: 'danger',
5944 stevensc 19
            msg: response.data,
5104 stevensc 20
          })
21
        }
22
        setNews(response.data)
23
      })
24
      .catch(() => {
25
        addNotification({
26
          style: 'danger',
5944 stevensc 27
          msg: 'Disculpe, ha ocurrido un error buscando novedades',
5104 stevensc 28
        })
29
      })
30
  }
4098 stevensc 31
 
5104 stevensc 32
  useEffect(() => {
33
    loadNews()
34
  }, [])
4088 stevensc 35
 
5104 stevensc 36
  return (
5944 stevensc 37
    <div className="posts-widget">
5440 stevensc 38
      <h3>{labels.POSTS}</h3>
5377 stevensc 39
      <div className="posts-list">
5944 stevensc 40
        {!news.length ? (
41
          <EmptySection message={labels.NOT_AVAILABLE_POSTS} />
42
        ) : (
43
          news.map((element) => {
5376 stevensc 44
            if (element.link.includes(currentPost)) {
45
              return null
46
            }
4098 stevensc 47
 
5376 stevensc 48
            return (
5944 stevensc 49
              <div key={element.title} className="post-item">
50
                <a href={element.link} target="secondary">
5949 stevensc 51
                  <img src={element.image} alt={`${element.title} image`} />
5376 stevensc 52
                </a>
5949 stevensc 53
                <div className="post-info">
54
                  <a href={element.link} target="secondary">
55
                    <h4 title={element.title}>{element.title}</h4>
56
                  </a>
57
                  <span>{element.date}</span>
58
                </div>
5376 stevensc 59
              </div>
60
            )
5944 stevensc 61
          })
62
        )}
5376 stevensc 63
      </div>
64
    </div>
5104 stevensc 65
  )
1386 steven 66
}