Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3505 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { useDispatch } from 'react-redux'
3
 
4
import { axios } from '@app/utils'
5
import { showReportModal } from '@app/redux/report/report.actions'
6
import { addNotification } from '@app/redux/notification/notification.actions'
7
 
8
import CommentForm from './comment-form'
9
import CommentsList from './comment-list'
10
import ConfirmModal from '@components/modals/ConfirmModal'
11
 
12
export default function Comments({
13
  comments: defaultComments = [],
14
  onAdd = ({ totalComments, comment }) => {},
15
  addUrl = ''
16
}) {
17
  const [comments, setComments] = useState(defaultComments)
18
  const [currentComment, setCurrentComment] = useState(null)
19
  const [showConfirm, setShowConfirm] = useState(false)
20
  const dispatch = useDispatch()
21
 
22
  const handleDelete = (comment) => {
23
    setCurrentComment(comment)
24
    setShowConfirm(true)
25
  }
26
 
27
  const removeComment = () => {
28
    setComments((prev) =>
29
      prev.filter((c) => c.unique !== currentComment.unique)
30
    )
31
  }
32
 
33
  const reportComment = (reportUrl) =>
34
    dispatch(
35
      showReportModal({
36
        reportUrl,
37
        type: 'Comentario',
38
        onComplete: () => removeComment()
39
      })
40
    )
41
 
42
  const deleteComment = async () => {
43
    axios
44
      .post(currentComment.link_delete)
45
      .then((response) => {
46
        const { success, data } = response.data
47
 
48
        if (!success) {
49
          throw new Error(
50
            'Ha ocurrido un error al intentar eliminar el comentario.'
51
          )
52
        }
53
 
54
        removeComment()
55
        setCurrentComment(null)
56
        setShowConfirm(false)
57
        dispatch(addNotification({ style: 'success', msg: data }))
58
      })
59
      .catch((error) => {
60
        dispatch(addNotification({ style: 'danger', msg: error.message }))
61
      })
62
  }
63
 
64
  const addComment = async (comment) => {
65
    try {
66
      const formData = new FormData()
67
      formData.append('comment', comment)
68
 
69
      const response = await axios.post(addUrl, formData)
70
      const {
71
        success,
72
        data: newComment,
73
        total_comments: totalComments
74
      } = response.data
75
 
76
      if (!success) {
77
        throw new Error(
78
          'Ha ocurrido un error al intentar agregar el comentario.'
79
        )
80
      }
81
 
82
      setComments((prev) => [...prev, newComment])
83
      onAdd({ comment: newComment, totalComments })
84
    } catch (error) {
85
      dispatch(addNotification({ style: 'danger', msg: error.message }))
86
    }
87
  }
88
 
89
  useEffect(() => setComments(defaultComments), [defaultComments])
90
 
91
  return (
92
    <>
93
      <CommentForm onSubmit={addComment} />
94
      <CommentsList
95
        comments={comments}
96
        onReport={reportComment}
97
        onDelete={handleDelete}
98
      />
99
      <ConfirmModal
100
        show={showConfirm}
101
        onClose={() => setShowConfirm(false)}
102
        onAccept={deleteComment}
103
      />
104
    </>
105
  )
106
}