Skip to content

Commit

Permalink
Add support for searching a QOTD
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Mar 22, 2023
1 parent 013a9fb commit 6c8bb9f
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 42 deletions.
9 changes: 9 additions & 0 deletions assets/controllers.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"controllers": {
"@symfony/ux-live-component": {
"live": {
"enabled": true,
"fetch": "eager",
"autoimport": {
"@symfony/ux-live-component/styles/live.css": true
}
}
},
"@symfony/ux-turbo": {
"turbo-core": {
"enabled": true,
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"symfony/security-bundle": "6.2.*",
"symfony/string": "6.2.*",
"symfony/twig-bundle": "6.2.*",
"symfony/ux-live-component": "^2.7",
"symfony/ux-turbo": "^2.7",
"symfony/ux-twig-component": "^2.7",
"symfony/web-link": "6.2.*",
"symfony/webpack-encore-bundle": "^1.16",
"symfony/yaml": "6.2.*",
Expand Down
165 changes: 164 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
Symfony\UX\Turbo\TurboBundle::class => ['all' => true],
Symfony\UX\TwigComponent\TwigComponentBundle::class => ['all' => true],
Symfony\UX\LiveComponent\LiveComponentBundle::class => ['all' => true],
];
5 changes: 5 additions & 0 deletions config/routes/ux_live_component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
live_component:
resource: '@LiveComponentBundle/config/routes.php'
prefix: '/_components'
# adjust prefix to add localization to your components
#prefix: '/{_locale}/_components'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@hotwired/stimulus": "^3.0.0",
"@hotwired/turbo": "^7.0.1",
"@symfony/stimulus-bridge": "^3.2.0",
"@symfony/ux-live-component": "file:vendor/symfony/ux-live-component/assets",
"@symfony/ux-turbo": "file:vendor/symfony/ux-turbo/assets",
"@symfony/webpack-encore": "^4.0.0",
"core-js": "^3.23.0",
Expand Down
6 changes: 6 additions & 0 deletions src/Controller/QotdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ public function vote(Request $request, Qotd $qotd, QotdVote $vote, #[CurrentUser

return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('qotd_index'));
}

#[Route('/search', name: 'qotd_search', methods: ['GET'])]
public function search(): Response
{
return $this->render('qotd/search.html.twig');
}
}
21 changes: 21 additions & 0 deletions src/Repository/QotdRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,25 @@ public function findForHomepageNotVoted(int $page, UserInterface $user): Paginat
20,
);
}

/**
* @return PaginationInterface<string, Qotd>
*/
public function search(string $query): PaginationInterface
{
$query = $this
->createQueryBuilder('q')
->where('q.message LIKE :query')->setParameter('query', "%{$query}%")
->addOrderBy('q.vote', 'DESC')
->addOrderBy('q.date', 'DESC')
->setMaxResults(20)
->getQuery()
;

return $this->paginator->paginate(
$query,
1,
20,
);
}
}
31 changes: 31 additions & 0 deletions src/Twig/Components/SearchComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Twig\Components;

use App\Repository\QotdRepository;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent('search')]
final class SearchComponent
{
use DefaultActionTrait;

#[LiveProp(writable: true)]
public ?string $query = '';

public function __construct(
private QotdRepository $qotdRepository,
) {
}

/**
* @return PaginationInterface<string, Qotd>
*/
public function getPagination(): PaginationInterface
{
return $this->qotdRepository->search($this->query);
}
}
15 changes: 15 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,24 @@
"templates/base.html.twig"
]
},
"symfony/ux-live-component": {
"version": "2.7",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "2.6",
"ref": "73e69baf18f47740d6f58688c5464b10cdacae06"
},
"files": [
"config/routes/ux_live_component.yaml"
]
},
"symfony/ux-turbo": {
"version": "v2.7.1"
},
"symfony/ux-twig-component": {
"version": "v2.7.1"
},
"symfony/web-profiler-bundle": {
"version": "6.2",
"recipe": {
Expand Down
3 changes: 3 additions & 0 deletions templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<li class="nav-item ">
<a class="nav-link {{ 'qotd_not_voted' == active ? 'active' }}" href="{{ url('qotd_index_not_voted') }}">Not voted yet</a>
</li>
<li class="nav-item ">
<a class="nav-link {{ 'qotd_search' == active ? 'active' }}" href="{{ url('qotd_search') }}">🔍Search</a>
</li>
</ul>
{# <ul class="navbar-nav mb-2 mb-lg-0 d-flex">
<li class="nav-item ">
Expand Down
10 changes: 10 additions & 0 deletions templates/components/search.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div{{ attributes }}>
<form>
<div class="mb-3">
<label for="query" class="form-label d-none">Search</label>
<input type="search" name="query" data-model="query" class="form-control" id="query">
</div>
</form>

{{ include("qotd/_qotds.html.twig", { pagination: this.pagination, with_pagination: false }, with_context = false) }}
</div>
48 changes: 48 additions & 0 deletions templates/qotd/_qotds.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% for qotd in pagination %}
<turbo-frame id="qotd-{{ qotd.id }}" data-delete-if-missing="true">
<div class="card mb-2">
<div class="card-body">
{{ qotd.message|replace_emoji|replace_username|markdown_to_html }}
</div>
<div class="card-footer text-muted">
<div class="d-flex justify-content-between">
<div class="me-auto">
{% cache qotd.id %}
{{ qotd.date|format_date() }} by {{ qotd.username }}
{% endcache %}
</div>
<div class="mx-1">
<a href="{{ qotd.permalink }}" class="btn btn-sm btn-outline-secondary">🔗 view in Slack</a>
</div>
<form action="{{ url(qotd.hasVotedDown(app.user) ? 'qotd_vote_null' : 'qotd_vote_down', { id: qotd.id }) }}" method="POST" class="mx-1">
<input type="hidden" name="page" value="{{ pagination.page }}">
<input type="hidden" name="token" value="{{ csrf_token('vote') }}">
<button type="submit" class="{{ html_classes('btn', 'btn-sm', { 'btn-outline-primary': qotd.hasVotedDown(app.user) }) }}">👎</button>
</form>
<div class="mx-1">
<span class="badge bg-primary">
{{ qotd.vote }}
</span>
</div>
<form action="{{ url(qotd.hasVotedUp(app.user) ? 'qotd_vote_null' : 'qotd_vote_up', { id: qotd.id }) }}" method="POST">
<input type="hidden" name="page" value="{{ pagination.page }}">
<input type="hidden" name="token" value="{{ csrf_token('vote') }}">
<button type="submit" class="{{ html_classes('btn', 'btn-sm', { 'btn-outline-primary': qotd.hasVotedUp(app.user) }) }}">👍</button>
</form>
</div>
</div>
</div>
</turbo-frame>
{% else %}
<div class="card mb-2">
<div class="card-body">
No QOTD found.
</div>
</div>
{% endfor %}

{% if with_pagination ?? true %}
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
{% endif %}
Loading

0 comments on commit 6c8bb9f

Please sign in to comment.