fix: Add null checks before attaching/detaching scroll event listeners and refactor product list display count reset logic.

This commit is contained in:
Suherdy Yacob 2026-03-17 16:40:08 +07:00
parent 04af9538f2
commit 0fa1972fe0
2 changed files with 24 additions and 20 deletions

View File

@ -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);
}
});
},

View File

@ -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);
}
});