Refactor invoice loading and search functionality for improved performance and user experience
This commit is contained in:
parent
810dbd7781
commit
aa8f049444
|
|
@ -67,7 +67,7 @@ export function renderFacturasPage() {
|
||||||
// Función para renderizar la página actual
|
// Función para renderizar la página actual
|
||||||
function renderPage(page = 1) {
|
function renderPage(page = 1) {
|
||||||
const invoicesList = container.querySelector('.invoices-list');
|
const invoicesList = container.querySelector('.invoices-list');
|
||||||
|
|
||||||
currentPage = page;
|
currentPage = page;
|
||||||
totalPages = Math.ceil(filteredInvoices.length / pageSize);
|
totalPages = Math.ceil(filteredInvoices.length / pageSize);
|
||||||
|
|
||||||
|
|
@ -100,23 +100,23 @@ export function renderFacturasPage() {
|
||||||
// Función para cargar todas las facturas
|
// Función para cargar todas las facturas
|
||||||
async function loadAllInvoices() {
|
async function loadAllInvoices() {
|
||||||
const invoicesList = container.querySelector('.invoices-list');
|
const invoicesList = container.querySelector('.invoices-list');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
invoicesList.innerHTML = '<div class="loading">Cargando facturas...</div>';
|
invoicesList.innerHTML = '<div class="loading">Cargando facturas...</div>';
|
||||||
|
|
||||||
const token = localStorage.getItem('token');
|
const token = localStorage.getItem('token');
|
||||||
|
|
||||||
// Construir URL con parámetros
|
// Construir URL con parámetros
|
||||||
let url = `${import.meta.env.VITE_API_BASE_URL}/api/Invoices?limit=1000`;
|
let url = `${import.meta.env.VITE_API_BASE_URL}/api/Invoices?limit=1000`;
|
||||||
|
|
||||||
if (currentFilter) {
|
if (currentFilter) {
|
||||||
url += `&status=${currentFilter}`;
|
url += `&status=${currentFilter}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (searchTerm) {
|
if (searchTerm) {
|
||||||
url += `&search=${encodeURIComponent(searchTerm)}`;
|
url += `&search=${encodeURIComponent(searchTerm)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -130,13 +130,13 @@ export function renderFacturasPage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
// Manejo de respuesta - obtener todas las facturas
|
// Manejo de respuesta - obtener todas las facturas
|
||||||
allInvoices = Array.isArray(data) ? data : data.data || data.invoices || [];
|
allInvoices = Array.isArray(data) ? data : data.data || data.invoices || [];
|
||||||
filteredInvoices = [...allInvoices];
|
filteredInvoices = [...allInvoices];
|
||||||
|
|
||||||
console.log(`Total de facturas cargadas: ${allInvoices.length}`);
|
console.log(`Total de facturas cargadas: ${allInvoices.length}`);
|
||||||
|
|
||||||
// Renderizar la primera página
|
// Renderizar la primera página
|
||||||
renderPage(1);
|
renderPage(1);
|
||||||
|
|
||||||
|
|
@ -183,16 +183,15 @@ export function renderFacturasPage() {
|
||||||
|
|
||||||
const searchInput = container.querySelector('.search-input');
|
const searchInput = container.querySelector('.search-input');
|
||||||
searchInput.addEventListener('input', (e) => {
|
searchInput.addEventListener('input', (e) => {
|
||||||
const inputValue = e.target.value;
|
const inputValue = e.target.value.trim();
|
||||||
// Debounce para no hacer muchas llamadas
|
// Debounce para no hacer muchas llamadas
|
||||||
clearTimeout(searchInput.debounceTimer);
|
clearTimeout(searchInput.debounceTimer);
|
||||||
searchInput.debounceTimer = setTimeout(() => {
|
searchInput.debounceTimer = setTimeout(() => {
|
||||||
// Solo aplicar búsqueda si contiene al menos un número
|
// Validación mínima: al menos 2 caracteres para activar búsqueda
|
||||||
const hasNumber = /\d/.test(inputValue);
|
if (inputValue.length >= 2) {
|
||||||
if (hasNumber && inputValue !== 'I' && inputValue !== 'IN' && inputValue !== 'N') {
|
|
||||||
searchTerm = inputValue;
|
searchTerm = inputValue;
|
||||||
} else {
|
} else {
|
||||||
searchTerm = ''; // Si no tiene número, mostrar todas
|
searchTerm = ''; // Si es muy corto, mostrar todas
|
||||||
}
|
}
|
||||||
applyFilters();
|
applyFilters();
|
||||||
}, 500); // Espera 500ms después de que el usuario deja de escribir
|
}, 500); // Espera 500ms después de que el usuario deja de escribir
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue