Rev 3452 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { useEffect, useState } from 'react';
import { useAlert, useApi } from '@shared/hooks';
import { addComment, deleteComment, getComments, getKnowledge } from '../services';
export function useKnowledge(uuid) {
const [comments, setComments] = useState([]);
const [knowledge, setKnowledge] = useState({
category: '',
title: '',
description: '',
link: null,
image: '',
attachment: '',
reaction: '',
routeComments: '',
routeCommentAdd: '',
routeSaveReaction: '',
routeDeleteReaction: ''
});
const { showError, showSuccess } = useAlert();
const { loading } = useApi(getKnowledge, {
autoFetch: true,
autoFetchArgs: [uuid],
onSuccess: (data) => {
setKnowledge(data);
},
onError: (error) => {
showError(error.message);
}
});
const { loading: commentsLoading, execute: executeGetComments } = useApi(getComments, {
onSuccess: (data) => {
setComments(data);
},
onError: (error) => {
showError(error.message);
}
});
const { execute: executeDeleteComment } = useApi(deleteComment, {
onSuccess: (message) => {
showSuccess(message);
},
onError: (error) => {
showError(error.message);
}
});
const { execute: executeAddComment } = useApi(addComment, {
onSuccess: (data) => {
setComments((prevMessages) => [...prevMessages, data]);
},
onError: (error) => {
showError(error.message);
}
});
useEffect(() => {
if (knowledge.routeComments) executeGetComments(knowledge.routeComments);
}, [knowledge.routeComments]);
return {
knowledge,
loading,
comments,
commentsLoading,
addComment: executeAddComment,
deleteComment: executeDeleteComment
};
}