Skip to content

Commit

Permalink
bug #1138 Fix search query (jkufner)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch.

Discussion
----------

Fix search query

When visiting `http://localhost:8000/cs/blog/search?q=de` directly, the search is not triggered automatically. This PR triggers the search on page load and passes the provided query argument to the input field.

Commits
-------

de60ace Fix search query
  • Loading branch information
javiereguiluz committed Jul 31, 2020
2 parents e1dffb2 + de60ace commit 1c1ff08
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
8 changes: 5 additions & 3 deletions assets/js/search.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import './jquery.instantSearch.js';

$(function() {
$('.search-field').instantSearch({
delay: 100,
});
$('.search-field')
.instantSearch({
delay: 100,
})
.keyup();
});
2 changes: 1 addition & 1 deletion public/build/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
"/build/login.js": "sha384-w5qwGnj2mp4pazKe9KGCQU+FR1n/717WouCp594UxY7HHGLafcGrZ9mrFR68V6oe",
"/build/admin.js": "sha384-nicrzvXIMGjWH5/pdimaGfVfn2LB/i2RxpKGp5XiWyspyQj6VmrfxlybhN5W+ksw",
"/build/admin.css": "sha384-ilLLAgcQxfeIVl6jV3AsZ2jmrWwgECQptl9HYwqc2NpYuTl6FEW46DBpDk5Q76sp",
"/build/search.js": "sha384-psGgj2E6N5ueWFmL86ZDUGX8QJNJM9Uoy//YLzvlW8/x6v1zn7zsgMIFVFEjgsC2"
"/build/search.js": "sha384-mmhQLZ7DRLI71dJilhS384iek0VyZaGARlhbLwwNgjsBtMlzb58H1LZO0MWe5w+b"
}
}
2 changes: 1 addition & 1 deletion public/build/search.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,13 @@ public function commentForm(Post $post): Response
*/
public function search(Request $request, PostRepository $posts): Response
{
$query = $request->query->get('q', '');
$limit = $request->query->get('l', 10);

if (!$request->isXmlHttpRequest()) {
return $this->render('blog/search.html.twig');
return $this->render('blog/search.html.twig', ['query' => $query]);
}

$query = $request->query->get('q', '');
$limit = $request->query->get('l', 10);
$foundPosts = $posts->findBySearchQuery($query, $limit);

$results = [];
Expand Down
1 change: 1 addition & 0 deletions templates/blog/search.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class="form-control search-field"
placeholder="{{ 'post.search_for'|trans }}"
autocomplete="off"
value="{{ query }}"
autofocus
data-no-results-message="{{ 'post.search_no_results'|trans }}"
>
Expand Down

0 comments on commit 1c1ff08

Please sign in to comment.