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 = () => {
|
||||
|
||||
// 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) => {
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user