2024-06-19 15:37:58 -06:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
import '../App.css';
|
|
|
|
|
import CardProfesor from './CardProfesor';
|
|
|
|
|
|
2024-06-20 18:51:54 -06:00
|
|
|
const Mapeo = ({ filters }) => {
|
2024-06-19 15:37:58 -06:00
|
|
|
const [profesores, setProfesores] = useState([]);
|
2024-06-19 16:17:02 -06:00
|
|
|
const [currentPage, setCurrentPage] = useState(1);
|
|
|
|
|
const [totalPages, setTotalPages] = useState(1);
|
2024-06-21 13:01:35 -06:00
|
|
|
const [edificios, setEdificios] = useState([]);
|
2024-06-19 15:37:58 -06:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-19 16:17:02 -06:00
|
|
|
fetchProfesores();
|
2024-06-20 18:51:54 -06:00
|
|
|
}, [currentPage, filters]);
|
2024-06-19 16:17:02 -06:00
|
|
|
|
|
|
|
|
const fetchProfesores = () => {
|
2024-06-20 18:51:54 -06:00
|
|
|
const query = new URLSearchParams({
|
|
|
|
|
page: currentPage,
|
|
|
|
|
limit: 10,
|
|
|
|
|
...filters
|
|
|
|
|
}).toString();
|
|
|
|
|
|
|
|
|
|
fetch(`http://localhost:3000/profesor/page?${query}`)
|
2024-06-19 15:37:58 -06:00
|
|
|
.then(response => {
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error('Network response was not ok');
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
2024-06-19 16:17:02 -06:00
|
|
|
setProfesores(data.profesores);
|
|
|
|
|
setTotalPages(data.totalPages);
|
2024-06-19 15:37:58 -06:00
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error('Error fetching the data: ', error);
|
|
|
|
|
});
|
2024-06-21 13:01:35 -06:00
|
|
|
|
|
|
|
|
fetch('http://localhost:3000/edificio')
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(data => setEdificios(data))
|
|
|
|
|
.catch(error => console.error('Error fetching edificios:', error));
|
2024-06-19 16:17:02 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleNextPage = () => {
|
|
|
|
|
if (currentPage < totalPages) {
|
|
|
|
|
setCurrentPage(prevPage => prevPage + 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handlePrevPage = () => {
|
|
|
|
|
if (currentPage > 1) {
|
|
|
|
|
setCurrentPage(prevPage => prevPage - 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-06-19 15:37:58 -06:00
|
|
|
|
2024-06-22 08:50:29 -06:00
|
|
|
const getPageNumbers = () => {
|
|
|
|
|
const maxPagesToShow = 6;
|
|
|
|
|
const halfRange = Math.floor(maxPagesToShow / 2);
|
|
|
|
|
let start = Math.max(1, currentPage - halfRange);
|
|
|
|
|
let end = Math.min(totalPages, currentPage + halfRange);
|
|
|
|
|
|
|
|
|
|
if (currentPage - halfRange < 1) {
|
|
|
|
|
end = Math.min(totalPages, end + (halfRange - currentPage + 1));
|
|
|
|
|
}
|
|
|
|
|
if (currentPage + halfRange > totalPages) {
|
|
|
|
|
start = Math.max(1, start - (currentPage + halfRange - totalPages));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pages = [];
|
|
|
|
|
for (let i = start; i <= end; i++) {
|
|
|
|
|
pages.push(i);
|
|
|
|
|
}
|
|
|
|
|
return pages;
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-19 15:37:58 -06:00
|
|
|
return (
|
2024-06-20 17:40:28 -06:00
|
|
|
<div className="main-content">
|
2024-06-21 13:01:35 -06:00
|
|
|
{profesores.map(profesor => {
|
|
|
|
|
const edificio = edificios.find(e => e.id_edificio === profesor.id_edificio);
|
|
|
|
|
const edificioNombre = edificio ? edificio.edificio : 'Desconocido';
|
|
|
|
|
return (
|
|
|
|
|
<CardProfesor key={profesor.id_profesor} profesor={profesor} edificioNombre={edificioNombre} />
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-06-19 16:17:02 -06:00
|
|
|
|
2024-06-20 17:40:28 -06:00
|
|
|
<nav aria-label="Page navigation example">
|
|
|
|
|
<ul className="pagination justify-content-center">
|
|
|
|
|
<li className={`page-item ${currentPage === 1 ? 'disabled' : ''}`}>
|
|
|
|
|
<a className="page-link" href="#" onClick={handlePrevPage}>
|
2024-06-20 18:51:54 -06:00
|
|
|
Anterior
|
2024-06-20 17:40:28 -06:00
|
|
|
</a>
|
|
|
|
|
</li>
|
2024-06-22 08:50:29 -06:00
|
|
|
{getPageNumbers().map(page => (
|
|
|
|
|
<li key={page} className={`page-item ${currentPage === page ? 'active' : ''}`}>
|
|
|
|
|
<a className="page-link" href="#" onClick={() => setCurrentPage(page)}>
|
|
|
|
|
{page}
|
2024-06-20 17:40:28 -06:00
|
|
|
</a>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
<li className={`page-item ${currentPage === totalPages ? 'disabled' : ''}`}>
|
|
|
|
|
<a className="page-link" href="#" onClick={handleNextPage}>
|
2024-06-20 18:51:54 -06:00
|
|
|
Siguiente
|
2024-06-20 17:40:28 -06:00
|
|
|
</a>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</nav>
|
2024-06-19 15:37:58 -06:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Mapeo;
|