Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

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