Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2854 stevensc 1
import React, { useState } from 'react'
2838 stevensc 2
import { useNavigate } from 'react-router-dom'
3
import { useDispatch, useSelector } from 'react-redux'
4
 
5
import { axios } from '@app/utils'
6
import { addNotification } from '@app/redux/notification/notification.actions'
7
import { showReportModal } from '@app/redux/report/report.actions'
8
import { fetchFeeds } from '@app/redux/feed/feed.actions'
9
 
10
import Widget from '@components/UI/Widget'
11
import Options from '@components/UI/Option'
12
import ConfirmModal from '@components/modals/ConfirmModal'
13
 
14
export default function FeedHeader({ id }) {
15
  const [showConfirmModal, setShowConfirmModal] = useState(false)
16
  const navigate = useNavigate()
17
  const dispatch = useDispatch()
18
 
19
  const { timelineUrl, currentPage } = useSelector(({ feed }) => feed)
20
 
21
  const {
22
    feed_delete_url: deleteUrl,
23
    feed_abuse_report_url: reportUrl,
24
    owner_image: image,
25
    owner_name: name,
26
    owner_time_elapse: timeElapse,
27
    owner_url: profileUrl
28
  } = useSelector(({ feed }) => feed.feeds.byId[id])
29
 
30
  const deleteFeed = async () => {
31
    try {
32
      const response = await axios.post(deleteUrl)
33
      const { data, success } = response.data
34
 
35
      if (!success) {
36
        throw new Error('Ha ocurrido un error al eliminar la publicación')
37
      }
38
 
39
      dispatch(deleteFeed(id))
40
      dispatch(addNotification({ style: 'success', msg: data }))
41
      toggleConfirm()
42
    } catch (error) {
43
      dispatch(addNotification({ style: 'danger', msg: error.message }))
44
    }
45
  }
46
 
47
  const reportFeed = () =>
48
    dispatch(
49
      showReportModal({
50
        type: 'publicación',
51
        reportUrl,
52
        onComplete: () => dispatch(fetchFeeds(timelineUrl, currentPage))
53
      })
54
    )
55
 
56
  const toggleConfirm = () => setShowConfirmModal(!showConfirmModal)
57
 
58
  return (
59
    <>
60
      <Widget.Header
61
        avatar={image}
62
        title={name}
63
        onClick={() => navigate(profileUrl)}
64
        subheader={timeElapse}
2854 stevensc 65
        renderAction={() => (
66
          <Options>
67
            {reportUrl ? (
68
              <Options.Item onClick={reportFeed}>Reportar</Options.Item>
69
            ) : null}
70
 
71
            {deleteUrl ? (
72
              <Options.Item onClick={toggleConfirm}>Borrar</Options.Item>
73
            ) : null}
74
          </Options>
75
        )}
2838 stevensc 76
      />
77
      <ConfirmModal
78
        show={showConfirmModal}
79
        onClose={toggleConfirm}
80
        onAccept={deleteFeed}
81
      />
82
    </>
83
  )
84
}