Proyectos de Subversion LeadersLinked - SPA

Rev

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

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