AutorÃa | Ultima modificación | Ver Log |
import { useState } from 'react';import { useApi, useAlert } from '@shared/hooks';import {addComment as addCommentService,deleteComment as deleteCommentService} from '@my-coach/services';export function useAnswerComments(initialComments = [], { onAddComment, onDeleteComment }) {const [comments, setComments] = useState(initialComments);const { showError, showSuccess } = useAlert();const { execute: executeAddComment } = useApi(addCommentService);const { execute: executeDeleteComment } = useApi(deleteCommentService);const deleteComment = async (url) => {await executeDeleteComment(url).then(({ message, ...data }) => {setComments((prev) => prev.filter((c) => c.link_delete !== url));showSuccess(message);onDeleteComment(data);}).catch((error) => {showError(error.message);});};const addComment = async (url, comment) => {await executeAddComment(url, comment).then(({ message, ...data }) => {setComments((prev) => [...prev, data.item]);showSuccess(message);onAddComment(data.total_comments_answer);}).catch((error) => {showError(error.message);});};return {comments,addComment,deleteComment};}