Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3511 | Rev 3513 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3511 Rev 3512
Línea 1... Línea 1...
1
import { useRef, useState } from 'react';
1
import { useRef, useState, useMemo } from 'react';
Línea 2... Línea 2...
2
 
2
 
-
 
3
import { useFetch } from '@shared/hooks';
Línea 3... Línea 4...
3
import { useFetch, useSearch } from '@shared/hooks';
4
import { useDebouncedSearchParam } from '@shared/hooks/useDebouncedSearchParam';
4
 
5
 
5
const CAPSULES_ROUTES = {
6
const CAPSULES_ROUTES = {
6
  pending: {
7
  pending: {
Línea 38... Línea 39...
38
  }
39
  }
39
];
40
];
Línea 40... Línea 41...
40
 
41
 
41
export function useMicrolearning() {
42
export function useMicrolearning() {
-
 
43
  const [category, setCategory] = useState(CAPSULES_CATEGORIES[0].value);
-
 
44
  const {
-
 
45
    inputValue: searchTerm,
-
 
46
    setInputValue: setSearchTerm,
-
 
47
    debouncedValue: debouncedSearchTerm
-
 
48
  } = useDebouncedSearchParam('search', 500);
42
  const [category, setCategory] = useState(CAPSULES_CATEGORIES[0].value);
49
 
43
  const capsulesRoutes = useRef(CAPSULES_ROUTES);
50
  const capsulesRoutes = useRef(CAPSULES_ROUTES);
Línea 44... Línea -...
44
  const capsuleCategories = useRef(CAPSULES_CATEGORIES);
-
 
45
 
-
 
46
  const { searchTerm, handleSearch } = useSearch();
51
  const capsuleCategories = useRef(CAPSULES_CATEGORIES);
47
 
52
 
48
  const { data: capsules, loading: capsulesLoading } = useFetch(
53
  const { data: rawCapsules, loading: capsulesLoading } = useFetch(
49
    capsulesRoutes.current[category].link,
54
    capsulesRoutes.current[category].link,
Línea -... Línea 55...
-
 
55
    { initialParams: capsulesRoutes.current[category].params }
-
 
56
  );
-
 
57
 
-
 
58
  const capsules = useMemo(() => {
-
 
59
    if (!rawCapsules?.data) return [];
-
 
60
    if (!debouncedSearchTerm) return rawCapsules.data;
-
 
61
    return rawCapsules.data.filter((capsule) =>
-
 
62
      capsule.title?.toLowerCase().includes(debouncedSearchTerm.toLowerCase())
50
    { initialParams: capsulesRoutes.current[category].params }
63
    );
51
  );
64
  }, [rawCapsules, debouncedSearchTerm]);
52
 
65
 
Línea 53... Línea -...
53
  const { data: currentCapsule, loading: currentCapsuleLoading } = useFetch(
-
 
54
    '/microlearning/last-capsule-in-progress'
-
 
55
  );
-
 
56
 
-
 
57
  const filteredCapsules =
66
  const { data: currentCapsule, loading: currentCapsuleLoading } = useFetch(
58
    capsules?.filter((capsule) => capsule.name.toLowerCase().includes(searchTerm.toLowerCase())) ||
67
    '/microlearning/last-capsule-in-progress'
59
    [];
68
  );
Línea 60... Línea 69...
60
 
69
 
61
  const changeCategory = (value) => {
70
  const changeCategory = (value) => {
62
    setCategory(value);
71
    setCategory(value);
63
  };
72
  };
64
 
73
 
65
  return {
74
  return {
66
    capsules: filteredCapsules,
75
    capsules,
67
    currentCapsule,
-
 
68
    capsulesLoading,
76
    currentCapsule,
69
    currentCapsuleLoading,
77
    capsulesLoading,
-
 
78
    currentCapsuleLoading,
70
    category,
79
    category,
71
    categories: capsuleCategories.current,
80
    categories: capsuleCategories.current,