Files
smartguide/webapp/src/app/modules/person-overview/person-overview.component.ts
T

85 lines
2.6 KiB
TypeScript

import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { combineLatest, distinctUntilChanged, map } from 'rxjs';
import {
ResearchTimelineComponent,
TopicTimelineElement,
} from './components/research-timeline/research-timeline.component';
interface TopicTimelineResponse {
elements: TopicTimelineElement[];
total: number;
}
@Component({
selector: 'app-person-overview',
standalone: true,
imports: [CommonModule, ResearchTimelineComponent],
templateUrl: './person-overview.component.html',
styleUrl: './person-overview.component.scss',
})
export class PersonOverviewComponent implements OnInit {
private readonly route = inject(ActivatedRoute);
private readonly http = inject(HttpClient);
private readonly destroyRef = inject(DestroyRef);
topicTimelineElements: TopicTimelineElement[] = [];
loadingTimeline = false;
timelineLoadFailed = false;
ngOnInit(): void {
combineLatest(this.route.pathFromRoot.map((route) => route.paramMap))
.pipe(
map((paramMaps) => this.findAuthorId(paramMaps)),
distinctUntilChanged(),
takeUntilDestroyed(this.destroyRef),
)
.subscribe((authorId) => {
this.loadTopicTimeline(authorId);
});
}
private findAuthorId(paramMaps: readonly ParamMap[]): string {
return (
paramMaps
.map((paramMap) => paramMap.get('id'))
.find((id): id is string => Boolean(id)) ?? ''
);
}
private loadTopicTimeline(authorId: string): void {
if (!authorId || authorId === '0') {
this.topicTimelineElements = [];
this.loadingTimeline = false;
this.timelineLoadFailed = false;
return;
}
this.loadingTimeline = true;
this.timelineLoadFailed = false;
const topicCountLimit = 4;
const url = `/inse-service/authors/${encodeURIComponent(authorId)}/topics-by-time?max_topics_per_year=${topicCountLimit}`;
this.http
.get<TopicTimelineResponse>(url)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: (response) => {
this.topicTimelineElements = response.elements ?? [];
this.loadingTimeline = false;
this.timelineLoadFailed = false;
},
error: (error) => {
console.error('Topic timeline could not be loaded.', error);
this.topicTimelineElements = [];
this.loadingTimeline = false;
this.timelineLoadFailed = true;
},
});
}
}