AutorÃa | Ultima modificación | Ver Log |
import { useState } from 'react';
import { useAlert, useApi } from '@shared/hooks';
import { deleteQuestion, getQuestion } from '@my-coach/services';
export function useQuestion(uuid, { onDeleteQuestion }) {
const [question, setQuestion] = useState(null);
const { loading } = useApi(getQuestion, {
autoFetch: true,
autoFetchArgs: [uuid],
onSuccess: (data) => {
setQuestion(data);
},
onError: (error) => {
showError(error.message);
}
});
const { showSuccess, showError } = useAlert();
const { execute: executeDeleteQuestion } = useApi(deleteQuestion);
const updateTotalAnswers = (answers) => {
setQuestion((prev) => ({ ...prev, answers }));
};
const updateTotalComments = (comments) => {
setQuestion((prev) => ({ ...prev, comments }));
};
const updateTotalReactions = (reactions) => {
setQuestion((prev) => ({ ...prev, reactions }));
};
const removeQuestion = async (url) => {
await executeDeleteQuestion(url)
.then(({ message }) => {
showSuccess(message);
setQuestion(null);
onDeleteQuestion();
})
.catch((error) => {
showError(error.message);
});
};
return {
question,
loading,
updateTotalAnswers,
updateTotalComments,
updateTotalReactions,
deleteQuestion: removeQuestion
};
}