Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1661 | Rev 2186 | 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'
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
 
2185 stevensc 65
  return (
66
    <>
67
      <CommentsContainer>
68
        {comments.map((comment) => (
69
          <Comment
70
            key={comment.unique}
71
            comment={comment}
72
            onDelete={handleDelete}
73
            onReport={reportComment}
74
          />
75
        ))}
76
      </CommentsContainer>
77
      <ConfirmModal
78
        show={showConfirm}
79
        onClose={onCloseConfirm}
80
        onAccept={deleteComment}
81
      />
82
    </>
83
  )
84
}
85
 
86
function Comment({ comment, onReport, onDelete }) {
87
  const {
88
    user_url,
89
    user_name,
90
    time_elapsed,
91
    comment: content,
92
    link_delete,
93
    link_abuse_report
94
  } = comment
95
 
1660 stevensc 96
  const actions = useMemo(() => {
97
    const options = []
98
 
2185 stevensc 99
    if (link_delete)
100
      options.push({ label: 'Borrar', action: () => onDelete(comment) })
1660 stevensc 101
 
2185 stevensc 102
    if (link_abuse_report)
103
      options.push({
104
        label: 'Reportar',
105
        action: () => onReport(link_abuse_report)
106
      })
107
 
1660 stevensc 108
    return options
109
  }, [link_delete, link_abuse_report])
110
 
1650 stevensc 111
  return (
2185 stevensc 112
    <CommentTemplate>
113
      <div className='content'>
114
        <div className='info'>
115
          <Link to={user_url}>
116
            <h3>{user_name}</h3>
117
          </Link>
118
          <span>{time_elapsed}</span>
119
        </div>
1657 stevensc 120
 
2185 stevensc 121
        <Options options={actions} />
1657 stevensc 122
 
2185 stevensc 123
        <p>{content}</p>
124
      </div>
125
    </CommentTemplate>
1650 stevensc 126
  )
127
}