Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2186 stevensc 1
import React, { useEffect, useMemo, useState } from 'react'
1650 stevensc 2
import { Link } from 'react-router-dom'
2185 stevensc 3
import { useDispatch } 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 { showReportModal } from '@app/redux/report/report.actions'
8
 
1661 stevensc 9
import { CommentsContainer, CommentTemplate } from './comments-ui'
1650 stevensc 10
import Options from '@app/components/UI/Option'
11
import ConfirmModal from '@app/components/modals/ConfirmModal'
12
 
2185 stevensc 13
export default function CommentsList({ comments: defaultComments = [] }) {
14
  const [comments, setComments] = useState(defaultComments)
15
  const [currentComment, setCurrentComment] = useState(null)
16
  const [showConfirm, setShowConfirm] = useState(false)
1657 stevensc 17
  const dispatch = useDispatch()
1650 stevensc 18
 
2185 stevensc 19
  const handleDelete = (comment) => {
20
    setCurrentComment(comment)
21
    setShowConfirm(true)
22
  }
1657 stevensc 23
 
2185 stevensc 24
  const removeComment = () => {
25
    setComments((prev) =>
26
      prev.filter((comment) => comment.unique !== currentComment.unique)
27
    )
28
  }
29
 
30
  const onCloseConfirm = () => {
31
    setShowConfirm(false)
32
    setCurrentComment(null)
33
  }
34
 
35
  const reportComment = (reportUrl) =>
1657 stevensc 36
    dispatch(
37
      showReportModal({
2185 stevensc 38
        reportUrl,
1657 stevensc 39
        type: 'Comentario',
2185 stevensc 40
        onComplete: () => removeComment()
1657 stevensc 41
      })
42
    )
43
 
2185 stevensc 44
  const deleteComment = () => {
1657 stevensc 45
    axios
2185 stevensc 46
      .post(currentComment.link_delete)
1657 stevensc 47
      .then((response) => {
48
        const { success, data } = response.data
49
 
50
        if (!success) {
2185 stevensc 51
          throw new Error(
52
            'Error al eliminar un comentario, por favor intente más tarde'
53
          )
1657 stevensc 54
        }
55
 
56
        dispatch(addNotification({ style: 'success', msg: data }))
2185 stevensc 57
        removeComment()
58
        onCloseConfirm()
1657 stevensc 59
      })
60
      .catch((error) => {
61
        dispatch(addNotification({ style: 'danger', msg: error.message }))
62
      })
1650 stevensc 63
  }
64
 
2186 stevensc 65
  useEffect(() => setComments(defaultComments), [defaultComments])
66
 
2185 stevensc 67
  return (
68
    <>
69
      <CommentsContainer>
70
        {comments.map((comment) => (
71
          <Comment
72
            key={comment.unique}
73
            comment={comment}
74
            onDelete={handleDelete}
75
            onReport={reportComment}
76
          />
77
        ))}
78
      </CommentsContainer>
79
      <ConfirmModal
80
        show={showConfirm}
81
        onClose={onCloseConfirm}
82
        onAccept={deleteComment}
83
      />
84
    </>
85
  )
86
}
87
 
88
function Comment({ comment, onReport, onDelete }) {
89
  const {
90
    user_url,
91
    user_name,
92
    time_elapsed,
93
    comment: content,
94
    link_delete,
95
    link_abuse_report
96
  } = comment
97
 
1660 stevensc 98
  const actions = useMemo(() => {
99
    const options = []
100
 
2185 stevensc 101
    if (link_delete)
102
      options.push({ label: 'Borrar', action: () => onDelete(comment) })
1660 stevensc 103
 
2185 stevensc 104
    if (link_abuse_report)
105
      options.push({
106
        label: 'Reportar',
107
        action: () => onReport(link_abuse_report)
108
      })
109
 
1660 stevensc 110
    return options
111
  }, [link_delete, link_abuse_report])
112
 
1650 stevensc 113
  return (
2185 stevensc 114
    <CommentTemplate>
115
      <div className='content'>
116
        <div className='info'>
117
          <Link to={user_url}>
118
            <h3>{user_name}</h3>
119
          </Link>
120
          <span>{time_elapsed}</span>
121
        </div>
1657 stevensc 122
 
2185 stevensc 123
        <Options options={actions} />
1657 stevensc 124
 
2185 stevensc 125
        <p>{content}</p>
126
      </div>
127
    </CommentTemplate>
1650 stevensc 128
  )
129
}