Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/app/core/pagination/pagination.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ export class PaginationService {
return `${paginationId}.page`;
}

/**
* Centralized fallback logic for pagination. Returns the correct page to use and updates the route if needed.
* @param paginationId - The pagination id
* @param currentPage - The current page number
* @param totalPages - The total number of pages
* @returns {number} - The corrected page number to use for the next data fetch
*/
public handlePaginationFallback(paginationId: string, currentPage: number, totalPages: number): number {
if (totalPages === 0 && currentPage !== 1) {
this.resetPage(paginationId);
return 1;
} else if (totalPages > 0 && currentPage > totalPages) {
this.updateRoute(paginationId, { page: totalPages });
return totalPages;
}
return currentPage;
}


private getCurrentRouting(paginationId: string) {
return this.getFindListOptions(paginationId, {}, true).pipe(
take(1),
Expand Down
22 changes: 22 additions & 0 deletions src/app/shared/pagination/pagination.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export class PaginationComponent implements OnChanges, OnDestroy, OnInit {
}));
this.checkConfig(this.paginationOptions);
this.initializeConfig();
this.checkAndHandleFallbackPage();
}

ngOnChanges(changes: SimpleChanges): void {
Expand Down Expand Up @@ -327,6 +328,7 @@ export class PaginationComponent implements OnChanges, OnDestroy, OnInit {
public doPageChange(page: number) {
this.updateParams({ page: page });
this.emitPaginationChange();
this.checkAndHandleFallbackPage();
}

/**
Expand All @@ -338,6 +340,7 @@ export class PaginationComponent implements OnChanges, OnDestroy, OnInit {
public doPageSizeChange(pageSize: number) {
this.updateParams({ page: 1, pageSize: pageSize });
this.emitPaginationChange();
this.checkAndHandleFallbackPage();
}

/**
Expand All @@ -349,6 +352,7 @@ export class PaginationComponent implements OnChanges, OnDestroy, OnInit {
public doSortDirectionChange(sortDirection: SortDirection) {
this.updateParams({ page: 1, sortDirection: sortDirection });
this.emitPaginationChange();
this.checkAndHandleFallbackPage();
}

/**
Expand Down Expand Up @@ -431,12 +435,29 @@ export class PaginationComponent implements OnChanges, OnDestroy, OnInit {
);
}

/**
* Check and handle fallback to the highest valid page if the current page is out of bounds.
*/
private checkAndHandleFallbackPage() {
this.paginationService.getCurrentPagination(this.id, this.paginationOptions).pipe(take(1)).subscribe((currentPaginationOptions) => {
const pageSize = currentPaginationOptions.pageSize;
const currentPage = currentPaginationOptions.currentPage;
const totalPages = Math.max(1, Math.ceil(this.collectionSize / pageSize));
const correctedPage = this.paginationService.handlePaginationFallback(this.id, currentPage, totalPages);
if (correctedPage !== currentPage) {
this.updateParams({ page: correctedPage });
this.emitPaginationChange();
}
});
}

/**
* Go to the previous page
*/
goPrev() {
this.prev.emit(true);
this.updatePagination(-1);
this.checkAndHandleFallbackPage();
}

/**
Expand All @@ -445,6 +466,7 @@ export class PaginationComponent implements OnChanges, OnDestroy, OnInit {
goNext() {
this.next.emit(true);
this.updatePagination(1);
this.checkAndHandleFallbackPage();
}

/**
Expand Down
Loading