From 0b39a45338a3422a63051424b63c6c289294d557 Mon Sep 17 00:00:00 2001 From: e-five <146029455+e-five256@users.noreply.github.com> Date: Sun, 5 May 2024 19:19:01 -0400 Subject: [PATCH] fix infinite scrolling on search pages --- src/Controller/SearchController.php | 26 ++++++++++++++++++++------ templates/search/_list.html.twig | 7 +++++++ templates/search/front.html.twig | 4 ++-- 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 templates/search/_list.html.twig diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index c7451b83e..6ea334cd6 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -14,6 +14,7 @@ use App\Service\SettingsManager; use App\Service\SubjectOverviewManager; use Psr\Log\LoggerInterface; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Messenger\MessageBusInterface; @@ -72,14 +73,27 @@ public function __invoke(Request $request): Response $user = $this->getUser(); $res = $this->manager->findPaginated($user, $query, $this->getPageNb($request)); + $params = [ + 'objects' => $objects, + 'results' => $this->overviewManager->buildList($res), + 'pagination' => $res, + 'q' => $request->query->get('q'), + ]; + + if ($request->isXmlHttpRequest()) { + return new JsonResponse( + [ + 'html' => $this->renderView( + 'search/_list.html.twig', + $params, + ), + ] + ); + } + return $this->render( 'search/front.html.twig', - [ - 'objects' => $objects, - 'results' => $this->overviewManager->buildList($res), - 'pagination' => $res, - 'q' => $request->query->get('q'), - ] + $params, ); } diff --git a/templates/search/_list.html.twig b/templates/search/_list.html.twig new file mode 100644 index 000000000..b864cbdf7 --- /dev/null +++ b/templates/search/_list.html.twig @@ -0,0 +1,7 @@ +
+ {% if objects|length %} + {% include 'layout/_subject_list.html.twig' with {results: [object], entryCommentAttributes: {showMagazineName: true, showEntryTitle: true}, postCommentAttributes: {withPost: false}} %} + {% elseif q %} + {% include 'layout/_subject_list.html.twig' with {entryCommentAttributes: {showMagazineName: true, showEntryTitle: true}, postCommentAttributes: {withPost: false}} %} + {% endif %} +
diff --git a/templates/search/front.html.twig b/templates/search/front.html.twig index feac7ebf2..3aeecabc6 100644 --- a/templates/search/front.html.twig +++ b/templates/search/front.html.twig @@ -40,11 +40,11 @@ {% elseif object.type is defined and object.type is same as 'magazine' %} {{ component('magazine_box', {magazine: object.object, stretchedLink: false}) }} {% else %} - {% include 'layout/_subject_list.html.twig' with {results: [object], entryCommentAttributes: {showMagazineName: true, showEntryTitle: true}, postCommentAttributes: {withPost: false}} %} + {% include 'search/_list.html.twig' %} {% endif %} {% endfor %} {% elseif q %} - {% include 'layout/_subject_list.html.twig' with {entryCommentAttributes: {showMagazineName: true, showEntryTitle: true}, postCommentAttributes: {withPost: false}} %} + {% include 'search/_list.html.twig' %} {% endif %} {% endblock %}