Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
1657 stevensc 1
import React, { useMemo, useState } from 'react'
1650 stevensc 2
import { Link } from 'react-router-dom'
1657 stevensc 3
import { useDispatch, useSelector } from 'react-redux'
1650 stevensc 4
 
1657 stevensc 5
import { axios } from '@app/utils'
6
import { addNotification } from '@app/redux/notification/notification.actions'
7
import { removeComment } from '@app/redux/feed/feed.actions'
8
import { showReportModal } from '@app/redux/report/report.actions'
9
 
1650 stevensc 10
import { StyledCommentList, StyledCommentTemplate } from './comments-ui'
11
import Options from '@app/components/UI/Option'
12
import ConfirmModal from '@app/components/modals/ConfirmModal'
13
 
1657 stevensc 14
export default function CommentsList({ comments }) {
1650 stevensc 15
  return (
16
    <>
17
      <StyledCommentList>
18
        {comments.map((comment) => {
19
          return (
20
            <li key={comment.unique}>
1657 stevensc 21
              <CommentsList.Item comment={comment} />
1650 stevensc 22
            </li>
23
          )
24
        })}
25
      </StyledCommentList>
26
    </>
27
  )
28
}
29
 
1657 stevensc 30
function Comment({ comment }) {
1650 stevensc 31
  const {
32
    unique,
33
    user_url,
34
    user_name,
35
    time_elapsed,
36
    comment: content,
1657 stevensc 37
    link_delete,
38
    link_abuse_report,
39
    feedId
1650 stevensc 40
  } = comment
1657 stevensc 41
  const [showConfirmModal, setShowConfirmModal] = useState(false)
42
  const labels = useSelector(({ intl }) => intl.labels)
43
  const dispatch = useDispatch()
1650 stevensc 44
 
1657 stevensc 45
  const actions = useMemo(() => {
46
    const actions = []
47
    if (link_delete) actions.push({ label: 'Borrar', action: toggleModal })
48
 
49
    if (link_abuse_report)
50
      actions.push({ label: 'Reportar', action: reportComment })
51
 
52
    return actions
53
  }, [link_delete])
54
 
55
  const toggleModal = () => setShowConfirmModal(!showConfirmModal)
56
 
57
  const reportComment = () =>
58
    dispatch(
59
      showReportModal({
60
        reportUrl: link_abuse_report,
61
        type: 'Comentario',
62
        onComplete: () => dispatch(removeComment({ commentId: unique, feedId }))
63
      })
64
    )
65
 
66
  const deleteComment = ({ id, deleteUrl }) => {
67
    axios
68
      .post(deleteUrl)
69
      .then((response) => {
70
        const { success, data } = response.data
71
 
72
        if (!success) {
73
          const errorMessage =
74
            typeof data === 'string'
75
              ? data
76
              : 'Error interno. Intente más tarde.'
77
          throw new Error(errorMessage)
78
        }
79
 
80
        dispatch(addNotification({ style: 'success', msg: data }))
81
        dispatch(removeComment({ feedId, commentId: id }))
82
      })
83
      .catch((error) => {
84
        dispatch(addNotification({ style: 'danger', msg: error.message }))
85
      })
1650 stevensc 86
  }
87
 
88
  return (
89
    <>
90
      <StyledCommentTemplate>
91
        <div className='content'>
92
          <div className='info'>
93
            <Link to={user_url}>
94
              <h3>{user_name}</h3>
95
            </Link>
96
            <span>{time_elapsed}</span>
97
          </div>
1657 stevensc 98
 
99
          <Options options={actions} right='0.5rem' top='1.5rem' />
100
 
1650 stevensc 101
          <p>{content}</p>
102
        </div>
103
      </StyledCommentTemplate>
1657 stevensc 104
 
1650 stevensc 105
      <ConfirmModal
106
        show={showConfirmModal}
107
        onClose={toggleModal}
1657 stevensc 108
        onAccept={() => deleteComment({ id: unique, deleteUrl: link_delete })}
1650 stevensc 109
        acceptLabel={labels.accept}
110
      />
111
    </>
112
  )
113
}
114
 
115
CommentsList.Item = Comment