diff --git a/src/OrchardCore.Modules/OrchardCore.Notifications/Assets/js/notification-manager.js b/src/OrchardCore.Modules/OrchardCore.Notifications/Assets/js/notification-manager.js index 5268195a05e..d48ae0b4e5b 100644 --- a/src/OrchardCore.Modules/OrchardCore.Notifications/Assets/js/notification-manager.js +++ b/src/OrchardCore.Modules/OrchardCore.Notifications/Assets/js/notification-manager.js @@ -12,7 +12,7 @@ notificationManager = function () { return false; } - const initialize = (readUrl, wrapperSelector) => { + const initialize = (readUrl, unreadBadgeSelector, wrapperSelector) => { if (!readUrl) { console.log('No readUrl was provided.'); @@ -21,6 +21,16 @@ notificationManager = function () { } const reading = []; + let totalUnread = 0; + let unreadElement = null; + + if (unreadBadgeSelector) { + unreadElement = document.querySelector(unreadBadgeSelector); + + if (unreadElement) { + totalUnread = parseInt(unreadElement.innerText) + } + } var elements = document.getElementsByClassName('mark-notification-as-read'); @@ -55,6 +65,15 @@ notificationManager = function () { }).then(response => response.json()) .then(result => { if (result.updated) { + + if (unreadElement) { + if (totalUnread > 1) { + unreadElement.innerText = --totalUnread; + } else { + unreadElement.style.display = 'none'; + } + } + if (wrapperSelector) { var wrapper = e.target.closest(wrapperSelector); if (wrapper) { diff --git a/src/OrchardCore.Modules/OrchardCore.Notifications/Drivers/NotificationNavbarDisplayDriver.cs b/src/OrchardCore.Modules/OrchardCore.Notifications/Drivers/NotificationNavbarDisplayDriver.cs index 867869e9090..ed8b46e07fb 100644 --- a/src/OrchardCore.Modules/OrchardCore.Notifications/Drivers/NotificationNavbarDisplayDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Notifications/Drivers/NotificationNavbarDisplayDriver.cs @@ -42,14 +42,17 @@ public override async Task DisplayAsync(Navbar model, BuildDispl .Processing(async model => { var userId = _httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); + var notifications = (await _session.Query(x => x.UserId == userId && !x.IsRead, collection: NotificationConstants.NotificationCollection) .OrderByDescending(x => x.CreatedAtUtc) - .Take(_notificationOptions.TotalUnreadNotifications + 1) + .Take(_notificationOptions.TotalUnreadNotifications) .ListAsync()).ToList(); model.Notifications = notifications; model.MaxVisibleNotifications = _notificationOptions.TotalUnreadNotifications; - model.TotalUnread = notifications.Count; + model.TotalUnread = notifications.Count < _notificationOptions.TotalUnreadNotifications + ? notifications.Count + : await _session.QueryIndex(x => x.UserId == userId && !x.IsRead, collection: NotificationConstants.NotificationCollection).CountAsync(); }).Location("Detail", "Content:9") .Location("DetailAdmin", "Content:9"); diff --git a/src/OrchardCore.Modules/OrchardCore.Notifications/Views/UserNotificationNavbar.cshtml b/src/OrchardCore.Modules/OrchardCore.Notifications/Views/UserNotificationNavbar.cshtml index 03cd74a9d81..91d588eb8db 100644 --- a/src/OrchardCore.Modules/OrchardCore.Notifications/Views/UserNotificationNavbar.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.Notifications/Views/UserNotificationNavbar.cshtml @@ -26,6 +26,8 @@ } + + @Model.TotalUnread } else { @@ -70,11 +72,15 @@