Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2844 stevensc 1
import React, { useEffect, useState } from 'react'
2185 stevensc 2
import { useDispatch } from 'react-redux'
1650 stevensc 3
 
1657 stevensc 4
import { axios } from '@app/utils'
5
import { addNotification } from '@app/redux/notification/notification.actions'
6
import { showReportModal } from '@app/redux/report/report.actions'
7
 
2844 stevensc 8
import { CommentsContainer } from './comments-ui'
1650 stevensc 9
import ConfirmModal from '@app/components/modals/ConfirmModal'
2844 stevensc 10
import CommentItem from './comment-item'
1650 stevensc 11
 
2185 stevensc 12
export default function CommentsList({ comments: defaultComments = [] }) {
13
  const [comments, setComments] = useState(defaultComments)
14
  const [currentComment, setCurrentComment] = useState(null)
15
  const [showConfirm, setShowConfirm] = useState(false)
1657 stevensc 16
  const dispatch = useDispatch()
1650 stevensc 17
 
2185 stevensc 18
  const handleDelete = (comment) => {
19
    setCurrentComment(comment)
20
    setShowConfirm(true)
21
  }
1657 stevensc 22
 
2185 stevensc 23
  const removeComment = () => {
24
    setComments((prev) =>
25
      prev.filter((comment) => comment.unique !== currentComment.unique)
26
    )
27
  }
28
 
29
  const onCloseConfirm = () => {
30
    setShowConfirm(false)
31
    setCurrentComment(null)
32
  }
33
 
34
  const reportComment = (reportUrl) =>
1657 stevensc 35
    dispatch(
36
      showReportModal({
2185 stevensc 37
        reportUrl,
1657 stevensc 38
        type: 'Comentario',
2185 stevensc 39
        onComplete: () => removeComment()
1657 stevensc 40
      })
41
    )
42
 
2185 stevensc 43
  const deleteComment = () => {
1657 stevensc 44
    axios
2185 stevensc 45
      .post(currentComment.link_delete)
1657 stevensc 46
      .then((response) => {
47
        const { success, data } = response.data
48
 
49
        if (!success) {
2185 stevensc 50
          throw new Error(
51
            'Error al eliminar un comentario, por favor intente más tarde'
52
          )
1657 stevensc 53
        }
54
 
55
        dispatch(addNotification({ style: 'success', msg: data }))
2185 stevensc 56
        removeComment()
57
        onCloseConfirm()
1657 stevensc 58
      })
59
      .catch((error) => {
60
        dispatch(addNotification({ style: 'danger', msg: error.message }))
61
      })
1650 stevensc 62
  }
63
 
2186 stevensc 64
  useEffect(() => setComments(defaultComments), [defaultComments])
65
 
2185 stevensc 66
  return (
67
    <>
68
      <CommentsContainer>
69
        {comments.map((comment) => (
2844 stevensc 70
          <CommentItem
2185 stevensc 71
            key={comment.unique}
72
            comment={comment}
73
            onDelete={handleDelete}
74
            onReport={reportComment}
75
          />
76
        ))}
77
      </CommentsContainer>
78
      <ConfirmModal
79
        show={showConfirm}
80
        onClose={onCloseConfirm}
81
        onAccept={deleteComment}
82
      />
83
    </>
84
  )
85
}