Skip to content

Commit

Permalink
fix: renumbering of pages if anchored to the first page. (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jun 10, 2024
1 parent b4d1eab commit 23d9b2c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 0.9.3

* fix: renumbering of pages if anchored to the first page.

# 0.9.2

* build: limit `zenstruck/foundry` to 1.37.* for now
Expand Down
52 changes: 28 additions & 24 deletions packages/rekapager-core/src/Pager/Internal/ProximityPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,30 +231,6 @@ public function __construct(

$this->nextPage = $this->nextNeighboringPages[0] ?? $this->lastPage;

// renumber pages if there is no gap from first page to the current page

if ($this->hasHiddenPagesBefore === false) {
$currentPageNumber = 1;

if ($this->firstPage !== null) {
$this->firstPage = $this->firstPage->withPageNumber($currentPageNumber);
$currentPageNumber++;
}

foreach ($this->previousNeighboringPages as $page) {
$page = $page->withPageNumber($currentPageNumber);
$currentPageNumber++;
}

$this->currentPage = $this->currentPage->withPageNumber($currentPageNumber);
$currentPageNumber++;

foreach ($this->nextNeighboringPages as $page) {
$page = $page->withPageNumber($currentPageNumber);
$currentPageNumber++;
}
}

// if we have page number larger than the last page, then the count
// must be wrong

Expand Down Expand Up @@ -363,6 +339,34 @@ public function __construct(
) {
// $this->nextPage = $this->lastPage;
}

// renumber pages if there is no gap from first page to the current page

if ($this->hasHiddenPagesBefore === false) {
$currentPageNumber = 1;

if ($this->firstPage !== null) {
$this->firstPage = $this->firstPage->withPageNumber($currentPageNumber);
$currentPageNumber++;
}

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

$this->currentPage = $this->currentPage->withPageNumber($currentPageNumber);
$currentPageNumber++;

$newNextNeighboringPages = [];
foreach ($this->nextNeighboringPages as $page) {
$newNextNeighboringPages[] = $page->withPageNumber($currentPageNumber);
$currentPageNumber++;
}
$this->nextNeighboringPages = $newNextNeighboringPages;
}
}

public function withProximity(int $proximity): static
Expand Down
2 changes: 1 addition & 1 deletion packages/rekapager-core/src/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function getPager(): PagerInterface
public function getCurrentPage(): PagerItemInterface
{
return new PagerItem(
$this->page,
$this->getPager()->getCurrentPage(),
$this->pagerUrlGenerator
);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/src/IntegrationTests/Pager/KeysetPagerTraversalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,40 @@ public function testLastPageFromSecondFromLastPage(string $pageableGeneratorClas
static::assertEquals(BoundaryType::Lower, $nextPageIdentifier->getBoundaryType());
static::assertCount(3, $nextPage);
}

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

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

foreach (range(1, 19) as $i) {
$previousPage = $pager->getPreviousPage();
self::assertNotNull($previousPage);

$pager = $this->createPagerFromPage($previousPage);
}

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

0 comments on commit 23d9b2c

Please sign in to comment.