Skip to content

Commit 508d801

Browse files
Use current activated route as a parameter to the createUrlTree to compose correct redirect url (#796)
1 parent 6d5b428 commit 508d801

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/app/item-page/versions/notice/item-versions-notice.component.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.u
1212
import { createPaginatedList } from '../../../shared/testing/utils.test';
1313
import { of } from 'rxjs';
1414
import { take } from 'rxjs/operators';
15-
import { Router } from '@angular/router';
15+
import { ActivatedRoute, Router } from '@angular/router';
16+
import { MockActivatedRoute } from '../../../shared/mocks/active-router.mock';
1617

1718
describe('ItemVersionsNoticeComponent', () => {
1819
let component: ItemVersionsNoticeComponent;
@@ -58,6 +59,8 @@ describe('ItemVersionsNoticeComponent', () => {
5859
);
5960

6061
let router: Router;
62+
let activatedRoute: ActivatedRoute;
63+
6164
beforeEach(waitForAsync(() => {
6265
router = jasmine.createSpyObj('router', ['createUrlTree']);
6366

@@ -66,7 +69,8 @@ describe('ItemVersionsNoticeComponent', () => {
6669
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])],
6770
providers: [
6871
{ provide: VersionHistoryDataService, useValue: versionHistoryServiceSpy },
69-
{ provide: Router, useValue: router }
72+
{ provide: Router, useValue: router },
73+
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() },
7074
],
7175
schemas: [NO_ERRORS_SCHEMA]
7276
}).compileComponents();

src/app/item-page/versions/notice/item-versions-notice.component.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { map, startWith, switchMap } from 'rxjs/operators';
1414
import { VersionHistoryDataService } from '../../../core/data/version-history-data.service';
1515
import { AlertType } from '../../../shared/alert/alert-type';
1616
import { getItemPageRoute } from '../../item-page-routing-paths';
17-
import { Router } from '@angular/router';
17+
import { ActivatedRoute, Router } from '@angular/router';
1818

1919
@Component({
2020
selector: 'ds-item-versions-notice',
@@ -67,7 +67,8 @@ export class ItemVersionsNoticeComponent implements OnInit {
6767
destinationUrl$: Observable<string>;
6868

6969
constructor(private versionHistoryService: VersionHistoryDataService,
70-
private router: Router) {
70+
private router: Router,
71+
private activatedRoute: ActivatedRoute) {
7172
}
7273

7374
/**
@@ -97,12 +98,14 @@ export class ItemVersionsNoticeComponent implements OnInit {
9798
}
9899
// Compute the destination URL from latestVersion$ with the namespace
99100
this.destinationUrl$ = this.latestVersion$.pipe(
100-
switchMap(latestVersion => latestVersion?.item || of(null)), // Handle the nested observable
101+
switchMap(latestVersion => latestVersion?.item || of(null)),
101102
map(item => {
102-
const payload = item?.payload;
103-
if (!payload) { return ''; } // Fallback if no payload
104-
const routeCommands = [this.getItemPage(payload)]; // Generate route commands
105-
return this.router.createUrlTree(routeCommands).toString(); // Convert to full URL
103+
const routeCommands = [this.getItemPage(item?.payload)]; // e.g., ['/items/xyz']
104+
105+
// Use the current ActivatedRoute to make it work like [routerLink]
106+
const urlTree = this.router.createUrlTree(routeCommands, { relativeTo: this.activatedRoute });
107+
108+
return this.router.serializeUrl(urlTree); // Get the final URL string
106109
})
107110
);
108111
}

0 commit comments

Comments
 (0)