Node.js based forum software built for the modern web
refactor: if there are unread notifications open unread tab
Barış Soner Uşaklıbd21871c0d5e
diff --git a/public/src/client/header/notifications.js b/public/src/client/header/notifications.jsindex 7c0a73cfa9..0bf6dc6514 100644--- a/public/src/client/header/notifications.js+++ b/public/src/client/header/notifications.js@@ -6,13 +6,25 @@ define('forum/header/notifications', function () {notifications.prepareDOM = function () {const notifTrigger = $('[component="notifications"] [data-bs-toggle="dropdown"]');- notifTrigger.on('show.bs.dropdown', async (ev) => {+ function filterNotifications(notifList, filter) {+ notifList.find('[data-nid]').toggleClass('hidden', filter === 'unread')+ .filter('.unread').removeClass('hidden');+ }++ async function loadNotifications(triggerEl) {const notifications = await app.require('notifications');- const triggerEl = $(ev.target);const dropdownEl = triggerEl.parent().find('.dropdown-menu');- dropdownEl.find('[data-filter="all"]').addClass('active');- dropdownEl.find('[data-filter="unread"]').removeClass('active');- notifications.loadNotifications(triggerEl, triggerEl.parent().find('[component="notifications/list"]'));+ const listEl = triggerEl.parent().find('[component="notifications/list"]');+ notifications.loadNotifications(triggerEl, listEl, function (data) {+ const hasUnread = data.unread.length > 0;+ dropdownEl.find('[data-filter="all"]').toggleClass('active', !hasUnread);+ dropdownEl.find('[data-filter="unread"]').toggleClass('active', hasUnread);+ filterNotifications(listEl, hasUnread ? 'unread' : 'all');+ });+ }++ notifTrigger.on('show.bs.dropdown', (ev) => {+ loadNotifications($(ev.target));});notifTrigger.each((index, el) => {@@ -20,24 +32,15 @@ define('forum/header/notifications', function () {const dropdownEl = triggerEl.parent().find('.dropdown-menu');const listEl = dropdownEl.find('[component="notifications/list"]');if (dropdownEl.hasClass('show')) {- app.require('notifications').then((notifications) => {- notifications.loadNotifications(triggerEl, listEl);- });+ loadNotifications(triggerEl);}dropdownEl.on('click', '[data-filter]', (e) => {const filter = e.target.getAttribute('data-filter');dropdownEl.find('[data-filter]').removeClass('active');e.target.classList.add('active');- if (filter === 'unread') {- listEl.get(0).querySelectorAll('[data-nid]:not(.unread)').forEach((e) => {- e.classList.toggle('hidden', true);- });- } else {- listEl.get(0).querySelectorAll('[data-nid]').forEach((e) => {- e.classList.toggle('hidden', false);- });- }+ filterNotifications(listEl, filter);+const visibleNotifCount = dropdownEl.find('[data-nid]:not(.hidden)').length;dropdownEl.find('.no-notifs').toggleClass('hidden', visibleNotifCount !== 0);});diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.jsindex d0e346890f..74fb50a8ce 100644--- a/public/src/modules/notifications.js+++ b/public/src/modules/notifications.js@@ -83,7 +83,7 @@ define('notifications', [notifications: notifs,list: notifList,});- callback();+ callback({ unread: data.unread, read: data.read });});});}).catch(alerts.error);