refactor: replace lifecycle hooks with useEffect and improve state initialization in OrderDisplay to prevent patch conflicts
This commit is contained in:
parent
c79145faa3
commit
43e908ce7d
@ -2,44 +2,47 @@
|
|||||||
|
|
||||||
import { OrderDisplay } from "@point_of_sale/app/components/order_display/order_display";
|
import { OrderDisplay } from "@point_of_sale/app/components/order_display/order_display";
|
||||||
import { patch } from "@web/core/utils/patch";
|
import { patch } from "@web/core/utils/patch";
|
||||||
import { useState, onMounted, onWillUnmount } from "@odoo/owl";
|
import { useState, useEffect } from "@odoo/owl";
|
||||||
|
|
||||||
patch(OrderDisplay.prototype, {
|
patch(OrderDisplay.prototype, {
|
||||||
setup() {
|
setup() {
|
||||||
super.setup();
|
super.setup();
|
||||||
this.state = useState({ displayedCount: 20 });
|
// Use Object.assign to avoid overwriting state from other patches (e.g. pos_urban_piper)
|
||||||
|
if (this.state) {
|
||||||
|
Object.assign(this.state, { displayedCount: 20 });
|
||||||
|
} else {
|
||||||
|
this.state = useState({ displayedCount: 20 });
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
// Use useEffect to handle dynamic mounting of the scrollable element
|
||||||
const scrollable = this.scrollableRef.el;
|
useEffect(
|
||||||
if (!scrollable) {
|
(scrollable) => {
|
||||||
return;
|
if (!scrollable) {
|
||||||
}
|
return;
|
||||||
|
|
||||||
this.onScroll = () => {
|
|
||||||
if (
|
|
||||||
scrollable.scrollTop + scrollable.clientHeight >=
|
|
||||||
scrollable.scrollHeight - 100
|
|
||||||
) {
|
|
||||||
if (this.state.displayedCount < this.comboSortedLines.length) {
|
|
||||||
this.state.displayedCount += 20;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
scrollable.addEventListener("scroll", this.onScroll);
|
|
||||||
|
|
||||||
// Ensure selected line is visible by expanding displayedCount if needed
|
const onScroll = () => {
|
||||||
const selectedLineIndex = this.comboSortedLines.findIndex(l => l.selected);
|
if (
|
||||||
if (selectedLineIndex >= this.state.displayedCount) {
|
scrollable.scrollTop + scrollable.clientHeight >=
|
||||||
this.state.displayedCount = selectedLineIndex + 1;
|
scrollable.scrollHeight - 100
|
||||||
}
|
) {
|
||||||
});
|
if (this.state.displayedCount < this.comboSortedLines.length) {
|
||||||
|
this.state.displayedCount += 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
scrollable.addEventListener("scroll", onScroll);
|
||||||
|
|
||||||
|
// Ensure selected line is visible by expanding displayedCount if needed
|
||||||
|
const selectedLineIndex = this.comboSortedLines.findIndex(l => l.selected);
|
||||||
|
if (selectedLineIndex >= this.state.displayedCount) {
|
||||||
|
this.state.displayedCount = selectedLineIndex + 1;
|
||||||
|
}
|
||||||
|
|
||||||
onWillUnmount(() => {
|
return () => scrollable.removeEventListener("scroll", onScroll);
|
||||||
const el = this.scrollableRef.el;
|
},
|
||||||
if (el) {
|
() => [this.scrollableRef?.el]
|
||||||
el.removeEventListener("scroll", this.onScroll);
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
get linesToDisplay() {
|
get linesToDisplay() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user