refactor: ensure safe socket listener management in ButtonGroup and AdminPage components to prevent errors when socketManager is not available
This commit is contained in:
@@ -60,17 +60,18 @@ class ButtonGroup extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addSocketListeners = () => {
|
addSocketListeners = () => {
|
||||||
|
// Remove existing listeners first to avoid duplicates
|
||||||
// Remove existing listeners first to avoid duplicates
|
this.removeSocketListeners();
|
||||||
this.removeSocketListeners();
|
|
||||||
|
if (window.socketManager) {
|
||||||
window.socketManager.on('cartUpdated', this.handleCartUpdated);
|
window.socketManager.on('cartUpdated', this.handleCartUpdated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeSocketListeners = () => {
|
removeSocketListeners = () => {
|
||||||
|
if (window.socketManager) {
|
||||||
window.socketManager.off('cartUpdated', this.handleCartUpdated);
|
window.socketManager.off('cartUpdated', this.handleCartUpdated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleCartUpdated = (id,user,cart) => {
|
handleCartUpdated = (id,user,cart) => {
|
||||||
|
|||||||
@@ -142,14 +142,16 @@ class AdminPage extends React.Component {
|
|||||||
addSocketListeners = () => {
|
addSocketListeners = () => {
|
||||||
// Remove existing listeners first to avoid duplicates
|
// Remove existing listeners first to avoid duplicates
|
||||||
this.removeSocketListeners();
|
this.removeSocketListeners();
|
||||||
window.socketManager.on('cartUpdated', this.handleCartUpdated);
|
|
||||||
|
if (window.socketManager) {
|
||||||
|
window.socketManager.on('cartUpdated', this.handleCartUpdated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeSocketListeners = () => {
|
removeSocketListeners = () => {
|
||||||
|
if (window.socketManager) {
|
||||||
window.socketManager.off('cartUpdated', this.handleCartUpdated);
|
window.socketManager.off('cartUpdated', this.handleCartUpdated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
formatPrice = (price) => {
|
formatPrice = (price) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user