Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3505 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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