From fc40da4e807b28d3848412bf7d9af5fa5d12f069 Mon Sep 17 00:00:00 2001
From: pfleu <fleutotp@gmail.com>
Date: Sun, 11 Jun 2023 23:11:51 +0200
Subject: [PATCH] =?UTF-8?q?Rechargement=20panneau=20notifications=20quand?=
 =?UTF-8?q?=20on=20marque=20une=20notif=20comme=20lue.=20Les=20notifs=20as?=
 =?UTF-8?q?soci=C3=A9es=20=C3=A0=20une=20invitation=20trait=C3=A9e=20passe?=
 =?UTF-8?q?=20au=20statut=20"lue"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 public/assets/js/app.js                   | 32 +++++++++++++++++++++++
 src/Controller/FriendController.php       |  3 ++-
 src/Controller/NotificationController.php |  9 ++++++-
 src/Entity/InvitationRequest.php          |  7 +++++
 src/Form/UserAddFriendType.php            |  4 +--
 templates/notifications.html.twig         |  2 +-
 templates/reloadNotifications.js.twig     |  1 +
 7 files changed, 53 insertions(+), 5 deletions(-)
 create mode 100644 templates/reloadNotifications.js.twig

diff --git a/public/assets/js/app.js b/public/assets/js/app.js
index c2637dd..d11aea0 100644
--- a/public/assets/js/app.js
+++ b/public/assets/js/app.js
@@ -27,6 +27,7 @@ $(function() {
 
     initializeForm();
     initializeAjaxLinks();
+    initializeAjaxAndRunResponse();
     initializeSearchHeadwords();
     // Pour améliorer en spécifiant la méthode (DELETE, POST...) Voir https://stackoverflow.com/a/8983053/4954580
     initializeFormToggles();
@@ -420,4 +421,35 @@ function initializeSearchHeadwords() {
             }
         })
     });
+}
+
+function initializeAjaxAndRunResponse() {
+
+    $('body').on('click', '.ajax-and-run', function () {
+        var $overlay = $('#overlay').show();
+        var method = $(this).data('method');
+
+        $.ajax({
+            type: 'GET',
+            url: $(this).data('url'),
+            dataType: "script"
+        });
+    });
+}
+
+function reloadNotifications() {
+    var $notifications = $('#box');
+    var $overlay = $('#overlay').show();
+    $.ajax({
+        type: "GET",
+        url: Routing.generate('app_notification_reload'),
+        cache: false,
+        success: function (resp) {
+            // console.log($(resp).find('#box'));
+            $notifications.html($(resp).find('#box').html());
+        },
+        complete: function () {
+            $overlay.hide();
+        }
+    });
 }
\ No newline at end of file
diff --git a/src/Controller/FriendController.php b/src/Controller/FriendController.php
index ca9ce3f..be5477a 100644
--- a/src/Controller/FriendController.php
+++ b/src/Controller/FriendController.php
@@ -124,6 +124,7 @@ class FriendController extends AbstractController
     public function denyFriendshipInvitationRequest(Request $request, InvitationRequest $invitationRequest): Response
     {
         $invitationRequest->setStatus(InvitationRequest::STATUS_DENIED);
+        $invitationRequest->markAllNotificationsAsRead();
         $notification = new Notification();
         $notification->setUser($invitationRequest->getSender());
         $notification->setContent("Invitation refusée");
@@ -141,7 +142,7 @@ class FriendController extends AbstractController
     public function acceptFriendshipInvitationRequest(Request $request, InvitationRequest $invitationRequest): Response
     {
         $invitationRequest->setStatus(InvitationRequest::STATUS_ACCEPTED);
-        $this->getUser()->addMyFriend($invitationRequest->getSender());
+        $invitationRequest->markAllNotificationsAsRead();
         $notification = new Notification();
         $notification->setUser($invitationRequest->getSender());
         $notification->setContent("Invitation acceptée");
diff --git a/src/Controller/NotificationController.php b/src/Controller/NotificationController.php
index 14c333f..d2301c2 100644
--- a/src/Controller/NotificationController.php
+++ b/src/Controller/NotificationController.php
@@ -38,6 +38,13 @@ class NotificationController extends AppBaseController
 
         $this->doctrine->getManager()->flush();
 
-        return $this->redirect($request->get('backUrl') ? : ($request->headers->get('referer')));
+        return $this->render('reloadNotifications.js.twig');
+    }
+    /**
+     * @Route("/reload-list", name="app_notification_reload", options={"expose" = true})
+     */
+    public function reloadList(Request $request): Response
+    {
+        return $this->render('base.html.twig');
     }
 }
diff --git a/src/Entity/InvitationRequest.php b/src/Entity/InvitationRequest.php
index 77020e8..8f0132d 100644
--- a/src/Entity/InvitationRequest.php
+++ b/src/Entity/InvitationRequest.php
@@ -82,6 +82,13 @@ class InvitationRequest
      */
     private $notifications;
 
+    public function markAllNotificationsAsRead()
+    {
+        foreach ($this->getNotifications() as $notification) {
+            $notification->setStatus(Notification::STATUS_READ);
+        }
+    }
+
     public function isAccepted()
     {
         return $this->getStatus() === self::STATUS_ACCEPTED;
diff --git a/src/Form/UserAddFriendType.php b/src/Form/UserAddFriendType.php
index 150e55e..51b4e94 100644
--- a/src/Form/UserAddFriendType.php
+++ b/src/Form/UserAddFriendType.php
@@ -31,10 +31,10 @@ class UserAddFriendType extends AbstractType
     public function buildForm(FormBuilderInterface $builder, array $options): void
     {
         $user = $this->security->getUser();
-        $hiddenUsers = [];
+        $hiddenUsers = ['4444'];
         if ($user) {
             $hiddenUsers[] = $user;
-            $hiddenUsers = array_merge($user->getMyFriends(), $user->getUsersFromPendingInvitationRequests());
+            $hiddenUsers = array_merge($hiddenUsers, $user->getMyFriends(), $user->getUsersFromPendingInvitationRequests());
         }
 
         $builder
diff --git a/templates/notifications.html.twig b/templates/notifications.html.twig
index adce8a4..c3f0d30 100644
--- a/templates/notifications.html.twig
+++ b/templates/notifications.html.twig
@@ -21,7 +21,7 @@
                     </div>
                 </a>
 
-                <a title="{{ "Marquer comme lu"|trans }}" href="{{ path('app_notification_mark_as_read', {id: notification.id}) }}"><i class="fa fa-envelope"></i></a>
+                <a title="{{ "Marquer comme lu"|trans }}" class="ajax-and-run" href="#" data-url="{{ path('app_notification_mark_as_read', {id: notification.id}) }}"><i class="fa fa-envelope"></i></a>
 
             </div>
         {% endfor %}
diff --git a/templates/reloadNotifications.js.twig b/templates/reloadNotifications.js.twig
new file mode 100644
index 0000000..acd6156
--- /dev/null
+++ b/templates/reloadNotifications.js.twig
@@ -0,0 +1 @@
+reloadNotifications();
-- 
GitLab