Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3511 | Rev 3538 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import { useEffect, useState } from 'react';

import { useAlert, useFetch } from '@shared/hooks';
import { api } from '@shared/libs';

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 { data, loading, refetch } = useFetch(`/microlearning/capsules/${uuid}`);

  const addComment = async ({ comment, rating }) => {
    try {
      const { message } = await api.post(capsule.link_comment_add, { comment, rating });
      showSuccess(message);
    } catch (error) {
      showError(error.message);
    }
  };

  useEffect(() => {
    if (data) setCapsule(data);
  }, [data]);

  return { capsule, loading, refetch, addComment };
}