-
Notifications
You must be signed in to change notification settings - Fork 2
[Port to dtq-dev] Improve back navigation logic in ItemComponent #1184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dtq-dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -76,6 +76,12 @@ export function getIIIFEnabled(enabled: boolean): MetadataValue { | |||||||||||||||||||||||||||||||||||||||||||
| export const mockRouteService = { | ||||||||||||||||||||||||||||||||||||||||||||
| getPreviousUrl(): Observable<string> { | ||||||||||||||||||||||||||||||||||||||||||||
| return observableOf(''); | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| storeUrlInSession(key: string, url: string): void { | ||||||||||||||||||||||||||||||||||||||||||||
| // no-op | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| getUrlFromSession(key: string): string | null { | ||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -425,6 +431,7 @@ describe('ItemComponent', () => { | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const searchUrl = '/search?query=test&spc.page=2'; | ||||||||||||||||||||||||||||||||||||||||||||
| const browseUrl = '/browse/title?scope=0cc&bbm.page=3'; | ||||||||||||||||||||||||||||||||||||||||||||
| const homeUrl = '/home'; | ||||||||||||||||||||||||||||||||||||||||||||
| const recentSubmissionsUrl = '/collections/be7b8430-77a5-4016-91c9-90863e50583a?cp.page=3'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| beforeEach(waitForAsync(() => { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -485,6 +492,7 @@ describe('ItemComponent', () => { | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should hide back button',() => { | ||||||||||||||||||||||||||||||||||||||||||||
| spyOn(mockRouteService, 'getPreviousUrl').and.returnValue(observableOf('/item')); | ||||||||||||||||||||||||||||||||||||||||||||
| comp.ngOnInit(); | ||||||||||||||||||||||||||||||||||||||||||||
| comp.showBackButton.subscribe((val) => { | ||||||||||||||||||||||||||||||||||||||||||||
| expect(val).toBeFalse(); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -510,6 +518,32 @@ describe('ItemComponent', () => { | |||||||||||||||||||||||||||||||||||||||||||
| expect(val).toBeTrue(); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should show back button for home', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| spyOn(mockRouteService, 'getPreviousUrl').and.returnValue(observableOf(homeUrl)); | ||||||||||||||||||||||||||||||||||||||||||||
| comp.ngOnInit(); | ||||||||||||||||||||||||||||||||||||||||||||
| comp.showBackButton.subscribe((val) => { | ||||||||||||||||||||||||||||||||||||||||||||
| expect(val).toBeTrue(); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should prioritize home previous url over session fallback', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const staleSessionUrl = searchUrl; | ||||||||||||||||||||||||||||||||||||||||||||
| const getPreviousUrlSpy = spyOn(mockRouteService, 'getPreviousUrl').and.returnValue(observableOf(homeUrl)); | ||||||||||||||||||||||||||||||||||||||||||||
| const getUrlFromSessionSpy = spyOn(mockRouteService, 'getUrlFromSession').and.returnValue(staleSessionUrl); | ||||||||||||||||||||||||||||||||||||||||||||
| const storeUrlInSessionSpy = spyOn(mockRouteService, 'storeUrlInSession'); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| comp.ngOnInit(); | ||||||||||||||||||||||||||||||||||||||||||||
| comp.showBackButton.subscribe((val) => { | ||||||||||||||||||||||||||||||||||||||||||||
| expect(val).toBeTrue(); | ||||||||||||||||||||||||||||||||||||||||||||
| expect(getPreviousUrlSpy).toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||
| expect(getUrlFromSessionSpy).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||
| expect(storeUrlInSessionSpy).toHaveBeenCalledWith('item-previous-url', homeUrl); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| comp.back(); | ||||||||||||||||||||||||||||||||||||||||||||
| expect(router.navigateByUrl).toHaveBeenCalledWith(homeUrl); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| }); | |
| }); | |
| it('should fallback to session stored url when previous url is invalid', () => { | |
| const invalidPreviousUrl = '/item'; | |
| const sessionUrl = searchUrl; | |
| const getPreviousUrlSpy = spyOn(mockRouteService, 'getPreviousUrl').and.returnValue(observableOf(invalidPreviousUrl)); | |
| const getUrlFromSessionSpy = spyOn(mockRouteService, 'getUrlFromSession').and.returnValue(sessionUrl); | |
| const storeUrlInSessionSpy = spyOn(mockRouteService, 'storeUrlInSession'); | |
| comp.ngOnInit(); | |
| comp.showBackButton.subscribe((val) => { | |
| expect(val).toBeTrue(); | |
| expect(getPreviousUrlSpy).toHaveBeenCalled(); | |
| expect(getUrlFromSessionSpy).toHaveBeenCalledWith('item-previous-url'); | |
| // Depending on implementation, the component may or may not store the session URL again. | |
| // The critical behavior is that navigation uses the fallback URL from session storage. | |
| comp.back(); | |
| expect(router.navigateByUrl).toHaveBeenCalledWith(sessionUrl); | |
| }); | |
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import { getItemPageRoute } from '../../../item-page-routing-paths'; | |
| import { RouteService } from '../../../../core/services/route.service'; | ||
| import { Observable } from 'rxjs'; | ||
| import { getDSpaceQuery, isIiifEnabled, isIiifSearchEnabled } from './item-iiif-utils'; | ||
| import { filter, map, take } from 'rxjs/operators'; | ||
| import { map, take } from 'rxjs/operators'; | ||
| import { Router } from '@angular/router'; | ||
| import { select, Store } from '@ngrx/store'; | ||
| import { AppState } from 'src/app/app.reducer'; | ||
|
|
@@ -22,11 +22,16 @@ import { APP_CONFIG, AppConfig } from 'src/config/app-config.interface'; | |
| export class ItemComponent implements OnInit { | ||
| @Input() object: Item; | ||
|
|
||
| /** | ||
| * Session storage key for storing the previous URL before entering item page | ||
| */ | ||
| private readonly ITEM_PREVIOUS_URL_SESSION_KEY = 'item-previous-url'; | ||
|
|
||
| /** | ||
| * This regex matches previous routes. The button is shown | ||
| * for matching paths and hidden in other cases. | ||
| */ | ||
| previousRoute = /^(\/search|\/browse|\/collections|\/admin\/search|\/mydspace)/; | ||
| previousRoute = /^(\/home|\/search|\/browse|\/collections|\/admin\/search|\/mydspace)/; | ||
|
|
||
| /** | ||
| * Used to show or hide the back to results button in the view. | ||
|
|
@@ -57,6 +62,11 @@ export class ItemComponent implements OnInit { | |
|
|
||
| isAuthenticated$: Observable<boolean>; | ||
|
|
||
| /** | ||
| * Stores the previous URL retrieved either from RouteService or sessionStorage | ||
| */ | ||
| private storedPreviousUrl: string; | ||
|
|
||
| constructor(protected routeService: RouteService, | ||
| protected router: Router, | ||
| private store: Store<AppState>, | ||
|
|
@@ -66,26 +76,36 @@ export class ItemComponent implements OnInit { | |
|
|
||
| /** | ||
| * The function used to return to list from the item. | ||
| * Uses stored previous URL if available, otherwise falls back to browser history. | ||
| */ | ||
| back = () => { | ||
| this.routeService.getPreviousUrl().pipe( | ||
| take(1) | ||
| ).subscribe( | ||
| (url => { | ||
| this.router.navigateByUrl(url); | ||
| }) | ||
| ); | ||
| this.router.navigateByUrl(this.storedPreviousUrl); | ||
| }; | ||
milanmajchrak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ngOnInit(): void { | ||
|
|
||
| this.itemPageRoute = getItemPageRoute(this.object); | ||
| // hide/show the back button | ||
| this.showBackButton = this.routeService.getPreviousUrl().pipe( | ||
| filter(url => this.previousRoute.test(url)), | ||
| take(1), | ||
| map(() => true) | ||
| map(url => { | ||
| const fromRoute = this.pickAllowedPrevious(url); | ||
|
|
||
| if (fromRoute) { | ||
| this.routeService.storeUrlInSession(this.ITEM_PREVIOUS_URL_SESSION_KEY, fromRoute); | ||
| this.storedPreviousUrl = fromRoute; | ||
| return true; | ||
| } | ||
|
|
||
| const storedUrl = this.routeService.getUrlFromSession(this.ITEM_PREVIOUS_URL_SESSION_KEY); | ||
| if (this.pickAllowedPrevious(storedUrl)) { | ||
| this.storedPreviousUrl = storedUrl; | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| }) | ||
| ); | ||
|
Comment on lines
88
to
107
|
||
|
|
||
| // check to see if iiif viewer is required. | ||
| this.iiifEnabled = isIiifEnabled(this.object); | ||
| this.iiifSearchEnabled = isIiifSearchEnabled(this.object); | ||
|
|
@@ -95,6 +115,13 @@ export class ItemComponent implements OnInit { | |
| this.isAuthenticated$ = this.store.pipe(select(isAuthenticated)); | ||
| } | ||
|
|
||
| /** | ||
| * Helper to check if a URL is from an allowed previous route and return it, otherwise null | ||
| */ | ||
| private pickAllowedPrevious(url?: string | null): string | null { | ||
| return url && this.previousRoute.test(url) ? url : null; | ||
| } | ||
|
|
||
| get hasConfiguredStatistics(): boolean { | ||
| return !!this.appConfig.statistics?.baseUrl && !!this.appConfig.statistics?.endpoint; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,16 @@ export const routeServiceStub: any = { | |||||||
| }, | ||||||||
| getPreviousUrl: () => { | ||||||||
| return observableOf('/home'); | ||||||||
| }, | ||||||||
| // Added generic session helpers used by ItemComponent | ||||||||
| storeUrlInSession: (key: string, url: string) => { | ||||||||
| // no-op for tests | ||||||||
| }, | ||||||||
| getUrlFromSession: (key: string): string | null => { | ||||||||
| return null; | ||||||||
| }, | ||||||||
| clearUrlFromSession: (key: string) => { | ||||||||
| // no-op for tests | ||||||||
|
Comment on lines
+47
to
+49
|
||||||||
| }, | |
| clearUrlFromSession: (key: string) => { | |
| // no-op for tests |
Uh oh!
There was an error while loading. Please reload this page.