Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
38 changes: 16 additions & 22 deletions src/app/item-page/versions/notice/item-versions-notice.component.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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',
Expand Down Expand Up @@ -51,10 +50,6 @@ export class ItemVersionsNoticeComponent implements OnInit {
*/
showLatestVersionNotice$: Observable<boolean>;

/**
* Pagination options to fetch a single version on the first page (this is the latest version in the history)
*/

/**
* The AlertType enumeration
* @type {AlertType}
Expand All @@ -67,8 +62,7 @@ export class ItemVersionsNoticeComponent implements OnInit {
destinationUrl$: Observable<string>;

constructor(private versionHistoryService: VersionHistoryDataService,
private router: Router,
private activatedRoute: ActivatedRoute) {
@Inject(DOCUMENT) private document: Document ) {
}

/**
Expand Down Expand Up @@ -96,27 +90,27 @@ 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 = item?.payload?.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
*/
getItemPage(item: Item): string {
if (hasValue(item)) {
return getItemPageRoute(item);
}
}
}