Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2854 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2838 stevensc 1
import React, { useMemo, useState } from 'react'
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 labels = useSelector(({ intl }) => intl.labels)
17
  const navigate = useNavigate()
18
  const dispatch = useDispatch()
19
 
20
  const { timelineUrl, currentPage } = useSelector(({ feed }) => feed)
21
 
22
  const {
23
    feed_delete_url: deleteUrl,
24
    feed_abuse_report_url: reportUrl,
25
    owner_image: image,
26
    owner_name: name,
27
    owner_time_elapse: timeElapse,
28
    owner_url: profileUrl
29
  } = useSelector(({ feed }) => feed.feeds.byId[id])
30
 
31
  const deleteFeed = async () => {
32
    try {
33
      const response = await axios.post(deleteUrl)
34
      const { data, success } = response.data
35
 
36
      if (!success) {
37
        throw new Error('Ha ocurrido un error al eliminar la publicación')
38
      }
39
 
40
      dispatch(deleteFeed(id))
41
      dispatch(addNotification({ style: 'success', msg: data }))
42
      toggleConfirm()
43
    } catch (error) {
44
      dispatch(addNotification({ style: 'danger', msg: error.message }))
45
    }
46
  }
47
 
48
  const reportFeed = () =>
49
    dispatch(
50
      showReportModal({
51
        type: 'publicación',
52
        reportUrl,
53
        onComplete: () => dispatch(fetchFeeds(timelineUrl, currentPage))
54
      })
55
    )
56
 
57
  const toggleConfirm = () => setShowConfirmModal(!showConfirmModal)
58
 
59
  const options = useMemo(() => {
60
    const defaultOptions = []
61
 
62
    if (deleteUrl) {
63
      defaultOptions.push({ action: toggleConfirm, label: labels.delete })
64
    }
65
 
66
    if (reportUrl) {
67
      defaultOptions.push({
68
        action: () => reportFeed(reportUrl),
69
        label: 'Reportar'
70
      })
71
    }
72
 
73
    return defaultOptions
74
  }, [deleteUrl, reportUrl])
75
 
76
  return (
77
    <>
78
      <Widget.Header
79
        avatar={image}
80
        title={name}
81
        onClick={() => navigate(profileUrl)}
82
        subheader={timeElapse}
83
        renderAction={() =>
84
          options.length ? <Options options={options} right='.5rem' /> : null
85
        }
86
      />
87
      <ConfirmModal
88
        show={showConfirmModal}
89
        onClose={toggleConfirm}
90
        onAccept={deleteFeed}
91
      />
92
    </>
93
  )
94
}