[FIX] website_sale_aplicoop: Arreglar búsqueda y filtrado por tags

Problemas resueltos:
- Contador de badges mostraba solo productos de página actual (20) en lugar del total
- Productos cargados con lazy loading no se filtraban por tags seleccionados

Cambios en realtime_search.js:
- Eliminado recálculo dinámico de contadores en _filterProducts()
- Los contadores permanecen estáticos (calculados por backend sobre dataset completo)
- Mejorado logging para debug de tags seleccionados

Cambios en infinite_scroll.js:
- Después de cargar nueva página, actualiza lista de productos para realtime search
- Aplica filtros activos automáticamente a productos recién cargados
- Garantiza consistencia de estado de filtrado en toda la aplicación

Documentación:
- Añadido docs/TAG_FILTER_FIX.md con explicación completa del sistema
- Incluye arquitectura, flujo de datos y casos de prueba
This commit is contained in:
snt 2026-02-18 18:51:26 +01:00
parent fee8ec9c45
commit 19eb1b91b5
3 changed files with 494 additions and 24 deletions

View file

@ -428,6 +428,23 @@ console.log("[INFINITE_SCROLL] Script loaded!");
window.aplicoopShop._attachEventListeners();
console.log("[INFINITE_SCROLL] Event listeners re-attached");
}
// Update realtime search to include newly loaded products
if (
window.realtimeSearch &&
typeof window.realtimeSearch._storeAllProducts === "function"
) {
window.realtimeSearch._storeAllProducts();
console.log(
"[INFINITE_SCROLL] Products list updated for realtime search"
);
// Apply current filters to newly loaded products
if (typeof window.realtimeSearch._filterProducts === "function") {
window.realtimeSearch._filterProducts();
console.log("[INFINITE_SCROLL] Filters applied to new products");
}
}
})
.catch(function (error) {
console.error("[INFINITE_SCROLL] Fetch error:", error);

View file

@ -609,11 +609,9 @@
var visibleCount = 0;
var hiddenCount = 0;
// Track tag counts for dynamic badge updates
var tagCounts = {};
for (var tagId in self.availableTags) {
tagCounts[tagId] = 0;
}
// NOTE: Tag counts are NOT updated dynamically here because with lazy loading,
// self.allProducts only contains products from current page.
// Tag counts must remain as provided by backend (calculated on full dataset).
self.allProducts.forEach(function (product) {
var nameMatches = !searchQuery || product.name.indexOf(searchQuery) !== -1;
@ -633,35 +631,20 @@
if (shouldShow) {
product.element.classList.remove("hidden-product");
visibleCount++;
// Count this product's tags toward the dynamic counters
product.tags.forEach(function (tagId) {
if (tagCounts.hasOwnProperty(tagId)) {
tagCounts[tagId]++;
}
});
} else {
product.element.classList.add("hidden-product");
hiddenCount++;
}
});
// Update badge counts dynamically
for (var tagId in tagCounts) {
var badge = document.querySelector('[data-tag-id="' + tagId + '"]');
if (badge) {
var countSpan = badge.querySelector(".tag-count");
if (countSpan) {
countSpan.textContent = tagCounts[tagId];
}
}
}
console.log(
"[realtimeSearch] Filter result: visible=" +
visibleCount +
" hidden=" +
hiddenCount
hiddenCount +
" (selectedTags: " +
Array.from(self.selectedTags).join(",") +
")"
);
} catch (error) {
console.error("[realtimeSearch] ERROR in _filterProducts():", error.message);