Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3527 Rev 3538
Línea 1... Línea 1...
1
import { useEffect, useState } from 'react';
1
import { useEffect, useState } from 'react';
Línea 2... Línea 2...
2
 
2
 
3
import { useAlert, useFetch } from '@shared/hooks';
3
import { useAlert, useAlertModal, useFetch } from '@shared/hooks';
Línea 4... Línea 4...
4
import { api } from '@shared/libs';
4
import { addCapsuleComment, deleteCapsuleComment } from '@microlearning/services';
5
 
5
 
6
const DEFAULT_STATE = {
6
const DEFAULT_STATE = {
7
  uuid: '',
7
  uuid: '',
Línea 25... Línea 25...
25
 
25
 
26
export function useCapsule(uuid) {
26
export function useCapsule(uuid) {
Línea 27... Línea 27...
27
  const [capsule, setCapsule] = useState(DEFAULT_STATE);
27
  const [capsule, setCapsule] = useState(DEFAULT_STATE);
-
 
28
 
Línea 28... Línea 29...
28
 
29
  const { showSuccess, showError } = useAlert();
Línea 29... Línea 30...
29
  const { showSuccess, showError } = useAlert();
30
  const { showAlert } = useAlertModal();
30
 
31
 
-
 
32
  const { data, loading, refetch } = useFetch(`/microlearning/capsules/${uuid}`);
-
 
33
 
-
 
34
  const addComment = async ({ comment, rating }) => {
-
 
35
    try {
31
  const { data, loading, refetch } = useFetch(`/microlearning/capsules/${uuid}`);
36
      const {
-
 
37
        message,
-
 
38
        capsule,
-
 
39
        comment: newComment
32
 
40
      } = await addCapsuleComment(capsule.link_comment_add, {
-
 
41
        comment,
33
  const addComment = async ({ comment, rating }) => {
42
        rating
34
    try {
43
      });
35
      const { message } = await api.post(capsule.link_comment_add, { comment, rating });
44
      showSuccess(message);
36
      showSuccess(message);
45
      setCapsule((prev) => ({ ...prev, ...capsule, comments: [newComment, ...prev.comments] }));
Línea -... Línea 46...
-
 
46
    } catch (error) {
-
 
47
      showError(error.message);
-
 
48
    }
-
 
49
  };
-
 
50
 
-
 
51
  const removeComment = async (url, uuid) => {
-
 
52
    try {
-
 
53
      const { message, capsule } = await deleteCapsuleComment(url);
-
 
54
      showSuccess(message);
-
 
55
      setCapsule((prev) => ({
-
 
56
        ...prev,
-
 
57
        ...capsule,
-
 
58
        comments: prev.comments.filter((c) => c.uuid !== uuid)
-
 
59
      }));
-
 
60
    } catch (error) {
-
 
61
      showError(error.message);
-
 
62
    }
-
 
63
  };
-
 
64
 
-
 
65
  const deleteComment = (url, uuid) => {
-
 
66
    showAlert({
37
    } catch (error) {
67
      message: '¿Estás seguro de querer eliminar este comentario?',
38
      showError(error.message);
68
      onConfirm: () => removeComment(url, uuid)
39
    }
69
    });
Línea 40... Línea 70...
40
  };
70
  };
41
 
71