Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3658 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import { useEffect, useRef, useState } from 'react';

import { useApi } from '@shared/hooks';
import { useDebouncedSearchParam } from '@shared/hooks/useDebouncedSearchParam';
import { getCapsules, getLastCapsuleInProgress } from '@microlearning/services';

const CAPSULES_ROUTES = {
  pending: {
    link: '/microlearning/capsules-pending',
    params: {
      order_field: 'added_on',
      order_direction: 'desc'
    }
  },
  progress: {
    link: '/microlearning/capsules-in-progress',
    params: {}
  },
  completed: {
    link: '/microlearning/capsules-completed',
    params: {
      order_field: 'last_access_on',
      order_direction: 'desc'
    }
  }
};

const CAPSULES_CATEGORIES = [
  {
    label: 'Pendiente',
    value: 'pending'
  },
  {
    label: 'En curso',
    value: 'progress'
  },
  {
    label: 'Finalizado',
    value: 'completed'
  }
];

export function useMicrolearning() {
  const [category, setCategory] = useState(CAPSULES_CATEGORIES[0].value);

  const {
    inputValue: searchTerm,
    setInputValue: setSearchTerm,
    debouncedValue: debouncedSearchTerm
  } = useDebouncedSearchParam('search', 500);

  const capsulesRoutes = useRef(CAPSULES_ROUTES);
  const capsuleCategories = useRef(CAPSULES_CATEGORIES);

  const {
    data: capsules,
    loading: capsulesLoading,
    execute: fetchCapsules
  } = useApi(getCapsules, {
    autoFetch: true
  });

  const { data: currentCapsule, loading: currentCapsuleLoading } = useApi(
    getLastCapsuleInProgress,
    { autoFetch: true }
  );

  useEffect(() => {
    fetchCapsules(capsulesRoutes.current[category].link, {
      params: capsulesRoutes.current[category].params
    });
  }, [category, debouncedSearchTerm]);

  return {
    category,
    capsules: capsules || [],
    currentCapsule,
    capsulesLoading,
    currentCapsuleLoading,
    categories: capsuleCategories.current,
    changeCategory: setCategory,
    searchCapsules: setSearchTerm,
    searchTerm
  };
}