refactor(keyword-search, news, accounts): use shared selector for news relevance filter and reset to first page on filter change

This commit is contained in:
2026-08-10 16:32:27 +02:00
parent 882ad3b8cf
commit 21f3f49078
5 changed files with 86 additions and 65 deletions
@@ -1,20 +1,19 @@
<div class="content">
<div class="filters">
<mat-form-field appearance="outline" class="relevance-field">
<mat-select
[formControl]="relevanceFilter"
placeholder="Relevanz"
multiple>
<mat-option value="relevant">Relevant</mat-option>
<mat-option value="not_relevant">Nicht relevant</mat-option>
</mat-select>
</mat-form-field>
<app-query-param-selector
class="relevance-field"
[options]="relevanceOptions"
[queryParamKey]="keywordsNewsQueryParamKeys.relevance"
label="Relevanz"
icon="check_circle"
[multiple]="true" />
<app-query-param-selector
class="sort-order-field"
[options]="sortOrderOptions"
[queryParamKey]="keywordsNewsQueryParamKeys.sortOrder"
label="Sortierung" />
label="Sortierung"
icon="sort" />
<app-year-range-slider
[minYear]="minYear"
@@ -13,12 +13,13 @@
align-items: center;
gap: 0.75rem;
.relevance-field {
.relevance-field,
.sort-order-field {
min-width: 200px;
}
.sort-order-field {
min-width: 180px;
::ng-deep .mat-mdc-form-field-icon-prefix {
padding: 0 4px 0 10px;
}
}
@@ -10,13 +10,9 @@ import {
} from '@angular/core';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { debounceTime } from 'rxjs';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatSelectModule } from '@angular/material/select';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatchedTopic, NewsPage, Topic } from '../../../../api';
@@ -38,17 +34,12 @@ import { YearRangeSliderComponent } from '../../../../shared/ui/year-range-slide
type NewsResult = NewsPage['results'][number];
const RELEVANCE_VALUES = ['relevant', 'not_relevant'];
@Component({
selector: 'app-news-results',
imports: [
ReactiveFormsModule,
RouterModule,
MatButtonModule,
MatFormFieldModule,
MatIconModule,
MatSelectModule,
MatTooltipModule,
DataTableComponent,
AppTopicChipComponent,
@@ -81,9 +72,10 @@ export class KeywordsToNewsResultsComponent {
{ value: 'asc', display_name: 'Älteste zuerst' },
];
readonly relevanceFilter = new FormControl<string[]>([], {
nonNullable: true,
});
readonly relevanceOptions: SelectorOption[] = [
{ value: 'relevant', display_name: 'Relevant' },
{ value: 'not_relevant', display_name: 'Nicht relevant' },
];
readonly yearFrom = signal<number>(this.minYear);
readonly yearTo = signal<number>(this.maxYear);
@@ -185,11 +177,6 @@ export class KeywordsToNewsResultsComponent {
this.route.queryParamMap
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((params) => {
this.relevanceFilter.setValue(
this.parseRelevance(params.get(keywordsNewsQueryParamKeys.relevance)),
{ emitEvent: false },
);
this.yearFrom.set(
this.parseYear(params.get(keywordsNewsQueryParamKeys.yearFrom)) ??
this.minYear,
@@ -199,10 +186,6 @@ export class KeywordsToNewsResultsComponent {
this.maxYear,
);
});
this.relevanceFilter.valueChanges
.pipe(debounceTime(300), takeUntilDestroyed(this.destroyRef))
.subscribe(() => this.applyRelevanceFilter());
}
applyYearFilter(): void {
@@ -292,28 +275,6 @@ export class KeywordsToNewsResultsComponent {
});
}
private applyRelevanceFilter(): void {
this.router.navigate([], {
relativeTo: this.route,
queryParams: {
[keywordsNewsQueryParamKeys.relevance]:
this.relevanceFilter.value.length > 0
? this.relevanceFilter.value.join(',')
: null,
[keywordsNewsQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
private parseRelevance(raw: string | null): string[] {
if (!raw) return [];
return raw
.split(',')
.map((entry) => entry.trim())
.filter((entry) => RELEVANCE_VALUES.includes(entry));
}
private parseYear(raw: string | null): number | null {
const year = Number.parseInt(raw ?? '', 10);
return Number.isNaN(year) ? null : year;
@@ -2,9 +2,14 @@
@if (label()) {
<mat-label>{{ label() }}</mat-label>
}
@if (icon()) {
<mat-icon matPrefix [fontIcon]="icon()"></mat-icon>
}
<mat-select
(selectionChange)="_handleSelectionChange($event)"
[value]="_urlValue()">
(openedChange)="_handleOpenedChange($event)"
[multiple]="multiple()"
[value]="_value()">
@for (option of _options(); track $index) {
<mat-option [value]="option.value">{{ option.display_name }}</mat-option>
}
@@ -1,6 +1,14 @@
import { Component, computed, inject, input, output } from '@angular/core';
import {
Component,
computed,
inject,
input,
output,
signal,
} from '@angular/core';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { FormsModule } from '@angular/forms';
import { MatIconModule } from '@angular/material/icon';
import { MatSelectChange, MatSelectModule } from '@angular/material/select';
import { ActivatedRoute, Router } from '@angular/router';
@@ -11,7 +19,7 @@ export type SelectorOption = {
@Component({
selector: 'app-query-param-selector',
imports: [FormsModule, MatSelectModule],
imports: [FormsModule, MatIconModule, MatSelectModule],
templateUrl: './query-param-selector.component.html',
styleUrl: './query-param-selector.component.scss',
})
@@ -21,6 +29,13 @@ export class QueryParamSelectorComponent {
handleSelectionChange = output<MatSelectChange>();
allowEmptyValue = input<boolean>(false);
label = input<string>('');
icon = input<string>('');
multiple = input<boolean>(false);
pageQueryParamKey = input<string | null>('page');
private readonly pendingValues = signal<string[] | null>(null);
_options = computed<SelectorOption[]>(() => {
const options = this.options();
@@ -50,14 +65,54 @@ export class QueryParamSelectorComponent {
return params && key ? params[key] || '' : '';
});
_urlValues = computed<string[]>(() => {
const raw = this._urlValue();
return raw
? raw
.split(',')
.map((value) => value.trim())
.filter(Boolean)
: [];
});
_value = computed<string | string[]>(() =>
this.multiple()
? (this.pendingValues() ?? this._urlValues())
: this._urlValue(),
);
_handleSelectionChange(event: MatSelectChange): void {
const newValue = event.value;
this.router.navigate([], {
queryParams: { [this.queryParamKey()]: newValue },
queryParamsHandling: 'merge',
});
if (this.multiple()) {
this.pendingValues.set(event.value ?? []);
return;
}
this.commit(event.value || null);
this.handleSelectionChange.emit(event);
}
_handleOpenedChange(opened: boolean): void {
if (opened || !this.multiple()) {
return;
}
const pending = this.pendingValues();
this.pendingValues.set(null);
if (pending !== null) {
this.commit(pending.length > 0 ? pending.join(',') : null);
}
}
private commit(value: string | null): void {
const pageKey = this.pageQueryParamKey();
this.router.navigate([], {
queryParams: {
[this.queryParamKey()]: value,
...(pageKey ? { [pageKey]: 0 } : {}),
},
queryParamsHandling: 'merge',
});
}
}