refactor: ensure safe socket listener management in ButtonGroup and AdminPage components to prevent errors when socketManager is not available

This commit is contained in:
sebseb7
2025-07-23 09:13:09 +02:00
parent a653908624
commit bfcc320e6d
2 changed files with 14 additions and 11 deletions

View File

@@ -60,17 +60,18 @@ class ButtonGroup extends Component {
}
addSocketListeners = () => {
// Remove existing listeners first to avoid duplicates
this.removeSocketListeners();
// Remove existing listeners first to avoid duplicates
this.removeSocketListeners();
if (window.socketManager) {
window.socketManager.on('cartUpdated', this.handleCartUpdated);
}
}
removeSocketListeners = () => {
if (window.socketManager) {
window.socketManager.off('cartUpdated', this.handleCartUpdated);
}
}
handleCartUpdated = (id,user,cart) => {

View File

@@ -142,14 +142,16 @@ class AdminPage extends React.Component {
addSocketListeners = () => {
// Remove existing listeners first to avoid duplicates
this.removeSocketListeners();
window.socketManager.on('cartUpdated', this.handleCartUpdated);
if (window.socketManager) {
window.socketManager.on('cartUpdated', this.handleCartUpdated);
}
}
removeSocketListeners = () => {
window.socketManager.off('cartUpdated', this.handleCartUpdated);
if (window.socketManager) {
window.socketManager.off('cartUpdated', this.handleCartUpdated);
}
}
formatPrice = (price) => {