| 3481 |
stevensc |
1 |
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
2 |
import { axios } from '../../../utils';
|
|
|
3 |
import { useSelector } from 'react-redux';
|
|
|
4 |
import { Box, Typography } from '@mui/material';
|
|
|
5 |
|
|
|
6 |
import { useNearScreen, useMicroLearning } from '@hooks';
|
|
|
7 |
|
|
|
8 |
import Spinner from 'components/UI/Spinner';
|
|
|
9 |
import EmptySection from 'components/UI/EmptySection';
|
|
|
10 |
import DetailTag from 'components/micro-learning/DetailTag';
|
|
|
11 |
|
|
|
12 |
const TimelinePage = () => {
|
|
|
13 |
const { link_timeline: linkTimeline } = useMicroLearning();
|
|
|
14 |
const [items, setItems] = useState([]);
|
|
|
15 |
const [isLoading, setIsLoading] = useState(false);
|
|
|
16 |
const [totalPages, setTotalPages] = useState(1);
|
|
|
17 |
const [currentPage, setCurrentPage] = useState(2);
|
|
|
18 |
const loaderRef = useRef(null);
|
|
|
19 |
const [isIntercepting] = useNearScreen({
|
|
|
20 |
externalRef: loaderRef,
|
|
|
21 |
once: false,
|
|
|
22 |
rootMargin: '20px'
|
|
|
23 |
});
|
|
|
24 |
|
|
|
25 |
const fetchData = useCallback(
|
|
|
26 |
(url = '') => {
|
|
|
27 |
if (isLoading || !url) return;
|
|
|
28 |
|
|
|
29 |
setIsLoading(true);
|
|
|
30 |
|
|
|
31 |
axios
|
|
|
32 |
.get(`${url}?page=${currentPage}`)
|
|
|
33 |
.then((response) => {
|
|
|
34 |
const { data, success } = response.data;
|
|
|
35 |
|
|
|
36 |
if (!success) {
|
|
|
37 |
throw new Error(data);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
setCurrentPage((prevCurrentPage) => prevCurrentPage + 1);
|
|
|
41 |
setTotalPages(data.total.pages);
|
|
|
42 |
setItems((prevItems) => [...prevItems, ...data.current.items]);
|
|
|
43 |
})
|
|
|
44 |
.catch((err) => console.log(err.message))
|
|
|
45 |
.finally(() => setIsLoading(false));
|
|
|
46 |
},
|
|
|
47 |
[currentPage, isLoading, totalPages, linkTimeline]
|
|
|
48 |
);
|
|
|
49 |
|
|
|
50 |
useEffect(() => {
|
|
|
51 |
if (isIntercepting) fetchData(linkTimeline);
|
|
|
52 |
}, [isIntercepting]);
|
|
|
53 |
|
|
|
54 |
useEffect(() => {
|
|
|
55 |
const getData = (url = '') => {
|
|
|
56 |
setIsLoading(true);
|
|
|
57 |
|
|
|
58 |
axios
|
|
|
59 |
.get(url)
|
|
|
60 |
.then((response) => {
|
|
|
61 |
const { data, success } = response.data;
|
|
|
62 |
|
|
|
63 |
if (!success) {
|
|
|
64 |
throw new Error(data);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
setTotalPages(data.total.pages);
|
|
|
68 |
setItems(data.current.items);
|
|
|
69 |
})
|
|
|
70 |
.catch((err) => console.log(err.message))
|
|
|
71 |
.finally(() => setIsLoading(false));
|
|
|
72 |
};
|
|
|
73 |
|
|
|
74 |
getData(linkTimeline);
|
|
|
75 |
}, [linkTimeline]);
|
|
|
76 |
|
|
|
77 |
return (
|
|
|
78 |
<>
|
|
|
79 |
<Typography variant='h2'>Linea de tiempo</Typography>
|
|
|
80 |
|
|
|
81 |
<Box
|
|
|
82 |
display='flex'
|
|
|
83 |
flexDirection='column'
|
|
|
84 |
gap={1}
|
|
|
85 |
width='100%'
|
|
|
86 |
mt={2}
|
|
|
87 |
maxHeight='60vh'
|
|
|
88 |
overflow='auto'
|
|
|
89 |
>
|
|
|
90 |
<TimelineList items={items} />
|
|
|
91 |
|
|
|
92 |
<div ref={loaderRef}>{currentPage < totalPages ? <Spinner /> : null}</div>
|
|
|
93 |
</Box>
|
|
|
94 |
</>
|
|
|
95 |
);
|
|
|
96 |
};
|
|
|
97 |
|
|
|
98 |
const TimelineList = ({ items = [] }) => {
|
|
|
99 |
const labels = useSelector(({ intl }) => intl.labels);
|
|
|
100 |
|
|
|
101 |
if (!items.length) {
|
|
|
102 |
return <EmptySection message={labels.datatable_szerorecords} />;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return (
|
|
|
106 |
<>
|
|
|
107 |
{items.map(({ activity, added_on }, i) => (
|
|
|
108 |
<DetailTag key={i} title={activity} label={added_on} />
|
|
|
109 |
))}
|
|
|
110 |
</>
|
|
|
111 |
);
|
|
|
112 |
};
|
|
|
113 |
|
|
|
114 |
export default TimelinePage;
|