Skip to content

Commit 9ae4195

Browse files
committed
chore(datacite-tracker): refactored doi extraction to be less repetitive
1 parent 11e5c0b commit 9ae4195

4 files changed

Lines changed: 21 additions & 23 deletions

File tree

src/app/features/preprints/pages/preprint-details/preprint-details.component.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { CreateNewVersion, PreprintStepperSelectors } from '@osf/features/prepri
4848
import { IS_MEDIUM, pathJoin } from '@osf/shared/helpers';
4949
import { DataciteTrackerComponent } from '@shared/components/datacite-tracker/datacite-tracker.component';
5050
import { ReviewPermissions, UserPermissions } from '@shared/enums';
51+
import { Identifier } from '@shared/models';
5152
import { MetaTagsService } from '@shared/services';
5253
import { ContributorsSelectors } from '@shared/stores';
5354

@@ -290,12 +291,10 @@ export class PreprintDetailsComponent extends DataciteTrackerComponent implement
290291
this.actions.resetState();
291292
}
292293

293-
protected getDoi(): Observable<string | null> {
294-
return this.preprint$.pipe(
295-
filter((project) => project != null),
296-
map((project) => project?.identifiers?.find((item) => item.category == 'doi')?.value ?? null)
297-
);
294+
protected override get trackable(): Observable<{ identifiers?: Identifier[] } | null> {
295+
return this.preprint$;
298296
}
297+
299298
fetchPreprintVersion(preprintVersionId: string) {
300299
const currentUrl = this.router.url;
301300
const newUrl = currentUrl.replace(/[^/]+$/, preprintVersionId);

src/app/features/project/overview/project-overview.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DialogService } from 'primeng/dynamicdialog';
77
import { Message } from 'primeng/message';
88
import { TagModule } from 'primeng/tag';
99

10-
import { filter, map, Observable } from 'rxjs';
10+
import { Observable } from 'rxjs';
1111

1212
import { CommonModule } from '@angular/common';
1313
import {
@@ -35,6 +35,7 @@ import {
3535
import { DataciteTrackerComponent } from '@shared/components/datacite-tracker/datacite-tracker.component';
3636
import { Mode, ResourceType, UserPermissions } from '@shared/enums';
3737
import { MapProjectOverview } from '@shared/mappers/resource-overview.mappers';
38+
import { Identifier } from '@shared/models';
3839
import { ToastService } from '@shared/services';
3940
import {
4041
ClearWiki,
@@ -191,11 +192,8 @@ export class ProjectOverviewComponent extends DataciteTrackerComponent implement
191192
return null;
192193
});
193194

194-
protected getDoi(): Observable<string | null> {
195-
return this.currentProject$.pipe(
196-
filter((project) => project != null),
197-
map((project) => project?.identifiers?.find((item) => item.category == 'doi')?.value ?? null)
198-
);
195+
protected override get trackable(): Observable<{ identifiers?: Identifier[] } | null> {
196+
return this.currentProject$;
199197
}
200198

201199
constructor() {

src/app/features/registry/registry.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { select } from '@ngxs/store';
22

3-
import { filter, map, Observable } from 'rxjs';
3+
import { Observable } from 'rxjs';
44

55
import { DatePipe } from '@angular/common';
66
import { ChangeDetectionStrategy, Component, effect, HostBinding, inject } from '@angular/core';
@@ -10,6 +10,7 @@ import { RouterOutlet } from '@angular/router';
1010
import { pathJoin } from '@osf/shared/helpers';
1111
import { MetaTagsService } from '@osf/shared/services';
1212
import { DataciteTrackerComponent } from '@shared/components/datacite-tracker/datacite-tracker.component';
13+
import { Identifier } from '@shared/models';
1314

1415
import { RegistryOverviewSelectors } from './store/registry-overview';
1516

@@ -42,11 +43,8 @@ export class RegistryComponent extends DataciteTrackerComponent {
4243
this.setupDataciteViewTrackerEffect().subscribe();
4344
}
4445

45-
protected getDoi(): Observable<string | null> {
46-
return this.registry$.pipe(
47-
filter((project) => project != null),
48-
map((project) => project?.identifiers?.find((item) => item.category == 'doi')?.value ?? null)
49-
);
46+
protected override get trackable(): Observable<{ identifiers?: Identifier[] } | null> {
47+
return this.registry$;
5048
}
5149

5250
private setMetaTags(): void {

src/app/shared/components/datacite-tracker/datacite-tracker.component.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { filter, Observable, switchMap, take } from 'rxjs';
1+
import { filter, map, Observable, switchMap, take } from 'rxjs';
22

33
import { inject, Injectable } from '@angular/core';
44

5+
import { Identifier } from '@shared/models';
56
import { DataciteService } from '@shared/services/datacite/datacite.service';
67

78
@Injectable()
89
export abstract class DataciteTrackerComponent {
910
private dataciteService = inject(DataciteService);
10-
1111
/**
12-
* Abstract method to retrieve the DOI (Digital Object Identifier) of the resource.
12+
* Abstract method to retrieve an observable of resource to be tracked.
13+
* This method is generic enough to support all objects that have `identifiers` property.
1314
* Must be implemented by subclasses.
1415
*
15-
* @returns An Observable that emits a string DOI or null if unavailable.
16+
* @returns An Observable that emits an item which may contain DOI identifier or null .
1617
*/
17-
protected abstract getDoi(): Observable<string | null>;
18+
protected abstract get trackable(): Observable<{ identifiers?: Identifier[] } | null>;
1819

1920
/**
2021
* Sets up a one-time effect to log a "view" event to Datacite for the resource DOI.
@@ -24,7 +25,9 @@ export abstract class DataciteTrackerComponent {
2425
* @returns An Observable that completes after logging the view.
2526
*/
2627
protected setupDataciteViewTrackerEffect(): Observable<void> {
27-
return this.getDoi().pipe(
28+
return this.trackable.pipe(
29+
filter((item) => item != null),
30+
map((item) => item?.identifiers?.find((identifier) => identifier.category == 'doi')?.value ?? null),
2831
take(1),
2932
filter((doi): doi is string => !!doi),
3033
switchMap((doi) => this.dataciteService.logView(doi))

0 commit comments

Comments
 (0)