load bio via non-blocking route resolver
This commit is contained in:
@@ -2,6 +2,7 @@ import { Routes } from '@angular/router';
|
||||
import { userGuard } from './shared/guards/user.guard';
|
||||
import { collaborationNetworkResolver } from './resolvers/collaboration-network.resolver';
|
||||
import { personListResolver } from './resolvers/person-list.resolver';
|
||||
import { personBioResolver } from './resolvers/person-bio.resolver';
|
||||
|
||||
export const routes: Routes = [
|
||||
{
|
||||
@@ -71,6 +72,10 @@ export const routes: Routes = [
|
||||
import('./modules/person/person.component').then(
|
||||
(mod) => mod.PersonComponent,
|
||||
),
|
||||
resolve: {
|
||||
personBio: personBioResolver,
|
||||
},
|
||||
runGuardsAndResolvers: (from, to) => from.params['id'] !== to.params['id'],
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
|
||||
@@ -21,6 +21,7 @@ import { of, switchMap, finalize, map, catchError } from 'rxjs';
|
||||
import { PersonInterestingComponent } from '../person-interesting/person-interesting.component';
|
||||
import { PersonsService, WorksService } from '../../api';
|
||||
import { LoadingComponent } from '../../shared/ui/loading/loading.component';
|
||||
import { PersonBioResolved } from '../../resolvers/person-bio.resolver';
|
||||
|
||||
const PERSON_INCLUDE_TAGS =
|
||||
'topics_full,topic_share,summary_stats,ids,last_known_institutions,' +
|
||||
@@ -110,26 +111,17 @@ export class PersonComponent {
|
||||
),
|
||||
{ initialValue: null },
|
||||
);
|
||||
// bio data
|
||||
|
||||
readonly personBio = toSignal(
|
||||
toObservable(this.authorId).pipe(
|
||||
switchMap((id) => {
|
||||
if (!id) {
|
||||
this.activatedRoute.data.pipe(
|
||||
switchMap((data) => {
|
||||
const resolved = data['personBio'] as PersonBioResolved | undefined;
|
||||
if (!resolved) {
|
||||
this.isBioLoading.set(false);
|
||||
return of(null);
|
||||
}
|
||||
this.isBioLoading.set(true);
|
||||
return this.personsApi.generatePersonBio(id).pipe(
|
||||
map((result) => {
|
||||
this.isBioLoading.set(false);
|
||||
return result.bio ?? null;
|
||||
}),
|
||||
catchError((err) => {
|
||||
console.error('Failed to load bio:', err);
|
||||
this.isBioLoading.set(false);
|
||||
return of(null);
|
||||
}),
|
||||
);
|
||||
return resolved.bio$.pipe(finalize(() => this.isBioLoading.set(false)));
|
||||
}),
|
||||
),
|
||||
{ initialValue: null },
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { ResolveFn } from '@angular/router';
|
||||
import { inject } from '@angular/core';
|
||||
import { Observable, of, map, catchError } from 'rxjs';
|
||||
import { PersonsService } from '../api';
|
||||
|
||||
export interface PersonBioResolved {
|
||||
bio$: Observable<string | null>;
|
||||
}
|
||||
|
||||
export const personBioResolver: ResolveFn<PersonBioResolved> = (route) => {
|
||||
const authorId = route.paramMap.get('id');
|
||||
const personsApi = inject(PersonsService);
|
||||
|
||||
if (!authorId) {
|
||||
return { bio$: of(null) };
|
||||
}
|
||||
|
||||
const bio$ = personsApi.generatePersonBio(authorId).pipe(
|
||||
map((result) => result.bio ?? null),
|
||||
catchError((err) => {
|
||||
console.error('Failed to load bio:', err);
|
||||
return of(null);
|
||||
}),
|
||||
);
|
||||
|
||||
return { bio$ };
|
||||
};
|
||||
Reference in New Issue
Block a user