Skip to content

Commit

Permalink
fixed system ajax load more comment
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-claireau committed Feb 16, 2020
1 parent 40b921c commit 983f962
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
4 changes: 4 additions & 0 deletions assets/css/app.scss
Expand Up @@ -22,6 +22,10 @@ h1 {
}
}

.hide {
display: none;
}

.lead {
font-size: 1.75rem;

Expand Down
9 changes: 5 additions & 4 deletions src/Controller/FigureController.php
Expand Up @@ -46,8 +46,8 @@ public function __construct(CommentRepository $commentRepository, FiguresReposit
public function show(Figures $figure, string $slug, Request $request): Response
{
$user = $this->getUser();
$comments = $this->commentRepository->findItems();
$nbGroups = round($this->commentRepository->countAll() / 10);
$comments = $this->commentRepository->findItems(1, $figure->getId());
$nbGroups = round($this->commentRepository->countAll($figure->getId()) / 10);

if ($user) {
$comment = new Comment();
Expand Down Expand Up @@ -102,10 +102,11 @@ public function ajaxLoadItems(Request $request)
{
$params = $request->attributes->get('_route_params');
$index = (int) $params['index'];
$nbGroups = round($this->commentRepository->countAll() / 10);
$idFigure = $params['id'];
$nbGroups = round($this->commentRepository->countAll($idFigure) / 10);

if (is_int($index) && $index > 1) {
$moreComments = (array) $this->commentRepository->findMoreItems($index);
$moreComments = (array) $this->commentRepository->findMoreItems($index, $idFigure);
$htmlData = [];

if ($moreComments) {
Expand Down
20 changes: 13 additions & 7 deletions src/Repository/CommentRepository.php
Expand Up @@ -30,32 +30,34 @@ public function resetIndex()
/**
* @return Comment[]
*/
public function findItems(int $index = 1): array
public function findItems(int $index = 1, $idFigure): array
{
return $this->getQueryDesc($index)
return $this->getQueryDesc($index, $idFigure)
->getQuery()
->getResult();
}

public function findMoreItems(int $index): array
public function findMoreItems(int $index, $idFigure): array
{
if ($index !== 1) {
return $this->getQueryDesc($index)
return $this->getQueryDesc($index, $idFigure)
->getQuery()
->getResult(Query::HYDRATE_ARRAY);
}
}

public function countAll()
public function countAll($idFigure)
{
return intval($this->createQueryBuilder('p')
->select('COUNT(p)')
->where('p.figure = :id_figure')
->setParameter('id_figure', $idFigure)
->getQuery()->getSingleScalarResult());
}

private function getQueryDesc(int $index)
private function getQueryDesc(int $index, $idFigure)
{
$total = $this->countAll();
$total = $this->countAll($idFigure);
$nbResultsPerPage = 10;


Expand All @@ -66,6 +68,8 @@ private function getQueryDesc(int $index)
if (!$total || !$nbGroups) {
return $this->createQueryBuilder('p')
->orderBy('p.created_at', 'DESC')
->where('p.figure = :id_figure')
->setParameter('id_figure', $idFigure)
->setMaxResults($nbResultsPerPage);
}

Expand All @@ -81,6 +85,8 @@ private function getQueryDesc(int $index)

return $this->createQueryBuilder('p')
->orderBy('p.created_at', 'DESC')
->where('p.figure = :id_figure')
->setParameter('id_figure', $idFigure)
->setFirstResult($interval['start'])
->setMaxResults($nbResultsPerPage);
}
Expand Down
2 changes: 1 addition & 1 deletion templates/figure/show.html.twig
Expand Up @@ -95,7 +95,7 @@
</div>
{% endif %}
</div>
<div class="comments-container">
<div class="comments-container {{ comments|length == 0 ? 'hide' : ''}}">
<h2>
Commentaires
</h2>
Expand Down

0 comments on commit 983f962

Please sign in to comment.