Skip to content

Commit 7529ed8

Browse files
107671: Fixed theme matching by handle not working in production mode
1 parent 4e54cca commit 7529ed8

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

src/app/shared/handle.service.spec.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { HandleService } from './handle.service';
1+
import { HandleService, CANONICAL_PREFIX_KEY } from './handle.service';
22
import { TestBed } from '@angular/core/testing';
33
import { ConfigurationDataServiceStub } from './testing/configuration-data.service.stub';
44
import { ConfigurationDataService } from '../core/data/configuration-data.service';
5-
import { of as observableOf } from 'rxjs';
5+
import { createSuccessfulRemoteDataObject$ } from './remote-data.utils';
6+
import { ConfigurationProperty } from '../core/shared/configuration-property.model';
67

78
describe('HandleService', () => {
89
let service: HandleService;
@@ -22,7 +23,11 @@ describe('HandleService', () => {
2223

2324
describe(`normalizeHandle`, () => {
2425
it('should normalize a handle url with custom conical prefix with trailing slash', (done: DoneFn) => {
25-
service.canonicalPrefix$ = observableOf('https://hdl.handle.net/');
26+
spyOn(configurationService, 'findByPropertyName').and.returnValue(createSuccessfulRemoteDataObject$({
27+
... new ConfigurationProperty(),
28+
name: CANONICAL_PREFIX_KEY,
29+
values: ['https://hdl.handle.net/'],
30+
}));
2631

2732
service.normalizeHandle('https://hdl.handle.net/123456789/123456').subscribe((handle: string | null) => {
2833
expect(handle).toBe('123456789/123456');
@@ -31,7 +36,11 @@ describe('HandleService', () => {
3136
});
3237

3338
it('should normalize a handle url with custom conical prefix without trailing slash', (done: DoneFn) => {
34-
service.canonicalPrefix$ = observableOf('https://hdl.handle.net');
39+
spyOn(configurationService, 'findByPropertyName').and.returnValue(createSuccessfulRemoteDataObject$({
40+
... new ConfigurationProperty(),
41+
name: CANONICAL_PREFIX_KEY,
42+
values: ['https://hdl.handle.net/'],
43+
}));
3544

3645
service.normalizeHandle('https://hdl.handle.net/123456789/123456').subscribe((handle: string | null) => {
3746
expect(handle).toBe('123456789/123456');

src/app/shared/handle.service.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ConfigurationDataService } from '../core/data/configuration-data.servic
44
import { getFirstCompletedRemoteData } from '../core/shared/operators';
55
import { map, take } from 'rxjs/operators';
66
import { ConfigurationProperty } from '../core/shared/configuration-property.model';
7-
import { Observable } from 'rxjs';
7+
import { Observable, of as observableOf } from 'rxjs';
88
import { RemoteData } from '../core/data/remote-data';
99

1010
export const CANONICAL_PREFIX_KEY = 'handle.canonical.prefix';
@@ -20,22 +20,9 @@ const NO_PREFIX_REGEX = /^([^\/]+\/[^\/]+)$/;
2020
})
2121
export class HandleService {
2222

23-
canonicalPrefix$: Observable<string | undefined>;
24-
2523
constructor(
2624
protected configurationService: ConfigurationDataService,
2725
) {
28-
this.canonicalPrefix$ = this.configurationService.findByPropertyName(CANONICAL_PREFIX_KEY).pipe(
29-
getFirstCompletedRemoteData(),
30-
take(1),
31-
map((configurationPropertyRD: RemoteData<ConfigurationProperty>) => {
32-
if (configurationPropertyRD.hasSucceeded) {
33-
return configurationPropertyRD.payload.values.length >= 1 ? configurationPropertyRD.payload.values[0] : undefined;
34-
} else {
35-
return undefined;
36-
}
37-
}),
38-
);
3926
}
4027

4128
/**
@@ -55,12 +42,20 @@ export class HandleService {
5542
* </ul>
5643
*/
5744
normalizeHandle(handle: string): Observable<string | null> {
58-
return this.canonicalPrefix$.pipe(
45+
if (hasNoValue(handle)) {
46+
return observableOf(null);
47+
}
48+
return this.configurationService.findByPropertyName(CANONICAL_PREFIX_KEY).pipe(
49+
getFirstCompletedRemoteData(),
50+
map((configurationPropertyRD: RemoteData<ConfigurationProperty>) => {
51+
if (configurationPropertyRD.hasSucceeded) {
52+
return configurationPropertyRD.payload.values.length >= 1 ? configurationPropertyRD.payload.values[0] : undefined;
53+
} else {
54+
return undefined;
55+
}
56+
}),
5957
map((prefix: string | undefined) => {
6058
let matches: string[];
61-
if (hasNoValue(handle)) {
62-
return null;
63-
}
6459

6560
matches = handle.match(PREFIX_REGEX(prefix));
6661

src/app/shared/theme-support/theme.model.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,26 @@ export class RegExTheme extends Theme {
4444

4545
export class HandleTheme extends Theme {
4646

47-
private normalizedHandle$: Observable<string | null>;
48-
4947
constructor(public config: HandleThemeConfig,
5048
protected handleService: HandleService
5149
) {
5250
super(config);
53-
this.normalizedHandle$ = this.handleService.normalizeHandle(this.config.handle).pipe(
54-
take(1),
55-
);
5651
}
5752

5853
matches<T extends DSpaceObject & HandleObject>(url: string, dso: T): Observable<boolean> {
59-
return combineLatest([
60-
this.handleService.normalizeHandle(dso?.handle),
61-
this.normalizedHandle$,
62-
]).pipe(
63-
map(([handle, normalizedHandle]: [string | null, string | null]) => {
64-
return hasValue(dso) && hasValue(dso.handle) && handle === normalizedHandle;
65-
}),
66-
take(1),
67-
);
54+
if (hasValue(dso?.handle)) {
55+
return combineLatest([
56+
this.handleService.normalizeHandle(dso?.handle),
57+
this.handleService.normalizeHandle(this.config.handle),
58+
]).pipe(
59+
map(([handle, normalizedHandle]: [string | null, string | null]) => {
60+
return hasValue(dso) && hasValue(dso.handle) && handle === normalizedHandle;
61+
}),
62+
take(1),
63+
);
64+
} else {
65+
return observableOf(false);
66+
}
6867
}
6968
}
7069

0 commit comments

Comments
 (0)