Skip to content

Commit

Permalink
107671: Fixed theme matching by handle not working in production mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrevryghem committed Nov 11, 2023
1 parent 4e54cca commit b67cf3b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
17 changes: 13 additions & 4 deletions src/app/shared/handle.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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');
Expand All @@ -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');
Expand Down
31 changes: 13 additions & 18 deletions src/app/shared/handle.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,22 +20,9 @@ const NO_PREFIX_REGEX = /^([^\/]+\/[^\/]+)$/;
})
export class HandleService {

canonicalPrefix$: Observable<string | undefined>;

constructor(
protected configurationService: ConfigurationDataService,
) {
this.canonicalPrefix$ = this.configurationService.findByPropertyName(CANONICAL_PREFIX_KEY).pipe(
getFirstCompletedRemoteData(),
take(1),
map((configurationPropertyRD: RemoteData<ConfigurationProperty>) => {
if (configurationPropertyRD.hasSucceeded) {
return configurationPropertyRD.payload.values.length >= 1 ? configurationPropertyRD.payload.values[0] : undefined;
} else {
return undefined;
}
}),
);
}

/**
Expand All @@ -55,12 +42,20 @@ export class HandleService {
* </ul>
*/
normalizeHandle(handle: string): Observable<string | null> {
return this.canonicalPrefix$.pipe(
if (hasNoValue(handle)) {
return observableOf(null);
}
return this.configurationService.findByPropertyName(CANONICAL_PREFIX_KEY).pipe(
getFirstCompletedRemoteData(),
map((configurationPropertyRD: RemoteData<ConfigurationProperty>) => {
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));

Expand Down
27 changes: 13 additions & 14 deletions src/app/shared/theme-support/theme.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,26 @@ export class RegExTheme extends Theme {

export class HandleTheme extends Theme {

private normalizedHandle$: Observable<string | null>;

constructor(public config: HandleThemeConfig,
protected handleService: HandleService
) {
super(config);
this.normalizedHandle$ = this.handleService.normalizeHandle(this.config.handle).pipe(
take(1),
);
}

matches<T extends DSpaceObject & HandleObject>(url: string, dso: T): Observable<boolean> {
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);
}
}
}

Expand Down

0 comments on commit b67cf3b

Please sign in to comment.