Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3658 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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