Skip to content

Commit

Permalink
Add pagination with PagerFanta (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianaromagnoli committed Mar 16, 2020
1 parent af3c271 commit 44690e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"symfony/property-info": "^4.3",
"doctrine/annotations": "^1.8",
"ramsey/uuid": "^3.8",
"webgriffe/amp-elasticsearch": "dev-master"
"webgriffe/amp-elasticsearch": "dev-master",
"pagerfanta/pagerfanta": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 8 additions & 1 deletion src/Console/Controller/FlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Amp\Beanstalk\Stats\System;
use Amp\Beanstalk\Stats\Tube;
use Amp\Http\Server\Request;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use Twig\Environment;
use Webgriffe\AmpElasticsearch\Client;
use function Amp\call;
Expand All @@ -27,15 +29,20 @@ public function __invoke(Request $request, string $flowCode): Promise
$queryParams = [];
parse_str($request->getUri()->getQuery(), $queryParams);
$query = $queryParams['query'] ?? '';
$page = (int)($queryParams['page'] ?? '1');
$foundJobs = yield $this->findAllTubeJobsByQuery($flowCode, $query);
$adapter = new ArrayAdapter($foundJobs);
$pager = new Pagerfanta($adapter);
$pager->setMaxPerPage(5);
$pager->setCurrentPage($page);
return new Response(
Status::OK,
[],
$this->getTwig()->render(
'flow.html.twig',
[
'flowCode' => $flowCode,
'foundJobs' => $foundJobs,
'pager' => $pager,
'query' => $query,
]
)
Expand Down
20 changes: 18 additions & 2 deletions src/Console/Resources/views/flow.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</div>
</form>

{% if foundJobs|length > 0 %}
{% if pager.getCurrentPageResults|length > 0 %}
<div class="table-responsive">
<table class="table table-striped">
<thead>
Expand All @@ -31,7 +31,7 @@
</tr>
</thead>
<tbody>
{% for job in foundJobs %}
{% for job in pager.getCurrentPageResults %}
<tr>
<th scope="row">{{ job.uuid }}</th>
{% set payload = job.payloadData|json_encode() %}
Expand All @@ -51,4 +51,20 @@
</table>
</div>
{% endif %}

{% if pager.haveToPaginate %}
<nav aria-label="Search results pages">
<ul class="pagination">
{% if pager.hasPreviousPage %}
<li class="page-item"><a class="page-link" href="/flow/{{ flowCode }}?page={{ pager.previousPage }}">&laquo;</a></li>
{% endif %}
{% for page in 1..pager.nbPages %}
<li class="page-item{% if pager.currentPage == page %} active{% endif %}"><a class="page-link" href="/flow/{{ flowCode }}?page={{ page }}">{{ page }}</a></li>
{% endfor %}
{% if pager.hasNextPage %}
<li class="page-item"><a class="page-link" href="/flow/{{ flowCode }}?page={{ pager.nextPage }}">&raquo;</a></li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endblock %}

0 comments on commit 44690e9

Please sign in to comment.