diff --git a/website_sale_aplicoop/static/src/js/realtime_search.js b/website_sale_aplicoop/static/src/js/realtime_search.js index ef0079f..2eef78c 100644 --- a/website_sale_aplicoop/static/src/js/realtime_search.js +++ b/website_sale_aplicoop/static/src/js/realtime_search.js @@ -646,11 +646,55 @@ Array.from(self.selectedTags).join(",") + ")" ); + + // Auto-load more products if too few are visible + self._autoLoadMoreIfNeeded(visibleCount); } catch (error) { console.error("[realtimeSearch] ERROR in _filterProducts():", error.message); console.error("[realtimeSearch] Stack:", error.stack); } }, + + _autoLoadMoreIfNeeded: function (visibleCount) { + /** + * Automatically load more products if there are too few visible on screen. + * This prevents empty screens when filters hide most products. + * + * @param {number} visibleCount - Number of currently visible products + */ + var self = this; + var minVisibleProducts = 10; // Minimum products before auto-loading more + + // Only auto-load if infinite scroll is available and has more pages + if ( + !window.infiniteScroll || + typeof window.infiniteScroll.loadNextPage !== "function" + ) { + return; + } + + // Check if we need more products + if (visibleCount < minVisibleProducts && window.infiniteScroll.hasMore) { + console.log( + "[realtimeSearch] Only " + + visibleCount + + " products visible (min: " + + minVisibleProducts + + "), auto-loading next page..." + ); + + // Delay slightly to avoid race conditions + setTimeout(function () { + if ( + window.infiniteScroll && + !window.infiniteScroll.isLoading && + window.infiniteScroll.hasMore + ) { + window.infiniteScroll.loadNextPage(); + } + }, 100); + } + }, }; // Initialize when DOM is ready