Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 891 | Rev 2278 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React, { useEffect, useState } from 'react'
import { Link } from 'react-router-dom'
import { axios } from '../../../utils'
import { useSelector } from 'react-redux'
import { addNotification } from '../../../redux/notification/notification.actions'
import styled from 'styled-components'

import WidgetWrapper from '../WidgetLayout'
import EmptySection from '../../UI/EmptySection'

const StyledNewsList = styled.ul`
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 380px;
  padding-bottom: 10px;
  overflow: auto;
`

const StyledNewItem = styled.div`
  display: flex;
  gap: 0.5rem;
  > a:first-child {
    flex-shrink: 0;
  }
  img {
    width: 100px;
    aspect-ratio: 100/60;
    object-fit: cover;
  }
  .post-info {
    display: flex;
    flex-direction: column;
    flex: 1 1 50%;
    max-width: calc(100% - (100px + 0.5rem));
    h4 {
      font-weight: 600;
      font-size: 1.2rem;
      color: var(--subtitle-color);
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span {
      color: var(--font-color);
      font-size: 0.8rem;
    }
  }
`

export default function HomeNews({ currentPost }) {
  const labels = useSelector(({ intl }) => intl.labels)
  const [news, setNews] = useState([])

  const loadNews = () => {
    axios
      .get('/helpers/posts')
      .then(({ data: responseData }) => {
        const { data, success } = responseData

        if (!success) {
          throw new Error(data)
        }

        setNews(data)
      })
      .catch(() => {
        addNotification({
          style: 'danger',
          msg: 'Disculpe, ha ocurrido un error buscando novedades'
        })
      })
  }

  useEffect(() => {
    loadNews()
  }, [])

  return (
    <WidgetWrapper>
      <WidgetWrapper.Header title={labels.posts} />

      <WidgetWrapper.Body className='py-0'>
        <StyledNewsList>
          {!news.length ? (
            <EmptySection message={labels.not_available_posts} />
          ) : (
            news.map(({ link, title, image, date }) => {
              if (link.includes(currentPost)) return null

              return (
                <StyledNewItem key={title}>
                  <Link to={link}>
                    <img src={image} alt={`${title} image`} />
                  </Link>

                  <div className='post-info'>
                    <Link to={link}>
                      <h4 title={title}>{title}</h4>
                    </Link>
                    <span>{date}</span>
                  </div>
                </StyledNewItem>
              )
            })
          )}
        </StyledNewsList>
      </WidgetWrapper.Body>
    </WidgetWrapper>
  )
}