Skip to content

Commit c1f6993

Browse files
authored
Merge pull request DSpace#1777 from atmire/w2p-93665_fix-pagintion-in-metadata-browse-links
Fix pagination in metadata browse links
2 parents 9a5a7c1 + 317c615 commit c1f6993

6 files changed

Lines changed: 87 additions & 19 deletions

File tree

src/app/browse-by/browse-by-metadata-page/browse-by-metadata-page.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { BrowseByDataType, rendersBrowseBy } from '../browse-by-switcher/browse-
1818
import { PaginationService } from '../../core/pagination/pagination.service';
1919
import { map } from 'rxjs/operators';
2020

21+
export const BBM_PAGINATION_ID = 'bbm';
22+
2123
@Component({
2224
selector: 'ds-browse-by-metadata-page',
2325
styleUrls: ['./browse-by-metadata-page.component.scss'],
@@ -50,7 +52,7 @@ export class BrowseByMetadataPageComponent implements OnInit {
5052
* The pagination config used to display the values
5153
*/
5254
paginationConfig: PaginationComponentOptions = Object.assign(new PaginationComponentOptions(), {
53-
id: 'bbm',
55+
id: BBM_PAGINATION_ID,
5456
currentPage: 1,
5557
pageSize: 20
5658
});

src/app/shared/browse-by/browse-by.component.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,12 @@ describe('BrowseByComponent', () => {
240240
});
241241
describe('back', () => {
242242
it('should navigate back to the main browse page', () => {
243+
const id = 'test-pagination';
243244
comp.back();
244-
expect(paginationService.updateRoute).toHaveBeenCalledWith('test-pagination', {page: 1}, {
245+
expect(paginationService.updateRoute).toHaveBeenCalledWith(id, {page: 1}, {
245246
value: null,
246-
startsWith: null
247+
startsWith: null,
248+
[id + '.return']: null
247249
});
248250
});
249251
});

src/app/shared/browse-by/browse-by.component.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Component, EventEmitter, Injector, Input, OnInit, Output } from '@angular/core';
1+
import { Component, EventEmitter, Injector, Input, OnDestroy, OnInit, Output } from '@angular/core';
22
import { RemoteData } from '../../core/data/remote-data';
33
import { PaginatedList } from '../../core/data/paginated-list.model';
44
import { PaginationComponentOptions } from '../pagination/pagination-component-options.model';
55
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
66
import { fadeIn, fadeInOut } from '../animations/fade';
7-
import { combineLatest as observableCombineLatest, Observable } from 'rxjs';
7+
import { BehaviorSubject, combineLatest as observableCombineLatest, Observable, Subscription } from 'rxjs';
88
import { ListableObject } from '../object-collection/shared/listable-object.model';
99
import { getStartsWithComponent, StartsWithType } from '../starts-with/starts-with-decorator';
1010
import { PaginationService } from '../../core/pagination/pagination.service';
@@ -25,7 +25,7 @@ import { hasValue } from '../empty.util';
2525
/**
2626
* Component to display a browse-by page for any ListableObject
2727
*/
28-
export class BrowseByComponent implements OnInit {
28+
export class BrowseByComponent implements OnInit, OnDestroy {
2929

3030
/**
3131
* ViewMode that should be passed to {@link ListableObjectComponentLoaderComponent}.
@@ -112,6 +112,16 @@ export class BrowseByComponent implements OnInit {
112112
*/
113113
shouldDisplayResetButton$: Observable<boolean>;
114114

115+
/**
116+
* Page number of the previous page
117+
*/
118+
previousPage$ = new BehaviorSubject<string>('1');
119+
120+
/**
121+
* Subscription that has to be unsubscribed from on destroy
122+
*/
123+
sub: Subscription;
124+
115125
public constructor(private injector: Injector,
116126
protected paginationService: PaginationService,
117127
private routeService: RouteService,
@@ -171,9 +181,20 @@ export class BrowseByComponent implements OnInit {
171181
this.shouldDisplayResetButton$ = observableCombineLatest([startsWith$, value$]).pipe(
172182
map(([startsWith, value]) => hasValue(startsWith) || hasValue(value))
173183
);
184+
this.sub = this.routeService.getQueryParameterValue(this.paginationConfig.id + '.return').subscribe(this.previousPage$);
174185
}
175186

187+
/**
188+
* Navigate back to the previous browse by page
189+
*/
176190
back() {
177-
this.paginationService.updateRoute(this.paginationConfig.id, {page: 1}, {value: null, startsWith: null});
191+
const page = +this.previousPage$.value > 1 ? +this.previousPage$.value : 1;
192+
this.paginationService.updateRoute(this.paginationConfig.id, {page: page}, {[this.paginationConfig.id + '.return']: null, value: null, startsWith: null});
193+
}
194+
195+
ngOnDestroy(): void {
196+
if (this.sub) {
197+
this.sub.unsubscribe();
198+
}
178199
}
179200
}

src/app/shared/object-list/browse-entry-list-element/browse-entry-list-element.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="d-flex flex-row">
2-
<a *ngIf="linkType != linkTypes.None" [target]="(linkType == linkTypes.ExternalLink) ? '_blank' : '_self'" rel="noopener noreferrer" [routerLink]="[]" [queryParams]="getQueryParams()" [queryParamsHandling]="'merge'" class="lead">
2+
<a *ngIf="linkType != linkTypes.None" [target]="(linkType == linkTypes.ExternalLink) ? '_blank' : '_self'" rel="noopener noreferrer" [routerLink]="[]" [queryParams]="queryParams$ | async" [queryParamsHandling]="'merge'" class="lead">
33
{{object.value}}
44
</a>
55
<span *ngIf="linkType == linkTypes.None" class="lead">

src/app/shared/object-list/browse-entry-list-element/browse-entry-list-element.component.spec.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { By } from '@angular/platform-browser';
44
import { TruncatePipe } from '../../utils/truncate.pipe';
55
import { BrowseEntryListElementComponent } from './browse-entry-list-element.component';
66
import { BrowseEntry } from '../../../core/shared/browse-entry.model';
7-
7+
import { PaginationService } from '../../../core/pagination/pagination.service';
8+
import { RouteService } from '../../../core/services/route.service';
9+
import { of as observableOf } from 'rxjs';
810
let browseEntryListElementComponent: BrowseEntryListElementComponent;
911
let fixture: ComponentFixture<BrowseEntryListElementComponent>;
1012

@@ -13,12 +15,28 @@ const mockValue: BrowseEntry = Object.assign(new BrowseEntry(), {
1315
value: 'De Langhe Kristof'
1416
});
1517

16-
describe('MetadataListElementComponent', () => {
18+
let paginationService;
19+
let routeService;
20+
const pageParam = 'bbm.page';
21+
22+
function init() {
23+
paginationService = jasmine.createSpyObj('paginationService', {
24+
getPageParam: pageParam
25+
});
26+
27+
routeService = jasmine.createSpyObj('routeService', {
28+
getQueryParameterValue: observableOf('1')
29+
});
30+
}
31+
describe('BrowseEntryListElementComponent', () => {
1732
beforeEach(waitForAsync(() => {
33+
init();
1834
TestBed.configureTestingModule({
1935
declarations: [BrowseEntryListElementComponent, TruncatePipe],
2036
providers: [
21-
{ provide: 'objectElementProvider', useValue: { mockValue } }
37+
{ provide: 'objectElementProvider', useValue: { mockValue } },
38+
{provide: PaginationService, useValue: paginationService},
39+
{provide: RouteService, useValue: routeService},
2240
],
2341

2442
schemas: [NO_ERRORS_SCHEMA]
Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22

33
import { AbstractListableElementComponent } from '../../object-collection/shared/object-collection-element/abstract-listable-element.component';
44
import { BrowseEntry } from '../../../core/shared/browse-entry.model';
55
import { ViewMode } from '../../../core/shared/view-mode.model';
66
import { listableObjectComponent } from '../../object-collection/shared/listable-object/listable-object.decorator';
7+
import { PaginationService } from '../../../core/pagination/pagination.service';
8+
import { Params } from '@angular/router';
9+
import { BBM_PAGINATION_ID } from '../../../browse-by/browse-by-metadata-page/browse-by-metadata-page.component';
10+
import { RouteService } from 'src/app/core/services/route.service';
11+
import { Observable } from 'rxjs';
12+
import { map } from 'rxjs/operators';
713

814
@Component({
915
selector: 'ds-browse-entry-list-element',
@@ -15,16 +21,35 @@ import { listableObjectComponent } from '../../object-collection/shared/listable
1521
* This component is automatically used to create a list view for BrowseEntry objects when used in ObjectCollectionComponent
1622
*/
1723
@listableObjectComponent(BrowseEntry, ViewMode.ListElement)
18-
export class BrowseEntryListElementComponent extends AbstractListableElementComponent<BrowseEntry> {
24+
export class BrowseEntryListElementComponent extends AbstractListableElementComponent<BrowseEntry> implements OnInit {
25+
/**
26+
* Emits the query parameters for the link of this browse entry list element
27+
*/
28+
queryParams$: Observable<Params>;
29+
30+
constructor(private paginationService: PaginationService, private routeService: RouteService) {
31+
super();
32+
}
33+
34+
ngOnInit() {
35+
this.queryParams$ = this.getQueryParams();
36+
}
1937

2038
/**
2139
* Get the query params to access the item page of this browse entry.
2240
*/
23-
public getQueryParams(): {[param: string]: any} {
24-
return {
25-
value: this.object.value,
26-
authority: !!this.object.authority ? this.object.authority : undefined,
27-
startsWith: undefined,
28-
};
41+
private getQueryParams(): Observable<Params> {
42+
const pageParamName = this.paginationService.getPageParam(BBM_PAGINATION_ID);
43+
return this.routeService.getQueryParameterValue(pageParamName).pipe(
44+
map((currentPage) => {
45+
return {
46+
value: this.object.value,
47+
authority: !!this.object.authority ? this.object.authority : undefined,
48+
startsWith: undefined,
49+
[pageParamName]: null,
50+
[BBM_PAGINATION_ID + '.return']: currentPage
51+
};
52+
})
53+
);
2954
}
3055
}

0 commit comments

Comments
 (0)