Skip to content

Commit 9160eb1

Browse files
authored
Merge pull request #937 from europeana/develop
Merge develop into master
2 parents 810a04f + 43ed78a commit 9160eb1

File tree

6 files changed

+107
-164
lines changed

6 files changed

+107
-164
lines changed

projects/sandbox/src/app/_services/debias.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HttpClient, HttpHeaders } from '@angular/common/http';
2-
import { inject, Injectable, ModelSignal } from '@angular/core';
2+
import { DestroyRef, inject, Injectable, ModelSignal } from '@angular/core';
3+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
34
import { Observable, timer } from 'rxjs';
45
import { switchMap, takeWhile } from 'rxjs/operators';
56
import { apiSettings } from '../../environments/apisettings';
@@ -8,6 +9,7 @@ import { DebiasDereferenceResult, DebiasInfo, DebiasReport, DebiasState } from '
89
@Injectable({ providedIn: 'root' })
910
export class DebiasService {
1011
private readonly http = inject(HttpClient);
12+
private readonly destroyRef = inject(DestroyRef);
1113

1214
dereferencedSuggestion: string;
1315

@@ -29,6 +31,7 @@ export class DebiasService {
2931
switchMap(() => {
3032
return this.getDebiasInfo(datasetId);
3133
}),
34+
takeUntilDestroyed(this.destroyRef),
3235
takeWhile((info: DebiasInfo) => {
3336
signal.set(info);
3437
return ![DebiasState.COMPLETED, DebiasState.ERROR].includes(info.state);

projects/sandbox/src/app/dataset-info/dataset-info.component.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<span
33
*ngIf="cmpDebias && canOfferDebiasView()"
44
class="debias-display"
5-
[ngClass]="{ locked: !authenticated() }"
6-
[attr.title]="!authenticated() ? 'debias report (log in to enable)' : undefined"
5+
[ngClass]="{ locked: !keycloak.authenticated }"
6+
[attr.title]="!keycloak.authenticated ? 'debias report (log in to enable)' : undefined"
77
>
8-
<span class="locked-indicator" *ngIf="!authenticated()"></span>
8+
<span class="locked-indicator" *ngIf="!keycloak.authenticated"></span>
99

1010
<!-- view link -->
1111
<a
1212
*ngIf="cmpDebias.debiasReport"
13-
[attr.tabindex]="authenticated() ? '0' : null"
13+
[attr.tabindex]="keycloak.authenticated ? '0' : null"
1414
class="debias-opener"
1515
title="view debias report"
1616
(click)="runOrShowDebiasReport(false)"
@@ -27,7 +27,8 @@
2727
</span>
2828
</a>
2929

30-
@let canRunDebias = isOwner() && modelDebiasInfo()?.state === DebiasState.READY;
30+
@let canRunDebias = isOwner() && (modelDebiasInfo().state === DebiasState.READY) && cmpDebias &&
31+
!cmpDebias.debiasReport;
3132

3233
<!-- run link / decoration -->
3334

@@ -82,7 +83,11 @@
8283
#modalDebias
8384
>
8485
<ng-container *ngIf="datasetId()">
85-
<sb-debias [datasetId]="datasetId()" #cmpDebias></sb-debias>
86+
<sb-debias
87+
[datasetId]="datasetId()"
88+
[(signalDebiasInfo)]="modelDebiasInfo"
89+
#cmpDebias
90+
></sb-debias>
8691
</ng-container>
8792
</lib-modal>
8893

projects/sandbox/src/app/dataset-info/dataset-info.component.spec.ts

Lines changed: 34 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ describe('DatasetInfoComponent', () => {
2727
let fixture: ComponentFixture<DatasetInfoComponent>;
2828
let modalConfirms: ModalConfirmService;
2929
let matomo: MatomoService;
30-
let keycloak: Keycloak;
3130
let debias: DebiasService;
3231

3332
const eventKeycloakLoggedOut = ({
@@ -75,10 +74,9 @@ describe('DatasetInfoComponent', () => {
7574
})
7675
.compileComponents();
7776

78-
debias = TestBed.inject(DebiasService);
7977
modalConfirms = TestBed.inject(ModalConfirmService);
8078
matomo = TestBed.inject(MatomoService);
81-
keycloak = TestBed.inject(Keycloak);
79+
debias = TestBed.inject(DebiasService);
8280
};
8381

8482
const getConfirmResult = (): Observable<boolean> => {
@@ -102,7 +100,7 @@ describe('DatasetInfoComponent', () => {
102100
it('should pre-authenticate', () => {
103101
TestBed.flushEffects();
104102
fixture.detectChanges();
105-
expect(component.authenticated()).toBeTruthy();
103+
expect(component.keycloakSignal()).toBeTruthy();
106104
});
107105

108106
it('should initiate polling', fakeAsync(() => {
@@ -123,6 +121,33 @@ describe('DatasetInfoComponent', () => {
123121

124122
expect(component.cmpDebias.pollDebiasReport).toHaveBeenCalled();
125123
}));
124+
125+
it('should run the debias report', fakeAsync(() => {
126+
fixture.componentRef.setInput('datasetId', '1');
127+
fixture.detectChanges();
128+
TestBed.flushEffects();
129+
tick(1);
130+
131+
const datasetInfo = component.datasetInfo();
132+
expect(datasetInfo).toBeTruthy();
133+
if (datasetInfo) {
134+
expect(datasetInfo['created-by-id']).toEqual('1234');
135+
}
136+
137+
component.keycloak.idTokenParsed = { sub: '1234' };
138+
139+
spyOn(debias, 'runDebiasReport').and.callThrough();
140+
141+
component.runOrShowDebiasReport(true);
142+
tick(1);
143+
fixture.detectChanges();
144+
TestBed.flushEffects();
145+
tick(1);
146+
expect(debias.runDebiasReport).toHaveBeenCalled();
147+
expect(component.isOwner()).toBeTruthy();
148+
149+
component.keycloak.idTokenParsed = { sub: '' };
150+
}));
126151
});
127152

128153
describe('Not logged in)', () => {
@@ -139,7 +164,6 @@ describe('DatasetInfoComponent', () => {
139164
it('should create', () => {
140165
expect(component).toBeTruthy();
141166
expect(component.datasetInfo()).toBeFalsy();
142-
expect(component.authenticated()).toBeFalsy();
143167
});
144168

145169
it('should track the user viewing the published records', () => {
@@ -254,13 +278,11 @@ describe('DatasetInfoComponent', () => {
254278
TestBed.flushEffects();
255279
tick(1);
256280

257-
component.authenticated.set(true);
258-
keycloak.idTokenParsed = { sub: '1234' };
259-
260-
component.cmpDebias.isBusy = true;
261-
tick(1);
262-
expect(component.authenticated()).toBeTruthy();
263-
TestBed.flushEffects();
281+
const datasetInfo = component.datasetInfo();
282+
expect(datasetInfo).toBeTruthy();
283+
if (datasetInfo) {
284+
expect(datasetInfo['created-by-id']).toEqual('1234');
285+
}
264286

265287
spyOn(debias, 'runDebiasReport').and.callThrough();
266288

@@ -270,68 +292,6 @@ describe('DatasetInfoComponent', () => {
270292
TestBed.flushEffects();
271293
tick(1);
272294
expect(debias.runDebiasReport).not.toHaveBeenCalled();
273-
274-
component.cmpDebias.isBusy = false;
275-
276-
component.runOrShowDebiasReport(true);
277-
fixture.detectChanges();
278-
TestBed.flushEffects();
279-
tick(1);
280-
expect(debias.runDebiasReport).toHaveBeenCalled();
281-
282-
component.authenticated.set(false);
283-
keycloak.idTokenParsed = { sub: 'tokenUnknown' };
284-
component.authenticated.set(true);
285-
286-
TestBed.flushEffects();
287-
288-
component.runOrShowDebiasReport(true);
289-
fixture.detectChanges();
290-
TestBed.flushEffects();
291-
tick(1);
292-
expect(debias.runDebiasReport).toHaveBeenCalledTimes(1);
293-
}));
294-
295-
it('should show the debias report', fakeAsync(() => {
296-
fixture.componentRef.setInput('datasetId', '1');
297-
fixture.detectChanges();
298-
299-
component.authenticated.set(true);
300-
keycloak.idTokenParsed = { sub: '1234' };
301-
302-
spyOn(modalConfirms, 'open').and.callFake(getConfirmResult);
303-
304-
tick(1);
305-
fixture.detectChanges();
306-
307-
component.runOrShowDebiasReport(false);
308-
expect(modalConfirms.open).toHaveBeenCalled();
309-
}));
310-
311-
it('should compute the debias owner', fakeAsync(() => {
312-
const tokenOwner = '1234';
313-
const tokenNonOwner = '4321';
314-
315-
fixture.componentRef.setInput('datasetId', '1');
316-
keycloak.idTokenParsed = { sub: tokenNonOwner };
317-
318-
fixture.detectChanges();
319-
component.authenticated.set(true);
320-
tick(1);
321-
322-
expect(component.authenticated()).toBeTruthy();
323-
324-
fixture.detectChanges();
325-
TestBed.flushEffects();
326-
expect(component.isOwner()).toBeFalsy();
327-
328-
component.authenticated.set(false);
329-
keycloak.idTokenParsed = { sub: tokenOwner };
330-
component.authenticated.set(true);
331-
332-
TestBed.flushEffects();
333-
334-
expect(component.isOwner()).toBeTruthy();
335295
}));
336296
});
337297
});

projects/sandbox/src/app/dataset-info/dataset-info.component.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ import {
1818
linkedSignal,
1919
model,
2020
ModelSignal,
21-
signal,
2221
ViewChild
2322
} from '@angular/core';
2423
import { toObservable, toSignal } from '@angular/core/rxjs-interop';
2524
import { switchMap, tap } from 'rxjs';
2625
import { take } from 'rxjs/operators';
2726

2827
import Keycloak from 'keycloak-js';
29-
import { KEYCLOAK_EVENT_SIGNAL, KeycloakEventType } from 'keycloak-angular';
28+
import { KEYCLOAK_EVENT_SIGNAL } from 'keycloak-angular';
3029

3130
import {
3231
ClickAwareDirective,
@@ -65,8 +64,8 @@ export class DatasetInfoComponent extends SubscriptionManager {
6564
private readonly debias = inject(DebiasService);
6665
private readonly sandbox = inject(SandboxService);
6766
private readonly matomo = inject(MatomoService);
68-
private readonly keycloakSignal = inject(KEYCLOAK_EVENT_SIGNAL);
69-
private readonly keycloak = inject(Keycloak);
67+
68+
readonly keycloakSignal = inject(KEYCLOAK_EVENT_SIGNAL);
7069

7170
public DatasetStatus = DatasetStatus;
7271
public DebiasState = DebiasState;
@@ -77,6 +76,7 @@ export class DatasetInfoComponent extends SubscriptionManager {
7776
'top-level-nav'
7877
];
7978

79+
readonly keycloak = inject(Keycloak);
8080
readonly pushHeight = input(false);
8181
readonly modalIdPrefix = input('');
8282
readonly datasetId = input.required<string>();
@@ -85,17 +85,15 @@ export class DatasetInfoComponent extends SubscriptionManager {
8585
@ViewChild('cmpDebias') cmpDebias: DebiasComponent;
8686

8787
// Top-level signals
88-
authenticated = signal(false);
8988

9089
isOwner = computed(() => {
91-
let value = false;
92-
if (this.authenticated()) {
90+
if (this.keycloakSignal()) {
9391
const info = this.datasetInfo();
9492
if (info && info['created-by-id'] === this.keycloak.idTokenParsed?.sub) {
95-
value = true;
93+
return true;
9694
}
9795
}
98-
return value;
96+
return false;
9997
});
10098

10199
canOfferDebiasView = linkedSignal({
@@ -109,7 +107,7 @@ export class DatasetInfoComponent extends SubscriptionManager {
109107
});
110108

111109
modelDebiasInfo: ModelSignal<DebiasInfo> = model(({
112-
status: DebiasState.INITIAL
110+
state: DebiasState.INITIAL
113111
} as unknown) as DebiasInfo);
114112

115113
datasetInfo = toSignal(
@@ -168,15 +166,6 @@ export class DatasetInfoComponent extends SubscriptionManager {
168166

169167
constructor() {
170168
super();
171-
effect(() => {
172-
const keycloakEvent = this.keycloakSignal();
173-
if (keycloakEvent.type === KeycloakEventType.Ready) {
174-
this.authenticated.set(true);
175-
} else {
176-
this.authenticated.set(false);
177-
}
178-
});
179-
180169
effect(() => {
181170
// close modal and trigger poll for info on dataset id change
182171
if (this.modalConfirms.isOpen(this.modalIdPrefix() + this.modalIdDebias)) {
@@ -187,13 +176,9 @@ export class DatasetInfoComponent extends SubscriptionManager {
187176

188177
effect(() => {
189178
// trigger poll for report (to get detections number)
190-
if (
191-
![DebiasState.ERROR, DebiasState.INITIAL, DebiasState.READY].includes(
192-
this.modelDebiasInfo().state
193-
)
194-
) {
179+
if ([DebiasState.PROCESSING, DebiasState.COMPLETED].includes(this.modelDebiasInfo().state)) {
195180
if (this.cmpDebias) {
196-
this.cmpDebias.pollDebiasReport(this.modelDebiasInfo);
181+
this.cmpDebias.pollDebiasReport();
197182
}
198183
}
199184
});
@@ -268,7 +253,7 @@ export class DatasetInfoComponent extends SubscriptionManager {
268253
const datasetId = this.datasetId();
269254
this.subs.push(
270255
this.debias.runDebiasReport(datasetId).subscribe(() => {
271-
this.cmpDebias.pollDebiasReport(this.modelDebiasInfo);
256+
this.cmpDebias.pollDebiasReport();
272257
})
273258
);
274259
}
@@ -290,7 +275,6 @@ export class DatasetInfoComponent extends SubscriptionManager {
290275
**/
291276
runOrShowDebiasReport(run: boolean, openerRef?: HTMLElement, openViaKeyboard = false): void {
292277
if (run && !this.isOwner()) {
293-
console.log('not allowed to run');
294278
return;
295279
}
296280
if (run) {

0 commit comments

Comments
 (0)