3719 |
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 = () => {},
|
|
|
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) => prev.filter((c) => c.unique !== currentComment.unique));
|
|
|
29 |
};
|
|
|
30 |
|
|
|
31 |
const reportComment = (reportUrl) =>
|
|
|
32 |
dispatch(
|
|
|
33 |
showReportModal({
|
|
|
34 |
reportUrl,
|
|
|
35 |
type: 'Comentario',
|
|
|
36 |
onComplete: () => removeComment()
|
|
|
37 |
})
|
|
|
38 |
);
|
|
|
39 |
|
|
|
40 |
const deleteComment = async () => {
|
|
|
41 |
axios
|
|
|
42 |
.post(currentComment.link_delete)
|
|
|
43 |
.then((response) => {
|
|
|
44 |
const { success, data } = response.data;
|
|
|
45 |
|
|
|
46 |
if (!success) {
|
|
|
47 |
throw new Error('Ha ocurrido un error al intentar eliminar el comentario.');
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
removeComment();
|
|
|
51 |
setCurrentComment(null);
|
|
|
52 |
setShowConfirm(false);
|
|
|
53 |
dispatch(addNotification({ style: 'success', msg: data }));
|
|
|
54 |
})
|
|
|
55 |
.catch((error) => {
|
|
|
56 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
57 |
});
|
|
|
58 |
};
|
|
|
59 |
|
|
|
60 |
const addComment = async (comment) => {
|
|
|
61 |
try {
|
|
|
62 |
const formData = new FormData();
|
|
|
63 |
formData.append('comment', comment);
|
|
|
64 |
|
|
|
65 |
const response = await axios.post(addUrl, formData);
|
|
|
66 |
const { success, data: newComment, total_comments: totalComments } = response.data;
|
|
|
67 |
|
|
|
68 |
if (!success) {
|
|
|
69 |
throw new Error('Ha ocurrido un error al intentar agregar el comentario.');
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
setComments((prev) => [...prev, newComment]);
|
|
|
73 |
onAdd({ comment: newComment, totalComments });
|
|
|
74 |
} catch (error) {
|
|
|
75 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
76 |
}
|
|
|
77 |
};
|
|
|
78 |
|
|
|
79 |
useEffect(() => setComments(defaultComments), [defaultComments]);
|
|
|
80 |
|
|
|
81 |
return (
|
|
|
82 |
<>
|
|
|
83 |
<CommentForm onSubmit={addComment} />
|
|
|
84 |
<CommentsList comments={comments} onReport={reportComment} onDelete={handleDelete} />
|
|
|
85 |
<ConfirmModal
|
|
|
86 |
show={showConfirm}
|
|
|
87 |
onClose={() => setShowConfirm(false)}
|
|
|
88 |
onAccept={deleteComment}
|
|
|
89 |
/>
|
|
|
90 |
</>
|
|
|
91 |
);
|
|
|
92 |
}
|