Set unread and fresh flags for notifications. #474
This commit is contained in:
parent
e4ad0169d7
commit
0aa3780c77
4 changed files with 52 additions and 1 deletions
|
|
@ -2,3 +2,8 @@ notification_ajax_list:
|
||||||
path: /ajax/notifications
|
path: /ajax/notifications
|
||||||
controller: App\Controller\NotificationController::ajaxList
|
controller: App\Controller\NotificationController::ajaxList
|
||||||
methods: [GET]
|
methods: [GET]
|
||||||
|
|
||||||
|
notification_ajax_update:
|
||||||
|
path: /ajax/notifications
|
||||||
|
controller: App\Controller\NotificationController::ajaxUpdate
|
||||||
|
methods: [POST]
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ class NotificationHandler {
|
||||||
console.log('loading notifications');
|
console.log('loading notifications');
|
||||||
// ajax load
|
// ajax load
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var notif_update_url = this.options['notif_ajax_update_url'];
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', this.options['notif_ajax_url']);
|
xhr.open('GET', this.options['notif_ajax_url']);
|
||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
|
|
@ -23,6 +24,7 @@ class NotificationHandler {
|
||||||
var notifs = data.notifications;
|
var notifs = data.notifications;
|
||||||
|
|
||||||
// update notification count
|
// update notification count
|
||||||
|
// TODO: notification count is total count or total unread count?
|
||||||
var count_html = data.count;
|
var count_html = data.count;
|
||||||
document.getElementById('notif-count').innerHTML = count_html;
|
document.getElementById('notif-count').innerHTML = count_html;
|
||||||
|
|
||||||
|
|
@ -32,13 +34,15 @@ class NotificationHandler {
|
||||||
|
|
||||||
// add actual notifications
|
// add actual notifications
|
||||||
var notif_body = document.getElementById('notif-body');
|
var notif_body = document.getElementById('notif-body');
|
||||||
|
var notif_index = 0;
|
||||||
notifs.forEach(function(notif) {
|
notifs.forEach(function(notif) {
|
||||||
var notif_date = moment(notif.date).fromNow();
|
var notif_date = moment(notif.date).fromNow();
|
||||||
|
|
||||||
var notif_html = '<div class="m-list-timeline__item">';
|
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__badge -m-list-timeline__badge--state-success"></span>';
|
||||||
notif_html += '<span class="m-list-timeline__text">';
|
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>';
|
||||||
notif_html += '<span class="m-list-timeline__time">';
|
notif_html += '<span class="m-list-timeline__time">';
|
||||||
notif_html += notif_date;
|
notif_html += notif_date;
|
||||||
|
|
@ -46,6 +50,18 @@ class NotificationHandler {
|
||||||
notif_html += '</div>';
|
notif_html += '</div>';
|
||||||
|
|
||||||
notif_body.insertAdjacentHTML('beforeend', 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'];
|
return this.options['default_icon'];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,11 +56,15 @@ class NotificationController extends AbstractController
|
||||||
$notifs = $em->getRepository(Notification::class)->findBy(['user_id' => 0]);
|
$notifs = $em->getRepository(Notification::class)->findBy(['user_id' => 0]);
|
||||||
$notif_data = [];
|
$notif_data = [];
|
||||||
|
|
||||||
|
// TODO: count how many notifs are unread and return the count
|
||||||
|
|
||||||
foreach ($notifs as $notif)
|
foreach ($notifs as $notif)
|
||||||
{
|
{
|
||||||
$notif_data[] = [
|
$notif_data[] = [
|
||||||
'id' => $notif->getID(),
|
'id' => $notif->getID(),
|
||||||
'type' => 'jo_new',
|
'type' => 'jo_new',
|
||||||
|
'is_read' => $notif->isRead(),
|
||||||
|
'is_fresh' => $notif->isFresh(),
|
||||||
'date' => $notif->getDateCreate()->format('Y-m-d\TH:i:s.000P'),
|
'date' => $notif->getDateCreate()->format('Y-m-d\TH:i:s.000P'),
|
||||||
'text' => $notif->getMessage(),
|
'text' => $notif->getMessage(),
|
||||||
'link' => $notif->getURL(),
|
'link' => $notif->getURL(),
|
||||||
|
|
@ -77,4 +81,28 @@ class NotificationController extends AbstractController
|
||||||
|
|
||||||
return $res;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -612,6 +612,7 @@
|
||||||
// notifications
|
// notifications
|
||||||
var notif = new NotificationHandler({
|
var notif = new NotificationHandler({
|
||||||
'notif_ajax_url': '{{ url('notification_ajax_list') }}',
|
'notif_ajax_url': '{{ url('notification_ajax_list') }}',
|
||||||
|
'notif_ajax_update_url': '{{ url('notification_ajax_update') }}',
|
||||||
'icons': {
|
'icons': {
|
||||||
'jo_new': 'flaticon-placeholder-3 kt-font-brand',
|
'jo_new': 'flaticon-placeholder-3 kt-font-brand',
|
||||||
'jo_cancel': 'fa fa-times-circle kt-font-danger',
|
'jo_cancel': 'fa fa-times-circle kt-font-danger',
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue