100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
import { CommonModule } from '@angular/common';
|
|
import { Component, DestroyRef, inject, signal } from '@angular/core';
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
|
import { catchError, forkJoin, map, of, switchMap } from 'rxjs';
|
|
|
|
import { CustomAuthor, PersonsService } from '../../api';
|
|
import { BookmarkService } from '../../shared/services/bookmark.service';
|
|
import { PersonCardComponent } from '../person-card/person-card.component';
|
|
|
|
const PERSON_INCLUDE_TAGS =
|
|
'display_name,works_count,cited_by_count,summary_stats,ids,' +
|
|
'last_known_institutions,affiliations,topics,orcid,topics_full,' +
|
|
'topic_share,career_metrics';
|
|
|
|
@Component({
|
|
selector: 'app-bookmarks',
|
|
standalone: true,
|
|
imports: [
|
|
CommonModule,
|
|
MatIconModule,
|
|
MatProgressSpinnerModule,
|
|
PersonCardComponent,
|
|
],
|
|
templateUrl: './bookmarks.component.html',
|
|
styleUrl: './bookmarks.component.scss',
|
|
})
|
|
export class BookmarksComponent {
|
|
readonly bookmarkService = inject(BookmarkService);
|
|
private readonly personsService = inject(PersonsService);
|
|
private readonly destroyRef = inject(DestroyRef);
|
|
|
|
readonly persons = signal<CustomAuthor[]>([]);
|
|
readonly loading = signal(true);
|
|
|
|
constructor() {
|
|
this.loadBookmarks();
|
|
}
|
|
|
|
toggleBookmark(person: CustomAuthor): void {
|
|
this.bookmarkService
|
|
.confirmToggle(person)
|
|
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
.subscribe({
|
|
next: (bookmarked) => {
|
|
if (!bookmarked) {
|
|
const id = this.personId(person);
|
|
this.persons.update((persons) =>
|
|
persons.filter((item) => this.personId(item) !== id),
|
|
);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
private loadBookmarks(): void {
|
|
this.bookmarkService
|
|
.load(true)
|
|
.pipe(
|
|
switchMap((bookmarks) => {
|
|
if (!bookmarks.length) return of([] as CustomAuthor[]);
|
|
return forkJoin(
|
|
bookmarks.map((bookmark) =>
|
|
this.personsService
|
|
.getPersonByID(bookmark.person_id, PERSON_INCLUDE_TAGS)
|
|
.pipe(
|
|
map(
|
|
(response) =>
|
|
response.results?.[0] ?? {
|
|
id: bookmark.person_id,
|
|
display_name: bookmark.person_name,
|
|
},
|
|
),
|
|
catchError(() =>
|
|
of({
|
|
id: bookmark.person_id,
|
|
display_name: bookmark.person_name,
|
|
} as CustomAuthor),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}),
|
|
takeUntilDestroyed(this.destroyRef),
|
|
)
|
|
.subscribe({
|
|
next: (persons) => {
|
|
this.persons.set(persons);
|
|
this.loading.set(false);
|
|
},
|
|
error: () => this.loading.set(false),
|
|
});
|
|
}
|
|
|
|
private personId(person: CustomAuthor): string {
|
|
return person.id?.replace(/\/$/, '').split('/').pop() ?? '';
|
|
}
|
|
}
|