From bb879f789f6a26bf376504f20a17c917d7ed10fa Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 6 Mar 2025 17:03:21 +0100 Subject: [PATCH 1/3] Get the base href using the DOCUMENT --- .../notice/item-versions-notice.component.ts | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/app/item-page/versions/notice/item-versions-notice.component.ts b/src/app/item-page/versions/notice/item-versions-notice.component.ts index 6a127f73b53..ebcdfae97b8 100644 --- a/src/app/item-page/versions/notice/item-versions-notice.component.ts +++ b/src/app/item-page/versions/notice/item-versions-notice.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, OnInit } from '@angular/core'; +import { Component, Inject, Input, OnInit } from '@angular/core'; import { Item } from '../../../core/shared/item.model'; import { Observable, of } from 'rxjs'; import { RemoteData } from '../../../core/data/remote-data'; @@ -13,8 +13,7 @@ import { import { map, startWith, switchMap } from 'rxjs/operators'; import { VersionHistoryDataService } from '../../../core/data/version-history-data.service'; import { AlertType } from '../../../shared/alert/alert-type'; -import { getItemPageRoute } from '../../item-page-routing-paths'; -import { ActivatedRoute, Router } from '@angular/router'; +import { DOCUMENT } from '@angular/common'; @Component({ selector: 'ds-item-versions-notice', @@ -51,10 +50,6 @@ export class ItemVersionsNoticeComponent implements OnInit { */ showLatestVersionNotice$: Observable; - /** - * Pagination options to fetch a single version on the first page (this is the latest version in the history) - */ - /** * The AlertType enumeration * @type {AlertType} @@ -67,8 +62,7 @@ export class ItemVersionsNoticeComponent implements OnInit { destinationUrl$: Observable; constructor(private versionHistoryService: VersionHistoryDataService, - private router: Router, - private activatedRoute: ActivatedRoute) { + @Inject(DOCUMENT) private document: Document ) { } /** @@ -96,27 +90,39 @@ export class ItemVersionsNoticeComponent implements OnInit { startWith(false), ); } + // Compute the destination URL from latestVersion$ with the namespace this.destinationUrl$ = this.latestVersion$.pipe( switchMap(latestVersion => latestVersion?.item || of(null)), map(item => { - const routeCommands = [this.getItemPage(item?.payload)]; // e.g., ['/items/xyz'] + const itemId = this.extractItemId(item?.payload); // Extract only the UUID + + if (!itemId) { + console.error('No valid UUID found in payload'); + return this.document.location.pathname; // Fallback to the current path if extraction fails + } - // Use the current ActivatedRoute to make it work like [routerLink] - const urlTree = this.router.createUrlTree(routeCommands, { relativeTo: this.activatedRoute }); + // Get the base URL dynamically - with the namespace. Remove the last part of the path (the item UUID). + const baseUrl = this.document.location.pathname.split('/').slice(0, -1).join('/'); - return this.router.serializeUrl(urlTree); // Get the final URL string + // Construct the final URL dynamically + const finalUrl = `${baseUrl}/${itemId}`; + + return finalUrl; }) ); } /** - * Get the item page url - * @param item The item for which the url is requested + * Extracts the first valid UUID from the payload */ - getItemPage(item: Item): string { - if (hasValue(item)) { - return getItemPageRoute(item); - } + extractItemId(payload: any): string | null { + if (!payload) { return null; } + + // Extracts the first valid UUID from the payload + const uuidRegex = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/; + const match = JSON.stringify(payload).match(uuidRegex); // Convert payload to string and search for UUID + + return match ? match[0] : null; // Return the first UUID found, or null if none } } From cb1b3bdabfb918071b9dfeb9e8ab4e19c53eb030 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 6 Mar 2025 17:09:11 +0100 Subject: [PATCH 2/3] Removed unused services from the test --- .../notice/item-versions-notice.component.spec.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/app/item-page/versions/notice/item-versions-notice.component.spec.ts b/src/app/item-page/versions/notice/item-versions-notice.component.spec.ts index ea5f1881a59..2b3270442d8 100644 --- a/src/app/item-page/versions/notice/item-versions-notice.component.spec.ts +++ b/src/app/item-page/versions/notice/item-versions-notice.component.spec.ts @@ -12,8 +12,6 @@ import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.u import { createPaginatedList } from '../../../shared/testing/utils.test'; import { of } from 'rxjs'; import { take } from 'rxjs/operators'; -import { ActivatedRoute, Router } from '@angular/router'; -import { MockActivatedRoute } from '../../../shared/mocks/active-router.mock'; describe('ItemVersionsNoticeComponent', () => { let component: ItemVersionsNoticeComponent; @@ -58,19 +56,12 @@ describe('ItemVersionsNoticeComponent', () => { ['getVersions', 'getLatestVersionFromHistory$', 'isLatest$', ] ); - let router: Router; - let activatedRoute: ActivatedRoute; - beforeEach(waitForAsync(() => { - router = jasmine.createSpyObj('router', ['createUrlTree']); - TestBed.configureTestingModule({ declarations: [ItemVersionsNoticeComponent], imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])], providers: [ - { provide: VersionHistoryDataService, useValue: versionHistoryServiceSpy }, - { provide: Router, useValue: router }, - { provide: ActivatedRoute, useValue: new MockActivatedRoute() }, + { provide: VersionHistoryDataService, useValue: versionHistoryServiceSpy } ], schemas: [NO_ERRORS_SCHEMA] }).compileComponents(); From 93c12594346a4cddc81416fe6bd27fdcef19dd37 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Fri, 7 Mar 2025 09:51:02 +0100 Subject: [PATCH 3/3] There is no need to extract the item's UUID --- .../notice/item-versions-notice.component.ts | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/app/item-page/versions/notice/item-versions-notice.component.ts b/src/app/item-page/versions/notice/item-versions-notice.component.ts index ebcdfae97b8..37f81976132 100644 --- a/src/app/item-page/versions/notice/item-versions-notice.component.ts +++ b/src/app/item-page/versions/notice/item-versions-notice.component.ts @@ -95,7 +95,7 @@ export class ItemVersionsNoticeComponent implements OnInit { this.destinationUrl$ = this.latestVersion$.pipe( switchMap(latestVersion => latestVersion?.item || of(null)), map(item => { - const itemId = this.extractItemId(item?.payload); // Extract only the UUID + const itemId = item?.payload?.uuid; if (!itemId) { console.error('No valid UUID found in payload'); @@ -113,16 +113,4 @@ export class ItemVersionsNoticeComponent implements OnInit { ); } - /** - * Extracts the first valid UUID from the payload - */ - extractItemId(payload: any): string | null { - if (!payload) { return null; } - - // Extracts the first valid UUID from the payload - const uuidRegex = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/; - const match = JSON.stringify(payload).match(uuidRegex); // Convert payload to string and search for UUID - - return match ? match[0] : null; // Return the first UUID found, or null if none - } }