add filter to news
LocationBasedServices/smartguide/pipeline/head There was a failure building this commit

This commit is contained in:
Steven Tischer
2026-06-23 21:49:45 +02:00
parent 9e0fa9c979
commit 4b895dc26f
5 changed files with 206 additions and 3 deletions
@@ -14,16 +14,21 @@
<app-news-list
[articles]="articles"
[showFilters]="true"
[displayedColumns]="displayedColumns"
[total]="total"
[loading]="loading"
[currentPage]="currentPage"
[sortOrder]="sortOrder"
[searchControl]="searchControl"
[initialRelevance]="relevanceValues"
[initialYearFrom]="yearFrom"
[initialYearTo]="yearTo"
extraColumnId="snippet"
extraColumnHeader="Inhalt"
[extraColumnTemplate]="snippetTpl"
(sortChange)="onSortChange($event)"
(pageChange)="onPage($event)">
(pageChange)="onPage($event)"
(filtersChange)="onFiltersChange($event)">
</app-news-list>
</div>
@@ -10,7 +10,9 @@ import { WorksService } from '../../api';
import { PaginationService } from '../../shared/services/pagination.service';
import {
NewsArticleBase,
NewsFilterState,
NewsListComponent,
NEWS_MIN_YEAR,
SortOrder,
} from '../../shared/ui/news-list/news-list.component';
@@ -58,6 +60,13 @@ export class NewsComponent implements OnInit {
currentPage = 1;
sortOrder: SortOrder = DEFAULT_SORT_ORDER;
relevanceValues: string[] = [];
yearFrom: number | null = null;
yearTo: number | null = null;
private readonly minYear = NEWS_MIN_YEAR;
private readonly maxYear = new Date().getFullYear();
ngOnInit(): void {
this.route.queryParamMap
.pipe(takeUntilDestroyed(this.destroyRef))
@@ -67,6 +76,9 @@ export class NewsComponent implements OnInit {
params.get('pageSize'),
);
this.sortOrder = this.parseSortOrder(params.get('sortOrder'));
this.relevanceValues = this.parseRelevance(params.get('relevance'));
this.yearFrom = this.parseYear(params.get('yearFrom'));
this.yearTo = this.parseYear(params.get('yearTo'));
this.searchControl.setValue(params.get('q') ?? '', {
emitEvent: false,
});
@@ -158,6 +170,24 @@ export class NewsComponent implements OnInit {
});
}
onFiltersChange(state: NewsFilterState): void {
const relevance =
state.relevance.length > 0 ? state.relevance.join(',') : null;
const yearFrom = state.yearFrom > this.minYear ? state.yearFrom : null;
const yearTo = state.yearTo < this.maxYear ? state.yearTo : null;
this.router.navigate([], {
relativeTo: this.route,
queryParams: {
relevance,
yearFrom,
yearTo,
page: 1,
},
queryParamsHandling: 'merge',
});
}
private parsePage(value: string | null): number {
const page = Number.parseInt(value ?? '', 10);
if (Number.isNaN(page) || page < 1) {
@@ -180,4 +210,23 @@ export class NewsComponent implements OnInit {
}
return DEFAULT_SORT_ORDER;
}
private parseRelevance(value: string | null): string[] {
if (!value) {
return [];
}
const allowed = ['relevant', 'not_relevant'];
return value
.split(',')
.map((entry) => entry.trim())
.filter((entry) => allowed.includes(entry));
}
private parseYear(value: string | null): number | null {
const year = Number.parseInt(value ?? '', 10);
if (Number.isNaN(year)) {
return null;
}
return year;
}
}
@@ -9,12 +9,47 @@
<mat-form-field appearance="outline" class="sort-field">
<mat-select
[value]="sortOrder"
(selectionChange)="onSortChange($event.value)">
(selectionChange)="onSortChange($event.value)"
placeholder="Sortierung">
<mat-option value="desc">Neueste zuerst</mat-option>
<mat-option value="asc">Älteste zuerst</mat-option>
</mat-select>
</mat-form-field>
@if (showFilters) {
<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>
<div class="year-field">
<div class="year-field__head">
<span class="year-field__label">Datum</span>
<span class="year-field__range">
{{ yearFrom.value }} – {{ yearTo.value }}
</span>
</div>
<mat-slider [min]="minYear" [max]="maxYear" step="1" discrete>
<input matSliderStartThumb [formControl]="yearFrom" />
<input matSliderEndThumb [formControl]="yearTo" />
</mat-slider>
</div>
<button
class="reset-filter-btn"
mat-stroked-button
type="button"
(click)="resetFilters()"
aria-label="Filter zurücksetzen">
<mat-icon>filter_alt_off</mat-icon>
<span>Zurücksetzen</span>
</button>
}
</div>
<div class="table-wrap">
@@ -50,6 +50,59 @@
width: 180px;
}
.relevance-field {
width: 180px;
}
.year-field {
display: flex;
flex-direction: column;
justify-content: center;
min-width: 220px;
padding: 0 6px;
}
.year-field__head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
font-size: 12px;
}
.year-field__label {
color: var(--sg-text-muted);
font-weight: 600;
}
.year-field__range {
color: var(--sg-text);
font-weight: 600;
white-space: nowrap;
}
.year-field mat-slider {
width: 100%;
margin: 0;
}
.reset-filter-btn {
min-width: 132px;
height: 42px;
padding: 0 14px;
border-color: #d9e2f0;
border-radius: var(--sg-radius-sm);
color: var(--sg-text);
background: #fff;
mat-icon {
margin-right: 6px;
width: 20px;
height: 20px;
font-size: 20px;
}
}
.table-wrap {
position: relative;
overflow-x: auto;
@@ -8,7 +8,9 @@ import {
} from '@angular/core';
import { NgTemplateOutlet } from '@angular/common';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { RouterLink } from '@angular/router';
import { merge, debounceTime } from 'rxjs';
import { MatIconModule } from '@angular/material/icon';
import { MatSelectModule } from '@angular/material/select';
import { MatPaginatorModule, PageEvent } from '@angular/material/paginator';
@@ -17,6 +19,8 @@ import { MatTableModule } from '@angular/material/table';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatSliderModule } from '@angular/material/slider';
import { MatButtonModule } from '@angular/material/button';
import { PaginationService } from '../../services/pagination.service';
@@ -31,6 +35,14 @@ export interface NewsArticleBase {
export type SortOrder = 'desc' | 'asc';
export const NEWS_MIN_YEAR = 2000;
export interface NewsFilterState {
relevance: string[];
yearFrom: number;
yearTo: number;
}
@Component({
selector: 'app-news-list',
imports: [
@@ -42,6 +54,8 @@ export type SortOrder = 'desc' | 'asc';
MatProgressSpinnerModule,
MatTableModule,
MatTooltipModule,
MatSliderModule,
MatButtonModule,
NgTemplateOutlet,
ReactiveFormsModule,
RouterLink,
@@ -64,6 +78,30 @@ export class NewsListComponent {
@Input() currentPage = 1;
@Input() sortOrder: SortOrder = 'desc';
@Input({ required: true }) searchControl!: FormControl<string>;
@Input() showFilters = false;
readonly minYear = NEWS_MIN_YEAR;
readonly maxYear = new Date().getFullYear();
readonly relevanceFilter = new FormControl<string[]>([], {
nonNullable: true,
});
readonly yearFrom = new FormControl<number>(this.minYear, {
nonNullable: true,
});
readonly yearTo = new FormControl<number>(this.maxYear, {
nonNullable: true,
});
@Input() set initialRelevance(values: string[] | null) {
this.relevanceFilter.setValue(values ?? [], { emitEvent: false });
}
@Input() set initialYearFrom(year: number | null) {
this.yearFrom.setValue(year ?? this.minYear, { emitEvent: false });
}
@Input() set initialYearTo(year: number | null) {
this.yearTo.setValue(year ?? this.maxYear, { emitEvent: false });
}
@Input() extraColumnId: string | null = null;
@Input() extraColumnHeader = '';
@@ -73,6 +111,23 @@ export class NewsListComponent {
@Output() sortChange = new EventEmitter<SortOrder>();
@Output() pageChange = new EventEmitter<PageEvent>();
@Output() filtersChange = new EventEmitter<NewsFilterState>();
constructor() {
merge(
this.relevanceFilter.valueChanges,
this.yearFrom.valueChanges,
this.yearTo.valueChanges,
)
.pipe(debounceTime(300), takeUntilDestroyed())
.subscribe(() => {
this.filtersChange.emit({
relevance: this.relevanceFilter.value,
yearFrom: this.yearFrom.value,
yearTo: this.yearTo.value,
});
});
}
get isSearching(): boolean {
return this.searchControl.value.trim().length > 0;
@@ -86,6 +141,12 @@ export class NewsListComponent {
this.pageChange.emit(event);
}
resetFilters(): void {
this.relevanceFilter.setValue([]);
this.yearFrom.setValue(this.minYear);
this.yearTo.setValue(this.maxYear);
}
formatDate(dateStr?: string | null): string {
if (!dateStr) {
return '-';