From 0fa1972fe0650f5e3a0544a8bdb15b13ad2e2ebe Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Tue, 17 Mar 2026 16:40:08 +0700 Subject: [PATCH] fix: Add null checks before attaching/detaching scroll event listeners and refactor product list display count reset logic. --- .../order_widget/order_widget_patch.js | 5 ++- .../product_list/product_list_patch.js | 39 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/static/src/app/generic_components/order_widget/order_widget_patch.js b/static/src/app/generic_components/order_widget/order_widget_patch.js index 57c2184..54f070e 100644 --- a/static/src/app/generic_components/order_widget/order_widget_patch.js +++ b/static/src/app/generic_components/order_widget/order_widget_patch.js @@ -33,7 +33,10 @@ patch(OrderWidget.prototype, { }); onWillUnmount(() => { - this.scrollableRef.el?.removeEventListener("scroll", this.onScroll); + const el = this.scrollableRef.el; + if (el) { + el.removeEventListener("scroll", this.onScroll); + } }); }, diff --git a/static/src/app/screens/product_screen/product_list/product_list_patch.js b/static/src/app/screens/product_screen/product_list/product_list_patch.js index c6b63f1..9878df4 100644 --- a/static/src/app/screens/product_screen/product_list/product_list_patch.js +++ b/static/src/app/screens/product_screen/product_list/product_list_patch.js @@ -24,35 +24,36 @@ patch(ProductsWidget.prototype, { } } }; - this.scrollContainer?.addEventListener("scroll", this.onScroll); + if (this.scrollContainer) { + this.scrollContainer.addEventListener("scroll", this.onScroll); + } }); onWillUnmount(() => { - this.scrollContainer?.removeEventListener("scroll", this.onScroll); + if (this.scrollContainer) { + this.scrollContainer.removeEventListener("scroll", this.onScroll); + } }); }, - get selectedCategoryId() { - if (this._lastCategoryId !== this.pos.selectedCategoryId) { - this._lastCategoryId = this.pos.selectedCategoryId; - this.state.displayedCount = 40; - } - return super.selectedCategoryId; - }, - - get searchWord() { - const word = this.pos.searchProductWord.trim(); - if (this._lastSearchWord !== word) { - this._lastSearchWord = word; - this.state.displayedCount = 40; - } - return super.searchWord; - }, - get productsToDisplay() { const list = super.productsToDisplay; // The core productsToDisplay returns the full list. // We slice it here for rendering. + + // React to category or search word changes to reset the count + const currentCategory = this.selectedCategoryId; + if (this._lastCategoryId !== currentCategory) { + this._lastCategoryId = currentCategory; + this.state.displayedCount = 40; + } + + const currentSearch = this.searchWord; + if (this._lastSearchWord !== currentSearch) { + this._lastSearchWord = currentSearch; + this.state.displayedCount = 40; + } + return list.slice(0, this.state.displayedCount); } });