added and applied person-list resolver

This commit is contained in:
2026-06-08 15:10:52 +02:00
parent cf43330206
commit a82e76bdef
3 changed files with 121 additions and 59 deletions
+30
View File
@@ -1,6 +1,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';
export const routes: Routes = [
{
@@ -25,6 +26,35 @@ export const routes: Routes = [
import('./modules/person-list/person-list.component').then(
(mod) => mod.PersonListComponent,
),
resolve: {
persons: personListResolver,
},
runGuardsAndResolvers: (to, from) => {
const keys = [
'query',
'page',
'pageSize',
'institutionName',
'yearFilterEnabled',
'filterMode',
'filterYear',
];
// scalar params
const scalarChanged = keys.some(
(key) => to.queryParamMap.get(key) !== from.queryParamMap.get(key),
);
// multi-value param: topics
const toTopics = to.queryParamMap.getAll('topic');
const fromTopics = from.queryParamMap.getAll('topic');
const topicsChanged =
toTopics.length !== fromTopics.length ||
toTopics.some((t, i) => t !== fromTopics[i]);
return scalarChanged || topicsChanged;
}
},
{
path: 'publication/:publicationId',
@@ -3,7 +3,6 @@ import { Router, RouterLink, ActivatedRoute } from '@angular/router';
import { CommonModule } from '@angular/common';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, ReactiveFormsModule, Validators } from '@angular/forms';
import { MatIconModule } from '@angular/material/icon';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
@@ -21,12 +20,17 @@ import { SharedStateService } from '../../shared/services/shared-state.service';
import { PaginationService } from '../../shared/services/pagination.service';
import { finalize } from 'rxjs';
import { MatSnackBar } from '@angular/material/snack-bar';
import { SkTextDirective } from '../../custom-directives/sk-text.directive';
import {
NavigationError,
NavigationEnd,
NavigationStart,
NavigationCancel,
} from '@angular/router';
@Component({
selector: 'app-person-list',
imports: [
CommonModule,
RouterLink,
ReactiveFormsModule,
MatIconModule,
MatPaginatorModule,
@@ -38,6 +42,7 @@ import { MatSnackBar } from '@angular/material/snack-bar';
MatRadioModule,
TopicHierarchyComponent,
PersonCardComponent,
SkTextDirective
],
templateUrl: './person-list.component.html',
styleUrl: './person-list.component.scss',
@@ -47,13 +52,9 @@ export class PersonListComponent {
private readonly router = inject(Router);
private readonly route = inject(ActivatedRoute);
private readonly personsService = inject(PersonsService);
private readonly topicsService = inject(TopicsService);
private readonly destroyRef = inject(DestroyRef);
private readonly snackBar = inject(MatSnackBar);
public readonly state = inject(SharedStateService);
public readonly paginationService = inject(PaginationService);
private readonly defaultIncludes =
'display_name,works_count,cited_by_count,topics,orcid,relevance_rating,last_known_institutions,affiliations';
@@ -83,12 +84,27 @@ export class PersonListComponent {
// Bookmarking
bookmarkedIds = new Set<string>();
constructor() {
// Enable/disable the year filter checkbox based on the presence of an institution value
this.router.events
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((event) => {
if (event instanceof NavigationStart) {
this.loadingState.set(true);
}
if (
event instanceof NavigationEnd ||
event instanceof NavigationCancel ||
event instanceof NavigationError
) {
this.loadingState.set(false);
}
});
if (!this.institutionControl.value) {
this.yearFilterEnabledControl.disable();
}
this.institutionControl.valueChanges
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((val) => {
@@ -100,7 +116,6 @@ export class PersonListComponent {
}
});
// Subscribe to route query parameters to trigger searches
this.route.queryParams
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((params) => {
@@ -141,56 +156,15 @@ export class PersonListComponent {
if (this.filterYearControl.value !== filterYear) {
this.filterYearControl.setValue(filterYear);
}
this.load(
query,
page,
pageSize,
topics,
institutionName,
yearFilterEnabled,
filterMode,
filterYear,
);
});
}
load(
query?: string,
page: number = 1,
pageSize: number = 10,
topics?: string[],
institutionName?: string,
yearFilterEnabled: boolean = false,
filterMode: 'in' | 'from' = 'from',
filterYear?: number,
): void {
this.loadingState.set(true);
this.personsService
.searchPersonsList(
query || undefined,
institutionName || undefined,
topics?.length ? topics : undefined,
page,
pageSize,
this.defaultIncludes,
yearFilterEnabled ? true : undefined,
yearFilterEnabled ? filterMode : undefined,
yearFilterEnabled ? filterYear : undefined,
)
.pipe(
finalize(() => this.loadingState.set(false)),
takeUntilDestroyed(this.destroyRef),
)
.subscribe({
next: (response) => {
this.persons = (response.results ?? []) as CustomAuthor[];
this.total = response.meta?.count ?? 0;
},
error: (err) => {
console.error('Error loading persons:', err);
},
// Use resolver value
this.route.data
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(({ persons }) => {
this.persons = (persons.results ?? []) as CustomAuthor[];
this.total = persons.meta?.count;
this.loadingState.set(false);
});
}
@@ -388,7 +362,7 @@ export class PersonListComponent {
this.state.searchValue.set(null);
this.router.navigate([], {
queryParams: { query: null, page: 1 },
queryParamsHandling: 'merge',
queryParamsHandling:"merge",
});
}
@@ -0,0 +1,58 @@
import { ResolveFn } from '@angular/router';
import { inject } from '@angular/core';
import { PersonsService, CustomAuthorsListResponse } from '../api';
export const personListResolver: ResolveFn<CustomAuthorsListResponse> = (
route,
state,
) => {
const parseQueryParamArray = (val: any): string[] => {
if (!val) return [];
if (Array.isArray(val)) return val;
return [val];
};
const defaultIncludes =
'display_name,works_count,cited_by_count,topics,orcid,relevance_rating,last_known_institutions,affiliations';
const params = route.queryParamMap;
const query = params.get('query') || undefined;
const page = params.get('page')
? Number.parseInt(params.get('page')!, 10)
: 1;
const pageSize = params.get('pageSize')
? Number.parseInt(params.get('pageSize')!, 10)
: 10;
const institutionName = params.get('institutionName') || undefined;
const topicsParam = params.getAll('topic');
const topics = parseQueryParamArray(topicsParam)
const yearFilterEnabled = params.get('yearFilterEnabled') === 'true';
const filterModeRaw = params.get('filterMode');
const filterMode =
filterModeRaw === 'in' || filterModeRaw === 'from'
? filterModeRaw
: undefined;
const filterYear = params.get('filterYear')
? Number.parseInt(params.get('filterYear')!, 10)
: undefined;
const personsService = inject(PersonsService);
return personsService.searchPersonsList(
query,
institutionName,
topics,
page,
pageSize,
defaultIncludes,
yearFilterEnabled ? true : undefined,
yearFilterEnabled ? filterMode : undefined,
yearFilterEnabled ? filterYear : undefined,
);
};