Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3545 | Rev 3547 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3545 Rev 3546
Línea 1... Línea 1...
1
import { useEffect, useState } from 'react';
1
import { useCallback, useEffect, useState } from 'react';
Línea 2... Línea 2...
2
 
2
 
3
import { useAlert, useAlertModal, useFetch } from '@shared/hooks';
3
import { useAlert, useAlertModal, useFetch } from '@shared/hooks';
Línea 4... Línea 4...
4
import { addCapsuleComment, deleteCapsuleComment } from '@microlearning/services';
4
import { addCapsuleComment, deleteCapsuleComment } from '@microlearning/services';
Línea 29... Línea 29...
29
  const { showSuccess, showError } = useAlert();
29
  const { showSuccess, showError } = useAlert();
30
  const { showAlert } = useAlertModal();
30
  const { showAlert } = useAlertModal();
Línea 31... Línea 31...
31
 
31
 
Línea -... Línea 32...
-
 
32
  const { data, loading, refetch } = useFetch(`/microlearning/capsules/${uuid}`);
32
  const { data, loading, refetch } = useFetch(`/microlearning/capsules/${uuid}`);
33
 
33
 
34
  const addComment = useCallback(
34
  const addComment = async ({ comment, rating }) => {
35
    async ({ comment, rating }) => {
35
    try {
36
      try {
36
      const {
37
        const {
37
        message,
38
          message,
38
        capsule,
39
          capsule,
39
        comment: newComment
40
          comment: newComment
40
      } = await addCapsuleComment(capsule.link_comment_add, {
41
        } = await addCapsuleComment(capsule.link_comment_add, {
41
        comment,
42
          comment,
42
        rating
43
          rating
43
      });
44
        });
44
      showSuccess(message);
45
        showSuccess(message);
45
      setCapsule((prev) => ({ ...prev, ...capsule, comments: [newComment, ...prev.comments] }));
46
        setCapsule((prev) => ({ ...prev, ...capsule, comments: [newComment, ...prev.comments] }));
-
 
47
      } catch (error) {
46
    } catch (error) {
48
        showError(error.message);
-
 
49
      }
47
      showError(error.message);
50
    },
48
    }
51
    [capsule.link_comment_add, showError, showSuccess]
49
  };
52
  );
-
 
53
 
50
 
54
  const removeComment = useCallback(
51
  const removeComment = async (url, uuid) => {
55
    async (url, uuid) => {
52
    try {
56
      try {
53
      const { message, capsule } = await deleteCapsuleComment(url);
57
        const { message, capsule } = await deleteCapsuleComment(url);
54
      showSuccess(message);
58
        showSuccess(message);
55
      setCapsule((prev) => ({
59
        setCapsule((prev) => ({
56
        ...prev,
60
          ...prev,
57
        ...capsule,
61
          ...capsule,
58
        comments: prev.comments.filter((c) => c.uuid !== uuid)
62
          comments: prev.comments.filter((c) => c.uuid !== uuid)
59
      }));
63
        }));
-
 
64
      } catch (error) {
60
    } catch (error) {
65
        showError(error.message);
-
 
66
      }
61
      showError(error.message);
67
    },
62
    }
68
    [showError, showSuccess]
63
  };
69
  );
-
 
70
 
64
 
71
  const deleteComment = useCallback(
65
  const deleteComment = (url, uuid) => {
72
    (url, uuid) => {
66
    if (!url || !uuid) {
73
      if (!url || !uuid) {
67
      showError('Error: URL o UUID no válidos');
74
        showError('Error: URL o UUID no válidos');
68
      return;
75
        return;
69
    }
76
      }
70
 
77
 
71
    try {
78
      try {
72
      showAlert({
79
        showAlert({
73
        message: '¿Estás seguro de querer eliminar este comentario?',
80
          message: '¿Estás seguro de querer eliminar este comentario?',
74
        onConfirm: () => {
81
          onConfirm: () => {
75
          removeComment(url, uuid);
82
            removeComment(url, uuid);
76
        }
83
          }
77
      });
84
        });
78
    } catch (error) {
85
      } catch (error) {
-
 
86
        console.error('Error al mostrar el diálogo de confirmación:', error);
79
      console.error('Error al mostrar el diálogo de confirmación:', error);
87
        showError('Error al mostrar el diálogo de confirmación');
-
 
88
      }
80
      showError('Error al mostrar el diálogo de confirmación');
89
    },
Línea 81... Línea 90...
81
    }
90
    [showError, showSuccess]
82
  };
91
  );
83
 
92