From 3a57c484a2ede160855a6ee48248a551df6b9411 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 10 Jul 2020 09:54:47 +0000 Subject: [PATCH] Mark notifications as read when user views the JO. User is redirected to the JO when clicking the View JO action. #433 --- config/routes/notification.yaml | 6 ++++ src/Controller/NotificationController.php | 29 ++++++++++++++++ templates/notification/list.html.twig | 41 +++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/config/routes/notification.yaml b/config/routes/notification.yaml index b61f6a64..86023d64 100644 --- a/config/routes/notification.yaml +++ b/config/routes/notification.yaml @@ -6,3 +6,9 @@ notification_rows: path: /notifications/rows controller: App\Controller\NotificationController::rows methods: [POST] + +# ajax call +notification_ajax_read: + path: /notifications/read + controller: App\Controller\NotificationController::markNotificationRead + methods: [POST] diff --git a/src/Controller/NotificationController.php b/src/Controller/NotificationController.php index 4b55916b..75ea2812 100644 --- a/src/Controller/NotificationController.php +++ b/src/Controller/NotificationController.php @@ -5,6 +5,8 @@ namespace App\Controller; use App\Entity\Notification; use Doctrine\ORM\Query; +use Doctrine\ORM\EntityManagerInterface; + use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -87,6 +89,11 @@ class NotificationController extends Controller $row['id'] = $orow->getID(); $row['message'] = $orow->getMessage(); $row['notif_type'] = $orow->getNotificationTypeName(); + $row['jo_id'] = $orow->getJobOrder()->getID(); + $row['flag_read'] = $orow->isNotificationRead(); + + // add row metadata + $row['meta']['view_jo_url'] = $this->generateUrl('jo_walkin_edit_form', ['id' => $row['jo_id']]); $rows[] = $row; } @@ -97,4 +104,26 @@ class NotificationController extends Controller 'data' => $rows ]); } + + public function markNotificationRead(EntityManagerInterface $em, Request $req) + { + $id = $req->request->get('id'); + + $notif = $em->getRepository(Notification::class)->find($id); + // make sure this notification exists + if (empty($notif)) { + return $this->json([ + 'success' => false, + 'errors' => 'No notification found.', + ], 422); + } + + $notif->setReadNotification(); + + $em->flush(); + + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } } diff --git a/templates/notification/list.html.twig b/templates/notification/list.html.twig index 6c4d8455..4840ec43 100644 --- a/templates/notification/list.html.twig +++ b/templates/notification/list.html.twig @@ -54,6 +54,17 @@ serverFiltering: true, serverSorting: true }, + rows: { + beforeTemplate: function(row, data, index) { + console.log(data.flag_read); + if (data.flag_read) { + $(row).addClass('m-table__row--primary'); + } + else { + $(row).addClass('m-table__row--danger'); + } + } + }, columns: [ { field: 'id', @@ -68,10 +79,40 @@ field: 'notif_type', title: 'Notification Type' }, + { + field: 'Actions', + width: 110, + title: 'Actions', + sortable: false, + overflow: 'visible', + template: function (row, index, datatable) { + var actions = ''; + + if (row.meta.view_jo_url != '') { + actions += ''; + } + return actions; + }, + } ] }; var table = $("#data-rows").mDatatable(options); + + $(document).on('click', '.btn-edit', function(e) { + var url = $(this).prop('href'); + var id = $(this).data('id'); + console.log(id); + $.ajax({ + method: 'POST', + url: "{{ url('notification_ajax_read') }}", + data: { id: id } + }).done(function(response) { + console.log(response); + }).error(function(err) { + console.log(err); + }); + }); }); {% endblock %}