Skip to content

Commit 33fdee5

Browse files
jensvannerumKoen PauwelsJens Vannerum
authored
[Port dspace-7_x] Get rid of unnecessary and failing REST requests when navigating between different browse indexes (#3788)
Co-authored-by: Koen Pauwels <[email protected]> Co-authored-by: Jens Vannerum <[email protected]>
1 parent d578222 commit 33fdee5

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { ChangeDetectorRef, Component, Inject } from '@angular/core';
22
import {
33
BrowseByMetadataPageComponent,
4-
browseParamsToOptions,
5-
getBrowseSearchOptions
4+
browseParamsToOptions
65
} from '../browse-by-metadata-page/browse-by-metadata-page.component';
76
import { combineLatest as observableCombineLatest, Observable } from 'rxjs';
87
import { hasValue, isNotEmpty } from '../../shared/empty.util';
@@ -11,7 +10,7 @@ import { BrowseService } from '../../core/browse/browse.service';
1110
import { DSpaceObjectDataService } from '../../core/data/dspace-object-data.service';
1211
import { StartsWithType } from '../../shared/starts-with/starts-with-decorator';
1312
import { PaginationService } from '../../core/pagination/pagination.service';
14-
import { map } from 'rxjs/operators';
13+
import { map, take } from 'rxjs/operators';
1514
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model';
1615
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
1716
import { isValidDate } from '../../shared/date.util';
@@ -52,15 +51,16 @@ export class BrowseByDatePageComponent extends BrowseByMetadataPageComponent {
5251
ngOnInit(): void {
5352
const sortConfig = new SortOptions('default', SortDirection.ASC);
5453
this.startsWithType = StartsWithType.date;
55-
// include the thumbnail configuration in browse search options
56-
this.updatePage(getBrowseSearchOptions(this.defaultBrowseId, this.paginationConfig, sortConfig, this.fetchThumbnails));
5754
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
5855
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
5956
this.subs.push(
60-
observableCombineLatest([this.route.params, this.route.queryParams, this.route.data,
61-
this.currentPagination$, this.currentSort$]).pipe(
62-
map(([routeParams, queryParams, data, currentPage, currentSort]) => {
63-
return [Object.assign({}, routeParams, queryParams, data), currentPage, currentSort];
57+
observableCombineLatest(
58+
[ this.route.params.pipe(take(1)),
59+
this.route.queryParams,
60+
this.currentPagination$,
61+
this.currentSort$]).pipe(
62+
map(([routeParams, queryParams, currentPage, currentSort]) => {
63+
return [Object.assign({}, routeParams, queryParams), currentPage, currentSort];
6464
})
6565
).subscribe(([params, currentPage, currentSort]: [Params, PaginationComponentOptions, SortOptions]) => {
6666
const metadataKeys = params.browseDefinition ? params.browseDefinition.metadataKeys : this.defaultMetadataKeys;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { DSpaceObjectDataService } from '../../core/data/dspace-object-data.serv
1515
import { DSpaceObject } from '../../core/shared/dspace-object.model';
1616
import { StartsWithType } from '../../shared/starts-with/starts-with-decorator';
1717
import { PaginationService } from '../../core/pagination/pagination.service';
18-
import { filter, map, mergeMap } from 'rxjs/operators';
18+
import { filter, map, mergeMap, take } from 'rxjs/operators';
1919
import { followLink, FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
2020
import { Bitstream } from '../../core/shared/bitstream.model';
2121
import { Collection } from '../../core/shared/collection.model';
@@ -152,7 +152,11 @@ export class BrowseByMetadataPageComponent implements OnInit, OnDestroy {
152152
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
153153
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
154154
this.subs.push(
155-
observableCombineLatest([this.route.params, this.route.queryParams, this.currentPagination$, this.currentSort$]).pipe(
155+
observableCombineLatest(
156+
[ this.route.params.pipe(take(1)),
157+
this.route.queryParams,
158+
this.currentPagination$,
159+
this.currentSort$]).pipe(
156160
map(([routeParams, queryParams, currentPage, currentSort]) => {
157161
return [Object.assign({}, routeParams, queryParams),currentPage,currentSort];
158162
})

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { ActivatedRoute, Params, Router } from '@angular/router';
44
import { hasValue } from '../../shared/empty.util';
55
import {
66
BrowseByMetadataPageComponent,
7-
browseParamsToOptions, getBrowseSearchOptions
7+
browseParamsToOptions
88
} from '../browse-by-metadata-page/browse-by-metadata-page.component';
99
import { DSpaceObjectDataService } from '../../core/data/dspace-object-data.service';
1010
import { BrowseService } from '../../core/browse/browse.service';
1111
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
1212
import { PaginationService } from '../../core/pagination/pagination.service';
13-
import { map } from 'rxjs/operators';
13+
import { map, take } from 'rxjs/operators';
1414
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model';
1515
import { AppConfig, APP_CONFIG } from '../../../config/app-config.interface';
1616
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
@@ -38,12 +38,10 @@ export class BrowseByTitlePageComponent extends BrowseByMetadataPageComponent {
3838

3939
ngOnInit(): void {
4040
const sortConfig = new SortOptions('dc.title', SortDirection.ASC);
41-
// include the thumbnail configuration in browse search options
42-
this.updatePage(getBrowseSearchOptions(this.defaultBrowseId, this.paginationConfig, sortConfig, this.fetchThumbnails));
4341
this.currentPagination$ = this.paginationService.getCurrentPagination(this.paginationConfig.id, this.paginationConfig);
4442
this.currentSort$ = this.paginationService.getCurrentSort(this.paginationConfig.id, sortConfig);
4543
this.subs.push(
46-
observableCombineLatest([this.route.params, this.route.queryParams, this.currentPagination$, this.currentSort$]).pipe(
44+
observableCombineLatest([this.route.params.pipe(take(1)), this.route.queryParams, this.currentPagination$, this.currentSort$]).pipe(
4745
map(([routeParams, queryParams, currentPage, currentSort]) => {
4846
return [Object.assign({}, routeParams, queryParams),currentPage,currentSort];
4947
})

src/app/menu.resolver.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import { MenuServiceStub } from './shared/testing/menu-service.stub';
1616
import { MenuID } from './shared/menu/menu-id.model';
1717
import { BrowseService } from './core/browse/browse.service';
1818
import { cold } from 'jasmine-marbles';
19-
import createSpy = jasmine.createSpy;
2019
import { createSuccessfulRemoteDataObject$ } from './shared/remote-data.utils';
2120
import { createPaginatedList } from './shared/testing/utils.test';
21+
import createSpy = jasmine.createSpy;
22+
import { AuthService } from './core/auth/auth.service';
23+
import { AuthServiceStub } from './shared/testing/auth-service.stub';
2224

2325
const BOOLEAN = { t: true, f: false };
2426
const MENU_STATE = {
@@ -61,6 +63,7 @@ describe('MenuResolver', () => {
6163
{ provide: BrowseService, useValue: browseService },
6264
{ provide: AuthorizationDataService, useValue: authorizationService },
6365
{ provide: ScriptDataService, useValue: scriptService },
66+
{ provide: AuthService, useValue: AuthServiceStub },
6467
{
6568
provide: NgbModal, useValue: {
6669
open: () => {/*comment*/
@@ -80,19 +83,19 @@ describe('MenuResolver', () => {
8083
describe('resolve', () => {
8184
it('should create all menus', (done) => {
8285
spyOn(resolver, 'createPublicMenu$').and.returnValue(observableOf(true));
83-
spyOn(resolver, 'createAdminMenu$').and.returnValue(observableOf(true));
86+
spyOn(resolver, 'createAdminMenuIfLoggedIn$').and.returnValue(observableOf(true));
8487

8588
resolver.resolve(null, null).subscribe(resolved => {
8689
expect(resolved).toBeTrue();
8790
expect(resolver.createPublicMenu$).toHaveBeenCalled();
88-
expect(resolver.createAdminMenu$).toHaveBeenCalled();
91+
expect(resolver.createAdminMenuIfLoggedIn$).toHaveBeenCalled();
8992
done();
9093
});
9194
});
9295

9396
it('should return an Observable that emits true as soon as all menus are created', () => {
9497
spyOn(resolver, 'createPublicMenu$').and.returnValue(cold('--(t|)', BOOLEAN));
95-
spyOn(resolver, 'createAdminMenu$').and.returnValue(cold('----(t|)', BOOLEAN));
98+
spyOn(resolver, 'createAdminMenuIfLoggedIn$').and.returnValue(cold('----(t|)', BOOLEAN));
9699

97100
expect(resolver.resolve(null, null)).toBeObservable(cold('----(t|)', BOOLEAN));
98101
});

src/app/menu.resolver.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
3-
import { combineLatest as observableCombineLatest, combineLatest, Observable } from 'rxjs';
3+
import { combineLatest as observableCombineLatest, combineLatest, Observable, of as observableOf } from 'rxjs';
44
import { MenuID } from './shared/menu/menu-id.model';
55
import { MenuState } from './shared/menu/menu-state.model';
66
import { MenuItemType } from './shared/menu/menu-item-type.model';
@@ -12,7 +12,7 @@ import { RemoteData } from './core/data/remote-data';
1212
import { TextMenuItemModel } from './shared/menu/menu-item/models/text.model';
1313
import { BrowseService } from './core/browse/browse.service';
1414
import { MenuService } from './shared/menu/menu.service';
15-
import { filter, find, map, take } from 'rxjs/operators';
15+
import { filter, find, map, mergeMap, take } from 'rxjs/operators';
1616
import { hasValue } from './shared/empty.util';
1717
import { FeatureID } from './core/data/feature-authorization/feature-id';
1818
import {
@@ -47,6 +47,7 @@ import {
4747
import {
4848
ExportBatchSelectorComponent
4949
} from './shared/dso-selector/modal-wrappers/export-batch-selector/export-batch-selector.component';
50+
import { AuthService } from './core/auth/auth.service';
5051

5152
/**
5253
* Creates all of the app's menus
@@ -61,6 +62,7 @@ export class MenuResolver implements Resolve<boolean> {
6162
protected authorizationService: AuthorizationDataService,
6263
protected modalService: NgbModal,
6364
protected scriptDataService: ScriptDataService,
65+
protected authService: AuthService,
6466
) {
6567
}
6668

@@ -70,7 +72,7 @@ export class MenuResolver implements Resolve<boolean> {
7072
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
7173
return combineLatest([
7274
this.createPublicMenu$(),
73-
this.createAdminMenu$(),
75+
this.createAdminMenuIfLoggedIn$(),
7476
]).pipe(
7577
map((menusDone: boolean[]) => menusDone.every(Boolean)),
7678
);
@@ -146,6 +148,15 @@ export class MenuResolver implements Resolve<boolean> {
146148
return this.waitForMenu$(MenuID.PUBLIC);
147149
}
148150

151+
/**
152+
* Initialize all menu sections and items for {@link MenuID.ADMIN}, only if the user is logged in.
153+
*/
154+
createAdminMenuIfLoggedIn$() {
155+
return this.authService.isAuthenticated().pipe(
156+
mergeMap((isAuthenticated) => isAuthenticated ? this.createAdminMenu$() : observableOf(true)),
157+
);
158+
}
159+
149160
/**
150161
* Initialize all menu sections and items for {@link MenuID.ADMIN}
151162
*/

0 commit comments

Comments
 (0)