diff --git a/public/assets/js/app.js b/public/assets/js/app.js index c2637dd8eff39930689f6e33e2285696dffd7d1f..d11aea04f81c33e3ac2837d8eddda0147bd33ed5 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 ca9ce3fec0d645f5af39564c5d1dda63fb0e5e16..be5477a89528926595e61a46bc9129e1e657d91e 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 14c333ff98c0c2a20b2592049856dcf03c603696..d2301c237eae4bc02c8da57c4e45a4ee2249ca4f 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 77020e843ced75c7a5c6c4a6a07bf299b2c2b1dd..8f0132d07565017089602d25a517b761e5f54954 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 150e55e66e3c790e26252683da6b8e1e47189bf8..51b4e9451d1f46dfa17ad8291839a3577f1ed450 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 adce8a43537acbcda827d8d5526ff2ecfe0f7533..c3f0d30deb2d20f12e74066d3ab99d8313b06843 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 0000000000000000000000000000000000000000..acd615660dbfd90c6f759b6ab1506207e1b1fe8e --- /dev/null +++ b/templates/reloadNotifications.js.twig @@ -0,0 +1 @@ +reloadNotifications();