diff --git a/src/app/shared/handle.service.spec.ts b/src/app/shared/handle.service.spec.ts index a499bdd464d..8203940c6ad 100644 --- a/src/app/shared/handle.service.spec.ts +++ b/src/app/shared/handle.service.spec.ts @@ -1,8 +1,9 @@ -import { HandleService } from './handle.service'; +import { HandleService, CANONICAL_PREFIX_KEY } from './handle.service'; import { TestBed } from '@angular/core/testing'; import { ConfigurationDataServiceStub } from './testing/configuration-data.service.stub'; import { ConfigurationDataService } from '../core/data/configuration-data.service'; -import { of as observableOf } from 'rxjs'; +import { createSuccessfulRemoteDataObject$ } from './remote-data.utils'; +import { ConfigurationProperty } from '../core/shared/configuration-property.model'; describe('HandleService', () => { let service: HandleService; @@ -22,7 +23,11 @@ describe('HandleService', () => { describe(`normalizeHandle`, () => { it('should normalize a handle url with custom conical prefix with trailing slash', (done: DoneFn) => { - service.canonicalPrefix$ = observableOf('https://hdl.handle.net/'); + spyOn(configurationService, 'findByPropertyName').and.returnValue(createSuccessfulRemoteDataObject$({ + ... new ConfigurationProperty(), + name: CANONICAL_PREFIX_KEY, + values: ['https://hdl.handle.net/'], + })); service.normalizeHandle('https://hdl.handle.net/123456789/123456').subscribe((handle: string | null) => { expect(handle).toBe('123456789/123456'); @@ -31,7 +36,11 @@ describe('HandleService', () => { }); it('should normalize a handle url with custom conical prefix without trailing slash', (done: DoneFn) => { - service.canonicalPrefix$ = observableOf('https://hdl.handle.net'); + spyOn(configurationService, 'findByPropertyName').and.returnValue(createSuccessfulRemoteDataObject$({ + ... new ConfigurationProperty(), + name: CANONICAL_PREFIX_KEY, + values: ['https://hdl.handle.net/'], + })); service.normalizeHandle('https://hdl.handle.net/123456789/123456').subscribe((handle: string | null) => { expect(handle).toBe('123456789/123456'); diff --git a/src/app/shared/handle.service.ts b/src/app/shared/handle.service.ts index 56b3922753b..1f22c7d3045 100644 --- a/src/app/shared/handle.service.ts +++ b/src/app/shared/handle.service.ts @@ -4,7 +4,7 @@ import { ConfigurationDataService } from '../core/data/configuration-data.servic import { getFirstCompletedRemoteData } from '../core/shared/operators'; import { map, take } from 'rxjs/operators'; import { ConfigurationProperty } from '../core/shared/configuration-property.model'; -import { Observable } from 'rxjs'; +import { Observable, of as observableOf } from 'rxjs'; import { RemoteData } from '../core/data/remote-data'; export const CANONICAL_PREFIX_KEY = 'handle.canonical.prefix'; @@ -20,22 +20,9 @@ const NO_PREFIX_REGEX = /^([^\/]+\/[^\/]+)$/; }) export class HandleService { - canonicalPrefix$: Observable; - constructor( protected configurationService: ConfigurationDataService, ) { - this.canonicalPrefix$ = this.configurationService.findByPropertyName(CANONICAL_PREFIX_KEY).pipe( - getFirstCompletedRemoteData(), - take(1), - map((configurationPropertyRD: RemoteData) => { - if (configurationPropertyRD.hasSucceeded) { - return configurationPropertyRD.payload.values.length >= 1 ? configurationPropertyRD.payload.values[0] : undefined; - } else { - return undefined; - } - }), - ); } /** @@ -55,12 +42,20 @@ export class HandleService { * */ normalizeHandle(handle: string): Observable { - return this.canonicalPrefix$.pipe( + if (hasNoValue(handle)) { + return observableOf(null); + } + return this.configurationService.findByPropertyName(CANONICAL_PREFIX_KEY).pipe( + getFirstCompletedRemoteData(), + map((configurationPropertyRD: RemoteData) => { + if (configurationPropertyRD.hasSucceeded) { + return configurationPropertyRD.payload.values.length >= 1 ? configurationPropertyRD.payload.values[0] : undefined; + } else { + return undefined; + } + }), map((prefix: string | undefined) => { let matches: string[]; - if (hasNoValue(handle)) { - return null; - } matches = handle.match(PREFIX_REGEX(prefix)); diff --git a/src/app/shared/theme-support/theme.model.ts b/src/app/shared/theme-support/theme.model.ts index ce470dedc07..470f09853c3 100644 --- a/src/app/shared/theme-support/theme.model.ts +++ b/src/app/shared/theme-support/theme.model.ts @@ -44,27 +44,26 @@ export class RegExTheme extends Theme { export class HandleTheme extends Theme { - private normalizedHandle$: Observable; - constructor(public config: HandleThemeConfig, protected handleService: HandleService ) { super(config); - this.normalizedHandle$ = this.handleService.normalizeHandle(this.config.handle).pipe( - take(1), - ); } matches(url: string, dso: T): Observable { - return combineLatest([ - this.handleService.normalizeHandle(dso?.handle), - this.normalizedHandle$, - ]).pipe( - map(([handle, normalizedHandle]: [string | null, string | null]) => { - return hasValue(dso) && hasValue(dso.handle) && handle === normalizedHandle; - }), - take(1), - ); + if (hasValue(dso?.handle)) { + return combineLatest([ + this.handleService.normalizeHandle(dso?.handle), + this.handleService.normalizeHandle(this.config.handle), + ]).pipe( + map(([handle, normalizedHandle]: [string | null, string | null]) => { + return hasValue(dso) && hasValue(dso.handle) && handle === normalizedHandle; + }), + take(1), + ); + } else { + return observableOf(false); + } } }