From 7edc8f458decd4cf1c23d321f34796a0cdfe4b1d Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 8 Feb 2021 15:25:10 -0500 Subject: [PATCH] feat: new notifications load/loaded hooks on client side refactored `toggleTimeagoShorthand` code to be inside of a hook listener --- public/src/modules/notifications.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js index 0a9b2fee64..d299c4cb6b 100644 --- a/public/src/modules/notifications.js +++ b/public/src/modules/notifications.js @@ -7,11 +7,23 @@ define('notifications', [ 'navigator', 'benchpress', 'tinycon', -], function (translator, components, navigator, Benchpress, Tinycon) { + 'hooks', +], function (translator, components, navigator, Benchpress, Tinycon, hooks) { var Notifications = {}; var unreadNotifs = {}; + const _addShortTimeagoString = ({ notifications: notifs }) => new Promise((resolve) => { + translator.toggleTimeagoShorthand(function () { + for (var i = 0; i < notifs.length; i += 1) { + notifs[i].timeago = $.timeago(new Date(parseInt(notifs[i].datetime, 10))); + } + translator.toggleTimeagoShorthand(); + resolve({ notifications: notifs }); + }); + }); + hooks.on('filter:notifications.load', _addShortTimeagoString); + Notifications.loadNotifications = function (notifList) { socket.emit('notifications.get', null, function (err, data) { if (err) { @@ -22,12 +34,8 @@ define('notifications', [ return parseInt(a.datetime, 10) > parseInt(b.datetime, 10) ? -1 : 1; }); - translator.toggleTimeagoShorthand(function () { - for (var i = 0; i < notifs.length; i += 1) { - notifs[i].timeago = $.timeago(new Date(parseInt(notifs[i].datetime, 10))); - } - translator.toggleTimeagoShorthand(); - app.parseAndTranslate('partials/notifications_list', { notifications: notifs }, function (html) { + hooks.fire('filter:notifications.load', { notifications: notifs }).then(({ notifications }) => { + app.parseAndTranslate('partials/notifications_list', { notifications }, function (html) { notifList.html(html); notifList.off('click').on('click', '[data-nid]', function (ev) { var notifEl = $(this); @@ -55,6 +63,11 @@ define('notifications', [ }); return false; }); + + hooks.fire('action:notifications.loaded', { + notifications: notifs, + list: notifList.get(0), + }); }); }); });