dynamically load notifications

This commit is contained in:
Matthias 2021-11-28 02:39:36 +01:00
parent d7bc16209d
commit d3ea23665b
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
2 changed files with 53 additions and 2 deletions

View File

@ -220,11 +220,13 @@ function inventreeDocReady() {
// Start notification background worker to check every 5 seconds if notifications are available // Start notification background worker to check every 5 seconds if notifications are available
setInterval(notificationCheck, 5000); setInterval(notificationCheck, 5000);
// also run when the focus returns // also run when the focus returns
$(document).on('focus', function(e){ $(document).on('focus', function(e){
notificationCheck(); notificationCheck();
}); });
$('#offcanvasRight').on('show.bs.offcanvas', openNotificationPanel) // listener for opening the notification panel
$('#offcanvasRight').on('hidden.bs.offcanvas', closeNotificationPanel) // listener for closing the notification panel
} }

View File

@ -144,3 +144,52 @@ function showMessage(message, options={}) {
); );
} }
} }
function openNotificationPanel() {
var html = '';
inventreeGet(
'/api/notifications/',
{
read: false,
},
{
success: function(response) {
if (response.length == 0) {
html = `<p class='text-muted'>{% trans "No unread notifications" %}</p>`;
} else {
// build up items
response.forEach(function(item, index) {
html += '<li class="list-group-item">';
// d-flex justify-content-between align-items-start
html += '<div>';
html += `<span class="badge rounded-pill bg-primary">${item.category}</span><span class="me-1">${item.name}</span>`;
html += '</div>';
if (item.target) {
var link_text = `${item.target.model}: ${item.target.name}`;
if (item.target.link) {
link_text = `<a href='${item.target.link}'>${link_text}</a>`;
}
html += link_text
}
html += '<div>';
html += `<span class="text-muted">${item.age_human}</span>`;
html += "</div></li>";
});
// package up
html = `<ul class="list-group">${html}</ul>`
}
// set html
$('#notification-center').html(html);
}
}
);
}
function closeNotificationPanel() {
$('#notification-center').html(`<p class='text-muted'>{% trans "Notifications will load here" %}</p>`);
}