Skip to content

Commit

Permalink
Create notifications.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent a4f67d7 commit 4d89159
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/ui/notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// notifications.js

class Notifications {
constructor() {
this.notifications = [];
this.init();
}

// Initialize the notification system
init() {
this.render();
}

// Add a new notification
addNotification(message) {
const notification = {
id: this.notifications.length + 1,
message,
timestamp: new Date(),
};
this.notifications.push(notification);
this.renderNotification(notification);
}

// Render the notification area
render() {
const notificationContainer = document.createElement('div');
notificationContainer.id = 'notifications';
notificationContainer.innerHTML = `
<h2>Notifications</h2>
<ul id="notification-list"></ul>
`;
document.body.appendChild(notificationContainer);
}

// Render a single notification
renderNotification(notification) {
const notificationList = document.getElementById('notification-list');
const notificationElement = document.createElement('li');
notificationElement.innerText = `${notification.timestamp.toLocaleTimeString()}: ${notification.message}`;
notificationList.appendChild(notificationElement);
}
}

// Example usage
const notifications = new Notifications();
notifications.addNotification("Transaction of $200 completed.");
notifications.addNotification("New alert: Your account balance is low.");

export default Notifications;

0 comments on commit 4d89159

Please sign in to comment.