Rev 3546 | Rev 3555 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { useCallback, useEffect, useState } from 'react';import { useAlert, useAlertModal, useFetch } from '@shared/hooks';import { addCapsuleComment, deleteCapsuleComment } from '@microlearning/services';const DEFAULT_STATE = {uuid: '',name: '',description: '',image: '',link_comments: '',link_comment_add: '',link_slides: '',total_comments: '',total_rating: '0',progress: 0,completed: 0,total_slides: null,link_first_slide: '',type_first_slide: '',order: null,added_on: '',updated_on: ''};export function useCapsule(uuid) {const [capsule, setCapsule] = useState(DEFAULT_STATE);const { showSuccess, showError } = useAlert();const { showAlert } = useAlertModal();const { data, loading, refetch } = useFetch(`/microlearning/capsules/${uuid}`);const addComment = useCallback(async ({ comment, rating }) => {try {const {message,capsule: updatedCapsule,comment: newComment} = await addCapsuleComment(capsule.link_comment_add, {comment,rating});showSuccess(message);setCapsule((prev) => ({...prev,...updatedCapsule,comments: [newComment, ...prev.comments]}));} catch (error) {showError(error.message);}},[capsule.link_comment_add, showError, showSuccess]);const removeComment = useCallback(async (url, uuid) => {try {const { message, capsule: updatedCapsule } = await deleteCapsuleComment(url);showSuccess(message);setCapsule((prev) => ({...prev,...updatedCapsule,comments: prev.comments.filter((c) => c.uuid !== uuid)}));} catch (error) {showError(error.message);}},[showError, showSuccess]);const deleteComment = useCallback((url, uuid) => {if (!url || !uuid) {showError('Error: URL o UUID no válidos');return;}try {showAlert({message: '¿Estás seguro de querer eliminar este comentario?',onConfirm: () => removeComment(url, uuid)});} catch (error) {console.error('Error al mostrar el diálogo de confirmación:', error);showError('Error al mostrar el diálogo de confirmación');}},[showError, showSuccess]);useEffect(() => {if (data) setCapsule(data);}, [data]);return { capsule, loading, refetch, addComment, deleteComment };}