Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3719 stevensc 1
import { useEffect, useRef, useState } from 'react';
2
 
3
import { useApi } from '@shared/hooks';
4
import { useDebouncedSearchParam } from '@shared/hooks/useDebouncedSearchParam';
5
import { getCapsules, getLastCapsuleInProgress } from '@microlearning/services';
6
 
7
const CAPSULES_ROUTES = {
8
  pending: {
9
    link: '/microlearning/capsules-pending',
10
    params: {
11
      order_field: 'added_on',
12
      order_direction: 'desc'
13
    }
14
  },
15
  progress: {
16
    link: '/microlearning/capsules-in-progress',
17
    params: {}
18
  },
19
  completed: {
20
    link: '/microlearning/capsules-completed',
21
    params: {
22
      order_field: 'last_access_on',
23
      order_direction: 'desc'
24
    }
25
  }
26
};
27
 
28
const CAPSULES_CATEGORIES = [
29
  {
30
    label: 'Pendiente',
31
    value: 'pending'
32
  },
33
  {
34
    label: 'En curso',
35
    value: 'progress'
36
  },
37
  {
38
    label: 'Finalizado',
39
    value: 'completed'
40
  }
41
];
42
 
43
export function useMicrolearning() {
44
  const [category, setCategory] = useState(CAPSULES_CATEGORIES[0].value);
45
 
46
  const {
47
    inputValue: searchTerm,
48
    setInputValue: setSearchTerm,
49
    debouncedValue: debouncedSearchTerm
50
  } = useDebouncedSearchParam('search', 500);
51
 
52
  const capsulesRoutes = useRef(CAPSULES_ROUTES);
53
  const capsuleCategories = useRef(CAPSULES_CATEGORIES);
54
 
55
  const {
56
    data: capsules,
57
    loading: capsulesLoading,
58
    execute: fetchCapsules
59
  } = useApi(getCapsules, {
60
    autoFetch: true
61
  });
62
 
63
  const { data: currentCapsule, loading: currentCapsuleLoading } = useApi(
64
    getLastCapsuleInProgress,
65
    { autoFetch: true }
66
  );
67
 
68
  useEffect(() => {
69
    fetchCapsules(capsulesRoutes.current[category].link, {
70
      params: capsulesRoutes.current[category].params
71
    });
72
  }, [category, debouncedSearchTerm]);
73
 
74
  return {
75
    category,
76
    capsules: capsules || [],
77
    currentCapsule,
78
    capsulesLoading,
79
    currentCapsuleLoading,
80
    categories: capsuleCategories.current,
81
    changeCategory: setCategory,
82
    searchCapsules: setSearchTerm,
83
    searchTerm
84
  };
85
}