Set unread and fresh flags for notifications. #474

This commit is contained in:
Korina Cordero 2020-08-24 11:26:05 +00:00
parent e4ad0169d7
commit 0aa3780c77
4 changed files with 52 additions and 1 deletions

View file

@ -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]

View file

@ -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 = '<div class="m-list-timeline__item">';
notif_html += '<span class="m-list-timeline__badge -m-list-timeline__badge--state-success"></span>';
notif_html += '<span class="m-list-timeline__text">';
notif_html += '<a href="' + notif.link + '">' + notif.text + '</a>';
//notif_html += '<a href="' + notif.link + '">' + notif.text + '</a>';
notif_html += '<a href="">' + notif.text + '</a>'
notif_html += '</span>';
notif_html += '<span class="m-list-timeline__time">';
notif_html += notif_date;
@ -46,6 +50,18 @@ class NotificationHandler {
notif_html += '</div>';
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'];
}
}

View file

@ -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;
}
}

View file

@ -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',