made the "Zurück" button visible only within components
This commit is contained in:
@@ -3,24 +3,31 @@
|
||||
<input
|
||||
matInput
|
||||
type="search"
|
||||
aria-label="Search"
|
||||
aria-label="Personensuche"
|
||||
[matAutocomplete]="auto"
|
||||
[formControl]="searchControl"
|
||||
(keydown.tab)="
|
||||
filteredPersons.results?.length ? onKeydownTab($any($event)) : null
|
||||
"
|
||||
placeholder="Nach Personen suchen..."
|
||||
(keyup.enter)="onEnter()"
|
||||
(keydown.enter)="onEnter($event)"
|
||||
(focus)="onFocus()" />
|
||||
|
||||
<mat-autocomplete #auto="matAutocomplete">
|
||||
<mat-autocomplete
|
||||
#auto="matAutocomplete"
|
||||
(optionSelected)="selectPerson($event.option.value)">
|
||||
@for (person of filteredPersons.results; track person.id) {
|
||||
<mat-option
|
||||
[value]="person.display_name"
|
||||
(click)="selectPerson(person.display_name)">
|
||||
<mat-option [value]="person.display_name">
|
||||
<mat-icon>{{ PERSON_ICON }}</mat-icon>
|
||||
<span>{{ person.display_name }}</span>
|
||||
</mat-option>
|
||||
}
|
||||
|
||||
@if (suggestionsLoading()) {
|
||||
<mat-option disabled>Suche läuft …</mat-option>
|
||||
} @else if (
|
||||
searchControl.value &&
|
||||
searchControl.value.length >= 2 &&
|
||||
!filteredPersons.results?.length
|
||||
) {
|
||||
<mat-option disabled>Keine Personen gefunden</mat-option>
|
||||
}
|
||||
</mat-autocomplete>
|
||||
</label>
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { Component, inject, ViewChild, DestroyRef } from '@angular/core';
|
||||
import {
|
||||
Component,
|
||||
inject,
|
||||
ViewChild,
|
||||
DestroyRef,
|
||||
signal,
|
||||
} from '@angular/core';
|
||||
import { MatIcon } from '@angular/material/icon';
|
||||
import { FormControl, ReactiveFormsModule } from '@angular/forms';
|
||||
import { Router, ActivatedRoute } from '@angular/router';
|
||||
@@ -11,9 +17,11 @@ import { MatInputModule } from '@angular/material/input';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { switchMap, debounceTime, distinctUntilChanged } from 'rxjs/operators';
|
||||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
||||
import { of } from 'rxjs';
|
||||
import { finalize, of } from 'rxjs';
|
||||
import { PersonsService, AuthorsListResponse } from '../../../api';
|
||||
import { PERSON_ICON, SEARCH_ICON } from '../icons';
|
||||
import { CredentialService } from '../../services/credential.service';
|
||||
import { activeInstitutionPersonListQueryParams } from '../../person-list-query-params';
|
||||
|
||||
@Component({
|
||||
selector: 'app-searchbar',
|
||||
@@ -48,12 +56,13 @@ export class AppSearchbarComponent {
|
||||
private readonly router = inject(Router);
|
||||
private readonly route = inject(ActivatedRoute);
|
||||
private readonly personService = inject(PersonsService);
|
||||
constructor() {
|
||||
const route = inject(ActivatedRoute);
|
||||
const router = inject(Router);
|
||||
private readonly credentialService = inject(CredentialService);
|
||||
|
||||
readonly suggestionsLoading = signal(false);
|
||||
|
||||
constructor() {
|
||||
// Sync searchControl from query param on every URL change
|
||||
route.queryParams
|
||||
this.route.queryParams
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe((params) => {
|
||||
const query = params['query'] ?? '';
|
||||
@@ -67,13 +76,17 @@ export class AppSearchbarComponent {
|
||||
.pipe(
|
||||
debounceTime(300),
|
||||
distinctUntilChanged(),
|
||||
switchMap((v) => {
|
||||
const value = v ?? '';
|
||||
if (value.length < 2) return of(this.emptyPersons());
|
||||
return this.personService.searchPersonsAutocomplete(
|
||||
value,
|
||||
'display_name',
|
||||
);
|
||||
switchMap((value) => {
|
||||
const term = (value ?? '').trim();
|
||||
if (term.length < 2) {
|
||||
this.suggestionsLoading.set(false);
|
||||
return of(this.emptyPersons());
|
||||
}
|
||||
|
||||
this.suggestionsLoading.set(true);
|
||||
return this.personService
|
||||
.searchPersonsAutocomplete(term, 'display_name')
|
||||
.pipe(finalize(() => this.suggestionsLoading.set(false)));
|
||||
}),
|
||||
takeUntilDestroyed(this.destroyRef),
|
||||
)
|
||||
@@ -83,22 +96,23 @@ export class AppSearchbarComponent {
|
||||
}
|
||||
|
||||
selectPerson(personName: string | undefined | null): void {
|
||||
if (personName) {
|
||||
this.searchControl.setValue(personName);
|
||||
this.startSearch();
|
||||
}
|
||||
if (!personName) return;
|
||||
|
||||
this.searchControl.setValue(personName, { emitEvent: false });
|
||||
this.startSearch();
|
||||
}
|
||||
|
||||
onKeydownTab(event: KeyboardEvent): void {
|
||||
onEnter(event: Event): void {
|
||||
if (event.defaultPrevented) return;
|
||||
|
||||
// Let Material select the highlighted option; its optionSelected handler
|
||||
// performs the navigation. Only free-text Enter starts a search here.
|
||||
if (this.autocomplete?.isOpen && this.autocompleteTrigger?.activeOption) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
const first = this.filteredPersons?.results?.[0];
|
||||
if (first?.display_name) {
|
||||
this.searchControl.setValue(first.display_name);
|
||||
}
|
||||
}
|
||||
|
||||
onEnter(): void {
|
||||
this.autocompleteTrigger.closePanel();
|
||||
this.autocompleteTrigger?.closePanel();
|
||||
this.startSearch();
|
||||
}
|
||||
|
||||
@@ -107,16 +121,31 @@ export class AppSearchbarComponent {
|
||||
}
|
||||
|
||||
startSearch(): void {
|
||||
const search = this.searchControl.value ?? '';
|
||||
const search = (this.searchControl.value ?? '').trim();
|
||||
const currentPath = this.router.url.split('?')[0];
|
||||
const onPersonList = currentPath.startsWith('/persons');
|
||||
|
||||
this.router.navigate(
|
||||
this.router.url.startsWith('/persons') ? [] : ['/persons'],
|
||||
{
|
||||
relativeTo: this.route,
|
||||
queryParams: { query: search || null, page: 1 },
|
||||
queryParamsHandling: 'merge',
|
||||
// On the person list, keep the active filters. Anywhere else, start from
|
||||
// the account's institution defaults and never leak unrelated params
|
||||
// (sort keys, titles, topics from other sections).
|
||||
const baseParams: Record<string, unknown> = onPersonList
|
||||
? { ...this.router.parseUrl(this.router.url).queryParams }
|
||||
: activeInstitutionPersonListQueryParams(
|
||||
this.credentialService.effectiveInstitutionId(),
|
||||
this.credentialService.organizationName(),
|
||||
);
|
||||
|
||||
delete baseParams['query'];
|
||||
delete baseParams['page'];
|
||||
|
||||
void this.router.navigate(['/persons'], {
|
||||
queryParams: {
|
||||
...baseParams,
|
||||
query: search || null,
|
||||
page: 1,
|
||||
pageSize: baseParams['pageSize'] ?? 10,
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private emptyPersons(): AuthorsListResponse {
|
||||
|
||||
Reference in New Issue
Block a user