Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3665 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import { useEffect, useState } from 'react';
2
 
3
import { useAlert, useApi } from '@shared/hooks';
4
import { addComment, deleteComment, getComments, getKnowledge } from '../services';
5
 
6
export function useKnowledge(uuid) {
7
  const [comments, setComments] = useState([]);
8
  const [knowledge, setKnowledge] = useState({
9
    category: '',
10
    title: '',
11
    description: '',
12
    link: null,
13
    image: '',
14
    attachment: '',
15
    reaction: '',
16
    routeComments: '',
17
    routeCommentAdd: '',
18
    routeSaveReaction: '',
19
    routeDeleteReaction: ''
20
  });
21
 
22
  const { showError, showSuccess } = useAlert();
23
 
24
  const { loading } = useApi(getKnowledge, {
25
    autoFetch: true,
26
    autoFetchArgs: [uuid],
27
    onSuccess: (data) => {
28
      setKnowledge(data);
29
    },
30
    onError: (error) => {
31
      showError(error.message);
32
    }
33
  });
34
 
35
  const { loading: commentsLoading, execute: executeGetComments } = useApi(getComments, {
36
    onSuccess: (data) => {
37
      setComments(data);
38
    },
39
    onError: (error) => {
40
      showError(error.message);
41
    }
42
  });
43
 
44
  const { execute: executeDeleteComment } = useApi(deleteComment, {
45
    onSuccess: (message) => {
46
      showSuccess(message);
47
    },
48
    onError: (error) => {
49
      showError(error.message);
50
    }
51
  });
52
 
53
  const { execute: executeAddComment } = useApi(addComment, {
54
    onSuccess: (data) => {
55
      setComments((prevMessages) => [...prevMessages, data]);
56
    },
57
    onError: (error) => {
58
      showError(error.message);
59
    }
60
  });
61
 
62
  const handleAddComment = (comment) => {
63
    executeAddComment(knowledge.routeCommentAdd, comment);
64
  };
65
 
66
  useEffect(() => {
67
    if (knowledge.routeComments) executeGetComments(knowledge.routeComments);
68
  }, [knowledge.routeComments]);
69
 
70
  return {
71
    knowledge,
72
    loading,
73
    comments,
74
    commentsLoading,
75
    addComment: handleAddComment,
76
    deleteComment: executeDeleteComment
77
  };
78
}