Skip to content

Commit

Permalink
refactor: Simplify notification handling in settings base component
Browse files Browse the repository at this point in the history
- Extract common notification creation logic into a private `_createNotification` method
- Refactor `handleNotification` to use the new private method
- Improve code reusability and reduce duplication in notification management
- Maintain existing notification behavior and timeout mechanism
  • Loading branch information
tphakala committed Feb 26, 2025
1 parent ffdf395 commit 4b9a991
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions views/pages/settings/settingsBase.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,7 @@
}
this.handleNotification = (notification) => {
const id = Date.now() + Math.random();
this.notifications.push({
id: id,
message: notification.message,
type: notification.type,
removing: false
});
setTimeout(() => {
const index = this.notifications.findIndex(n => n.id === id);
if (index !== -1) {
this.notifications[index].removing = true;
setTimeout(() => {
this.notifications = this.notifications.filter(n => n.id !== id);
}, 300);
}
}, 5000);
this._createNotification(notification);
};
window.SSEManager.subscribe(this.handleNotification);
Expand Down Expand Up @@ -139,14 +123,16 @@
}
return visibleFieldsValid;
},
addNotification(message, type = 'info') {
_createNotification(notification) {
const id = Date.now() + Math.random();
this.notifications.push({
id: id,
message: message,
type: type,
const notificationObj = {
id: id,
message: notification.message,
type: notification.type || 'info',
removing: false
});
};
this.notifications.push(notificationObj);
setTimeout(() => {
const index = this.notifications.findIndex(n => n.id === id);
Expand All @@ -158,6 +144,9 @@
}
}, 5000);
},
addNotification(message, type = 'info') {
this._createNotification({ message, type });
},
saveSettings() {
const form = document.getElementById('settingsForm');
const formData = new FormData(form);
Expand Down

0 comments on commit 4b9a991

Please sign in to comment.