From 0aa3780c77a3cbd13f2a3f8a11aadf4118b400bd Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 24 Aug 2020 11:26:05 +0000 Subject: [PATCH 1/2] Set unread and fresh flags for notifications. #474 --- config/routes/notification.yaml | 5 ++++ public/assets/js/notification.js | 19 ++++++++++++++- src/Controller/NotificationController.php | 28 +++++++++++++++++++++++ templates/base.html.twig | 1 + 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/config/routes/notification.yaml b/config/routes/notification.yaml index 7bb58aa7..f3a15104 100644 --- a/config/routes/notification.yaml +++ b/config/routes/notification.yaml @@ -2,3 +2,8 @@ notification_ajax_list: path: /ajax/notifications controller: App\Controller\NotificationController::ajaxList methods: [GET] + +notification_ajax_update: + path: /ajax/notifications + controller: App\Controller\NotificationController::ajaxUpdate + methods: [POST] diff --git a/public/assets/js/notification.js b/public/assets/js/notification.js index bc1bfc40..1758ade3 100644 --- a/public/assets/js/notification.js +++ b/public/assets/js/notification.js @@ -15,6 +15,7 @@ class NotificationHandler { console.log('loading notifications'); // ajax load var self = this; + var notif_update_url = this.options['notif_ajax_update_url']; var xhr = new XMLHttpRequest(); xhr.open('GET', this.options['notif_ajax_url']); xhr.onload = function() { @@ -23,6 +24,7 @@ class NotificationHandler { var notifs = data.notifications; // update notification count + // TODO: notification count is total count or total unread count? var count_html = data.count; document.getElementById('notif-count').innerHTML = count_html; @@ -32,13 +34,15 @@ class NotificationHandler { // add actual notifications var notif_body = document.getElementById('notif-body'); + var notif_index = 0; notifs.forEach(function(notif) { var notif_date = moment(notif.date).fromNow(); var notif_html = '
'; notif_html += ''; notif_html += ''; - notif_html += '' + notif.text + ''; + //notif_html += '' + notif.text + ''; + notif_html += '' + notif.text + '' notif_html += ''; notif_html += ''; notif_html += notif_date; @@ -46,6 +50,18 @@ class NotificationHandler { notif_html += '
'; notif_body.insertAdjacentHTML('beforeend', notif_html); + + document.getElementsByClassName('m-list-timeline__item')[notif_index].addEventListener('click', function(e) { + e.preventDefault(); + $.ajax({ + method: "POST", + url: notif_update_url, + data: {id: notif.id} + }).done(function(response) { + window.location.href = notif.link; + }); + }); + notif_index++; }); } @@ -92,4 +108,5 @@ class NotificationHandler { return this.options['default_icon']; } + } diff --git a/src/Controller/NotificationController.php b/src/Controller/NotificationController.php index 54928528..6414ce55 100644 --- a/src/Controller/NotificationController.php +++ b/src/Controller/NotificationController.php @@ -56,11 +56,15 @@ class NotificationController extends AbstractController $notifs = $em->getRepository(Notification::class)->findBy(['user_id' => 0]); $notif_data = []; + // TODO: count how many notifs are unread and return the count + foreach ($notifs as $notif) { $notif_data[] = [ 'id' => $notif->getID(), 'type' => 'jo_new', + 'is_read' => $notif->isRead(), + 'is_fresh' => $notif->isFresh(), 'date' => $notif->getDateCreate()->format('Y-m-d\TH:i:s.000P'), 'text' => $notif->getMessage(), 'link' => $notif->getURL(), @@ -77,4 +81,28 @@ class NotificationController extends AbstractController return $res; } + + // TODO: security + public function ajaxUpdate(EntityManagerInterface $em, Request $req) + { + $notif_id = $req->request->get('id'); + error_log($notif_id); + + $notif = $em->getRepository(Notification::class)->find($notif_id); + + if ($notif != null) + { + // TODO: fresh is if unread and still within x hours + // but for now fresh and unread are both the same + $notif->setIsRead(true); + $notif->setIsFresh(false); + + $em->persist($notif); + $em->flush(); + } + + $res = new JsonResponse(); + + return $res; + } } diff --git a/templates/base.html.twig b/templates/base.html.twig index d8dbede9..37fcfc4d 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -612,6 +612,7 @@ // notifications var notif = new NotificationHandler({ 'notif_ajax_url': '{{ url('notification_ajax_list') }}', + 'notif_ajax_update_url': '{{ url('notification_ajax_update') }}', 'icons': { 'jo_new': 'flaticon-placeholder-3 kt-font-brand', 'jo_cancel': 'fa fa-times-circle kt-font-danger', From 08b4b25f4ea6e23aaa7aeee4b0b894f4a9129fcc Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 25 Aug 2020 07:21:24 +0000 Subject: [PATCH 2/2] Return count of unread notifications. #474 --- public/assets/js/notification.js | 7 +++---- src/Controller/NotificationController.php | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/public/assets/js/notification.js b/public/assets/js/notification.js index 1758ade3..9a4b169c 100644 --- a/public/assets/js/notification.js +++ b/public/assets/js/notification.js @@ -23,9 +23,8 @@ class NotificationHandler { var data = JSON.parse(xhr.responseText); var notifs = data.notifications; - // update notification count - // TODO: notification count is total count or total unread count? - var count_html = data.count; + // update notification unread count + var count_html = data.unread_count; document.getElementById('notif-count').innerHTML = count_html; // do we have any notifications? @@ -41,7 +40,6 @@ class NotificationHandler { var notif_html = '
'; notif_html += ''; notif_html += ''; - //notif_html += '' + notif.text + ''; notif_html += '' + notif.text + '' notif_html += ''; notif_html += ''; @@ -61,6 +59,7 @@ class NotificationHandler { window.location.href = notif.link; }); }); + notif_index++; }); } diff --git a/src/Controller/NotificationController.php b/src/Controller/NotificationController.php index 6414ce55..64150c51 100644 --- a/src/Controller/NotificationController.php +++ b/src/Controller/NotificationController.php @@ -56,10 +56,12 @@ class NotificationController extends AbstractController $notifs = $em->getRepository(Notification::class)->findBy(['user_id' => 0]); $notif_data = []; - // TODO: count how many notifs are unread and return the count - + $unread_count = 0; foreach ($notifs as $notif) { + if (!($notif->isRead())) + $unread_count++; + $notif_data[] = [ 'id' => $notif->getID(), 'type' => 'jo_new', @@ -74,6 +76,7 @@ class NotificationController extends AbstractController $sample_data = [ 'count' => count($notif_data), + 'unread_count' => $unread_count, 'notifications' => $notif_data, ];