Proyectos de Subversion LeadersLinked - SPA

Rev

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

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