Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3452 Rev 3665
Línea 1... Línea 1...
1
import { useEffect, useState } from 'react';
1
import { useEffect, useState } from 'react';
2
import { useDispatch } from 'react-redux';
-
 
Línea 3... Línea 2...
3
 
2
 
4
import { useFetch } from '@hooks';
3
import { useAlert, useApi } from '@shared/hooks';
5
import { addComment, deleteComment } from '../services';
-
 
Línea 6... Línea 4...
6
import { addNotification } from '@store/notification/notification.actions';
4
import { addComment, deleteComment, getComments, getKnowledge } from '../services';
7
 
5
 
8
export function useKnowledge(uuid) {
6
export function useKnowledge(uuid) {
9
  const [comments, setComments] = useState([]);
7
  const [comments, setComments] = useState([]);
Línea 18... Línea 16...
18
    routeComments: '',
16
    routeComments: '',
19
    routeCommentAdd: '',
17
    routeCommentAdd: '',
20
    routeSaveReaction: '',
18
    routeSaveReaction: '',
21
    routeDeleteReaction: ''
19
    routeDeleteReaction: ''
22
  });
20
  });
23
  const dispatch = useDispatch();
-
 
Línea 24... Línea 21...
24
 
21
 
25
  const { data: knowledgeData, isLoading: knowledgeLoading } = useFetch(
-
 
26
    uuid ? `/knowledge-area/view/${uuid}` : ''
-
 
27
  );
-
 
28
  const { data: commentsData, isLoading: commentsLoading } = useFetch(knowledge.routeComments);
22
  const { showError, showSuccess } = useAlert();
29
 
23
 
30
  const handleAddComment = async (comment) => {
24
  const { loading } = useApi(getKnowledge, {
31
    try {
25
    autoFetch: true,
-
 
26
    autoFetchArgs: [uuid],
32
      const newComment = await addComment(knowledge.routeCommentAdd, comment);
27
    onSuccess: (data) => {
-
 
28
      setKnowledge(data);
33
      setComments((prevMessages) => [...prevMessages, newComment]);
29
    },
34
    } catch (error) {
30
    onError: (error) => {
35
      dispatch(addNotification({ style: 'danger', msg: error.message }));
31
      showError(error.message);
36
    }
32
    }
Línea 37... Línea 33...
37
  };
33
  });
38
 
-
 
39
  const handleDeleteComment = async (id, url) => {
34
 
40
    try {
-
 
41
      const message = await deleteComment(url);
35
  const { loading: commentsLoading, execute: executeGetComments } = useApi(getComments, {
42
      const newComments = comments.filter((comment) => comment.unique !== id);
36
    onSuccess: (data) => {
43
      setComments(newComments);
-
 
44
 
37
      setComments(data);
45
      dispatch(addNotification({ style: 'success', msg: message }));
38
    },
46
    } catch (error) {
39
    onError: (error) => {
47
      dispatch(addNotification({ style: 'danger', msg: error.message }));
40
      showError(error.message);
Línea -... Línea 41...
-
 
41
    }
-
 
42
  });
-
 
43
 
-
 
44
  const { execute: executeDeleteComment } = useApi(deleteComment, {
48
    }
45
    onSuccess: (message) => {
-
 
46
      showSuccess(message);
-
 
47
    },
-
 
48
    onError: (error) => {
-
 
49
      showError(error.message);
-
 
50
    }
-
 
51
  });
49
  };
52
 
-
 
53
  const { execute: executeAddComment } = useApi(addComment, {
-
 
54
    onSuccess: (data) => {
50
 
55
      setComments((prevMessages) => [...prevMessages, data]);
-
 
56
    },
-
 
57
    onError: (error) => {
Línea 51... Línea 58...
51
  useEffect(() => {
58
      showError(error.message);
52
    if (knowledgeData) setKnowledge(knowledgeData);
59
    }
53
  }, [knowledgeData]);
60
  });
Línea 54... Línea 61...
54
 
61
 
55
  useEffect(() => {
62
  useEffect(() => {
-
 
63
    if (knowledge.routeComments) executeGetComments(knowledge.routeComments);
56
    if (commentsData) setComments(commentsData);
64
  }, [knowledge.routeComments]);
57
  }, [commentsData]);
-
 
58
 
65
 
59
  return {
66
  return {
60
    knowledge,
67
    knowledge,
61
    comments,
68
    loading,
62
    knowledgeLoading,
69
    comments,