Skip to content

Commit 9af7e78

Browse files
authored
Merge pull request #52 from EasyAbp/lau/ui-fix
Fix toolbar badge count management to avoid stale server cache
2 parents e8da61f + e021839 commit 9af7e78

1 file changed

Lines changed: 18 additions & 25 deletions

File tree

  • src/EasyAbp.ProcessManagement.Web/Components/NotificationsOffcanvasWidget

src/EasyAbp.ProcessManagement.Web/Components/NotificationsOffcanvasWidget/Default.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
}
4242
});
4343
refreshBaseUiElements();
44-
setToolbarBadgeCount(res.totalCount);
44+
refreshToolbarWidget();
4545
}).catch(function () {
4646
// Silently ignore network errors for background notification fetch
4747
});
@@ -106,7 +106,7 @@
106106
});
107107
newAlertNode.addEventListener('closed.bs.alert', function () {
108108
refreshBaseUiElements();
109-
updateToolbarBadge(-1);
109+
updateToolbarBadgeCount(Math.max(0, getToolbarBadgeCount() - 1));
110110
});
111111
}
112112

@@ -145,8 +145,8 @@
145145
refreshBaseUiElements();
146146
}
147147

148-
// Update the toolbar badge count
149-
updateToolbarBadge(1);
148+
// Update the toolbar badge count directly to avoid stale server cache
149+
updateToolbarBadgeCount(getToolbarBadgeCount() + 1);
150150
});
151151

152152
connection.onreconnected(function () {
@@ -185,28 +185,18 @@
185185
}
186186
}
187187

188-
function setToolbarBadgeCount(count) {
189-
var newCount = Math.min(99, Math.max(0, count));
190-
for (const randomId of (window.toolbarNotificationsWidgetAreaRandomIds || [])) {
191-
var $wrapper = $('#ToolbarNotificationsWidgetArea-' + randomId);
192-
var $link = $wrapper.find('.notifications-toolbar-item');
193-
if ($link.length) {
194-
$link.html('<i class="fas fa-bell"></i> ' + newCount);
195-
}
196-
}
188+
function updateToolbarBadgeCount(count) {
189+
$('.notifications-toolbar-item').each(function () {
190+
var $icon = $(this).find('i');
191+
$(this).text(' ' + count).prepend($icon);
192+
});
197193
}
198194

199-
function updateToolbarBadge(delta) {
200-
for (const randomId of (window.toolbarNotificationsWidgetAreaRandomIds || [])) {
201-
var $wrapper = $('#ToolbarNotificationsWidgetArea-' + randomId);
202-
var $link = $wrapper.find('.notifications-toolbar-item');
203-
if ($link.length) {
204-
var currentText = $link.text().trim();
205-
var currentCount = parseInt(currentText) || 0;
206-
setToolbarBadgeCount(currentCount + delta);
207-
break;
208-
}
209-
}
195+
function getToolbarBadgeCount() {
196+
var $first = $('.notifications-toolbar-item').first();
197+
if (!$first.length) return 0;
198+
var text = $first.text().trim();
199+
return parseInt(text) || 0;
210200
}
211201

212202
function refreshToolbarWidget() {
@@ -293,11 +283,14 @@
293283
maxCreationTime: maxNotificationTime
294284
};
295285
easyAbp.processManagement.notifications.notification.dismiss(dismissOpts).then(function () {
286+
var dismissedCount = existingAlertIds.size;
296287
existingAlertIds.forEach(function (alert, id) {
297288
removeAlert(alert)
298289
});
299290
if (dismiss === 'DismissAll') {
300-
setToolbarBadgeCount(0);
291+
updateToolbarBadgeCount(0);
292+
} else {
293+
updateToolbarBadgeCount(Math.max(0, getToolbarBadgeCount() - dismissedCount));
301294
}
302295
});
303296
}

0 commit comments

Comments
 (0)