Proyectos de Subversion LeadersLinked - SPA

Rev

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

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