[#5352] Fix broken pagination if totalItems is missing (#5473)

* [#5352] Fix broken pagination if totalItems is missing. This happens when a search query is executed against Solr and not the DB

* fix lint issues

Co-authored-by: Arik Sidney Guggenheim <ariksidney@outlook.com>
This commit is contained in:
Eugenio Romano 2020-02-21 12:12:28 +00:00 committed by GitHub
parent b7de48c821
commit feebaebd6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 11 deletions

View File

@ -1,12 +1,7 @@
<ng-container *ngIf="hasItems">
<div class="adf-pagination__block adf-pagination__range-block">
<span class="adf-pagination__range">
{{
'CORE.PAGINATION.ITEMS_RANGE' | translate: {
range: range.join('-'),
total: pagination.totalItems
}
}}
{{ itemRangeText }}
</span>
</div>
@ -50,9 +45,11 @@
<mat-icon>arrow_drop_down</mat-icon>
</button>
<span class="adf-pagination__total-pages">
{{ 'CORE.PAGINATION.TOTAL_PAGES' | translate: { total: pages.length } }}
</span>
<div *ngIf="pagination.totalItems">
<span class="adf-pagination__total-pages">
{{ 'CORE.PAGINATION.TOTAL_PAGES' | translate: { total: pages.length } }}
</span>
</div>
<mat-menu #pagesMenu="matMenu" class="adf-pagination__page-selector">
<button

View File

@ -347,4 +347,27 @@ describe('PaginationComponent', () => {
});
});
describe('without total items', () => {
beforeEach(() => {
component.pagination = new FakePaginationInput(3, 2, 5);
component.pagination.hasMoreItems = true;
component.pagination.totalItems = undefined;
});
it('has the same, previous page', () => {
expect(component.previous).toBe(1);
});
it('has next page', () => {
expect(component.next).toBe(3);
});
it('has range', () => {
expect(component.range).toEqual([ 26, 50 ]);
});
it('cannot calculate number of pages', () => {
expect(component.pages).toEqual([1]);
});
});
});

View File

@ -27,6 +27,7 @@ import { Subject } from 'rxjs';
import { PaginationModel } from '../models/pagination.model';
import { UserPreferencesService, UserPreferenceValues } from '../services/user-preferences.service';
import { takeUntil } from 'rxjs/operators';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'adf-pagination',
@ -85,7 +86,7 @@ export class PaginationComponent implements OnInit, OnDestroy, PaginationCompone
private onDestroy$ = new Subject<boolean>();
constructor(private cdr: ChangeDetectorRef, private userPreferencesService: UserPreferencesService) {
constructor(private cdr: ChangeDetectorRef, private userPreferencesService: UserPreferencesService, private translate: TranslateService) {
}
ngOnInit() {
@ -133,6 +134,9 @@ export class PaginationComponent implements OnInit, OnDestroy, PaginationCompone
}
get isLastPage(): boolean {
if (!this.pagination.totalItems && this.pagination.hasMoreItems) {
return false;
}
return this.current === this.lastPage;
}
@ -161,7 +165,11 @@ export class PaginationComponent implements OnInit, OnDestroy, PaginationCompone
const { skipCount, maxItems, totalItems } = this.pagination;
const { isLastPage } = this;
const start = totalItems ? skipCount + 1 : 0;
let start = 0;
if (totalItems || totalItems !== 0) {
start = skipCount + 1;
}
const end = isLastPage ? totalItems : skipCount + maxItems;
return [start, end];
@ -173,6 +181,18 @@ export class PaginationComponent implements OnInit, OnDestroy, PaginationCompone
.map((_, index) => (index + 1));
}
get itemRangeText(): string {
const rangeString = this.range.join('-');
let translation = this.translate.instant('CORE.PAGINATION.ITEMS_RANGE', {
range: rangeString,
total: this.pagination.totalItems
});
if (!this.pagination.totalItems) {
translation = translation.substr(0, translation.indexOf(rangeString) + rangeString.length);
}
return translation;
}
goNext() {
if (this.hasItems) {
const maxItems = this.pagination.maxItems;