Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3659 Rev 3719
Línea 1... Línea 1...
1
import { useState } from 'react';
1
import { useState } from 'react';
2
 
2
 
3
import { useAlert, useAlertModal, useApi } from '@shared/hooks';
3
import { useAlert, useAlertModal, useApi } from '@shared/hooks';
4
import { addCapsuleComment, deleteCapsuleComment, getCapsule } 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: '',
8
  name: '',
8
  name: '',
9
  description: '',
9
  description: '',
10
  image: '',
10
  image: '',
11
  link_comments: '',
11
  link_comments: '',
12
  link_comment_add: '',
12
  link_comment_add: '',
13
  link_slides: '',
13
  link_slides: '',
14
  total_comments: '',
14
  total_comments: '',
15
  total_rating: '0',
15
  total_rating: '0',
16
  progress: 0,
16
  progress: 0,
17
  completed: 0,
17
  completed: 0,
18
  total_slides: null,
18
  total_slides: null,
19
  link_first_slide: '',
19
  link_first_slide: '',
20
  type_first_slide: '',
20
  type_first_slide: '',
21
  order: null,
21
  order: null,
22
  added_on: '',
22
  added_on: '',
23
  updated_on: ''
23
  updated_on: ''
24
};
24
};
25
 
25
 
26
export function useCapsule(uuid) {
26
export function useCapsule(uuid) {
27
  const [capsule, setCapsule] = useState(DEFAULT_STATE);
27
  const [capsule, setCapsule] = useState(DEFAULT_STATE);
28
  const [deletedUrl, setDeletedUrl] = useState(null);
28
  const [deletedUrl, setDeletedUrl] = useState(null);
29
 
29
 
30
  const { showSuccess, showError } = useAlert();
30
  const { showSuccess, showError } = useAlert();
31
  const { showAlert, closeAlert } = useAlertModal();
31
  const { showAlert, closeAlert } = useAlertModal();
32
 
32
 
33
  const { loading, execute: fetchCapsule } = useApi(getCapsule, {
33
  const { loading, execute: fetchCapsule } = useApi(getCapsule, {
34
    autoFetch: true,
34
    autoFetch: true,
35
    autoFetchArgs: [uuid],
35
    autoFetchArgs: [uuid],
36
    onSuccess: (data) => {
36
    onSuccess: (data) => {
37
      setCapsule(data);
37
      setCapsule(data);
38
    },
38
    },
39
    onError: (error) => {
39
    onError: (error) => {
40
      showError(error.message);
40
      showError(error.message);
41
    }
41
    }
42
  });
42
  });
43
 
43
 
44
  const { execute: addComment } = useApi(addCapsuleComment, {
44
  const { execute: addComment } = useApi(addCapsuleComment, {
45
    onSuccess: ({ message, capsule: updatedCapsule, comment: newComment }) => {
45
    onSuccess: ({ message, capsule: updatedCapsule, comment: newComment }) => {
46
      setCapsule((prev) => ({
46
      setCapsule((prev) => ({
47
        ...prev,
47
        ...prev,
48
        ...updatedCapsule,
48
        ...updatedCapsule,
49
        comments: [newComment, ...prev.comments]
49
        comments: [newComment, ...prev.comments]
50
      }));
50
      }));
51
      showSuccess(message);
51
      showSuccess(message);
52
    },
52
    },
53
    onError: (error) => {
53
    onError: (error) => {
54
      showError(error.message);
54
      showError(error.message);
55
    }
55
    }
56
  });
56
  });
57
 
57
 
58
  const { execute: deleteComment } = useApi(deleteCapsuleComment, {
58
  const { execute: deleteComment } = useApi(deleteCapsuleComment, {
59
    onSuccess: ({ message, capsule: updatedCapsule }) => {
59
    onSuccess: ({ message, capsule: updatedCapsule }) => {
60
      setCapsule((prev) => ({
60
      setCapsule((prev) => ({
61
        ...prev,
61
        ...prev,
62
        ...updatedCapsule,
62
        ...updatedCapsule,
63
        comments: prev.comments.filter((c) => c.link_delete !== deletedUrl)
63
        comments: prev.comments.filter((c) => c.link_delete !== deletedUrl)
64
      }));
64
      }));
65
      showSuccess(message);
65
      showSuccess(message);
66
      setDeletedUrl(null);
66
      setDeletedUrl(null);
67
      closeAlert();
67
      closeAlert();
68
    },
68
    },
69
    onError: (error) => {
69
    onError: (error) => {
70
      showError(error.message);
70
      showError(error.message);
71
    }
71
    }
72
  });
72
  });
73
 
73
 
74
  const handleAddComment = (comment) => {
74
  const handleAddComment = (comment) => {
75
    addComment(capsule.link_comment_add, comment);
75
    addComment(capsule.link_comment_add, comment);
76
  };
76
  };
77
 
77
 
78
  const removeComment = (url) => {
78
  const removeComment = (url) => {
79
    setDeletedUrl(url);
79
    setDeletedUrl(url);
80
    showAlert({
80
    showAlert({
81
      message: '¿Estás seguro de querer eliminar este comentario?',
81
      message: '¿Estás seguro de querer eliminar este comentario?',
82
      onConfirm: () => deleteComment(url)
82
      onConfirm: () => deleteComment(url)
83
    });
83
    });
84
  };
84
  };
85
 
85
 
86
  return { capsule, loading, fetchCapsule, addComment: handleAddComment, removeComment };
86
  return { capsule, loading, fetchCapsule, addComment: handleAddComment, removeComment };
87
}
87
}