Skip to content

Commit

Permalink
fix(ProximityPager): fix bug when going to next page from second to…
Browse files Browse the repository at this point in the history
… last page (#37)
  • Loading branch information
priyadi authored Apr 5, 2024
1 parent 3ccd12e commit 798ca05
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* build: update babel config according to symfony docs
* fix(`bootstrap5.html.twig`): fix label_unknown bug
* fix(`ProximityPager`): fix bug when going to next page from second to last
page

# 0.7.0

Expand Down
28 changes: 28 additions & 0 deletions packages/rekapager-core/src/Pager/Internal/ProximityPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,24 @@ public function __construct(
}
}

// if current page is last page & page number is not set, then
// set page number to -1, and renumber the previous pages

if ($currentIsLastPage && $currentPage->getPageNumber() === null) {
$this->currentPage = $this->currentPage->withPageNumber(-1);

$currentPageNumber = -1;
$previousNeigboringPages = [];

foreach (array_reverse($this->previousNeighboringPages) as $page) {
$currentPageNumber--;
$page = $page->withPageNumber($currentPageNumber);
$previousNeigboringPages[] = $page;
}

$this->previousNeighboringPages = array_reverse($previousNeigboringPages);
}

// if no gap to last page, set the last page number to the last + 1

if ($hasGapToLastPage === false && $this->lastPage !== null) {
Expand All @@ -319,6 +337,16 @@ public function __construct(
}
}
}

// if next page is last page, then replace the next page with last
// page

if (
\count($this->nextNeighboringPages) === 0
&& $this->nextPage !== null
) {
$this->nextPage = $this->lastPage;
}
}

public function withProximity(int $proximity): static
Expand Down
40 changes: 40 additions & 0 deletions tests/src/IntegrationTests/Pager/KeysetPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,44 @@ public function testSixthLastPage(string $pageableGeneratorClass): void
currentCount: 5,
);
}

#[DataProviderExternal(PageableGeneratorProvider::class, 'keyset')]
public function testNextFromSecondLastPage(string $pageableGeneratorClass): void
{
$pageable = $this->createPageableFromGenerator($pageableGeneratorClass);

$lastPage = $pageable->getLastPage();
self::assertNotNull($lastPage);

$lastPagePager = $this->createPagerFromPage($lastPage);

$secondLastPage = $lastPagePager->getPreviousPage();
self::assertNotNull($secondLastPage);

$secondLastPagePager = $this->createPagerFromPage($secondLastPage);

$nextFromSecondLastPage = $secondLastPagePager->getNextPage();
self::assertNotNull($nextFromSecondLastPage);

$pager = $this->createPagerFromPage($nextFromSecondLastPage);

$this->assertPager(
$pager,
proximity: 2,
hasPrevious: true,
hasNext: false,
hasFirst: true,
hasLast: false,
hasGapToFirstPage: true,
hasGapToLastPage: false,
numOfPreviousNeighboringPages: 4,
numOfNextNeighboringPages: 0,
firstPageNumber: 1,
lastPageNumber: null,
currentPageNumber: -1,
previousPageNumbers: [-5, -4, -3, -2],
nextPageNumbers: [],
currentCount: 5,
);
}
}

0 comments on commit 798ca05

Please sign in to comment.