import { CommonModule } from '@angular/common'; import { Component, DestroyRef, input, output, signal } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { MatAutocompleteModule } from '@angular/material/autocomplete'; import { MatButtonModule } from '@angular/material/button'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatIconModule } from '@angular/material/icon'; import { MatInputModule } from '@angular/material/input'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { TopicsService } from '../../../api/api/topics.service'; import { EventEmitter } from '@angular/core'; import { catchError, debounceTime, distinctUntilChanged, finalize, map, of, switchMap, } from 'rxjs'; import { Topic } from '../../../api/model/topic'; @Component({ selector: 'app-topic-autocomplete', standalone: true, imports: [ CommonModule, ReactiveFormsModule, MatAutocompleteModule, MatButtonModule, MatFormFieldModule, MatIconModule, MatInputModule, MatProgressSpinnerModule, ], templateUrl: './topic-autocomplete.component.html', styleUrl: './topic-autocomplete.component.scss', }) export class TopicAutocompleteComponent { readonly label = input('Thema'); readonly placeholder = input('Thema suchen oder eintippen...'); readonly minCharacters = input(2); readonly limit = input(12); readonly topicSelected = output(); readonly searchControl = new FormControl('', { nonNullable: true }); readonly topics = signal([]); readonly loading = signal(false); constructor( private readonly topicsService: TopicsService, private readonly destroyRef: DestroyRef, ) { this.searchControl.valueChanges .pipe( debounceTime(250), distinctUntilChanged(), switchMap((query) => { const normalizedQuery = query.trim(); if (normalizedQuery.length < this.minCharacters()) { this.loading.set(false); return of([]); } this.loading.set(true); return this.topicsService .listTopics( normalizedQuery, undefined, undefined, undefined, this.limit(), ) .pipe( map(({ results }) => { const seen = new Map(); for (const topic of results ?? []) { if (topic?.id != null) seen.set(String(topic.id), topic); } return Array.from(seen.values()); }), catchError((error) => { console.error( 'Error fetching topic autocomplete options:', error, ); return of([] as Topic[]); }), finalize(() => this.loading.set(false)), ); }), takeUntilDestroyed(this.destroyRef), ) .subscribe((topics) => { this.topics.set(topics); }); } selectTopic(topic: string): void { this.searchControl.setValue('', { emitEvent: false }); const selectedTopic = this.getTopicByDisplayName(topic); this.topics.set([]); if (selectedTopic) { this.topicSelected.emit(selectedTopic); } else { const newTopic: Topic = { display_name: topic, id: null, }; this.topicSelected.emit(newTopic); } } onKeydownTab(event: KeyboardEvent): void { event.preventDefault(); const first = this.topics()[0]; if (!first) return; this.searchControl.setValue(first.display_name ?? '', { emitEvent: false }); } getTopicByDisplayName(displayName: string): Topic | undefined { const normalized = displayName.trim().toLowerCase(); return this.topics().find( (topic) => topic.display_name?.trim().toLowerCase() === normalized, ); } submitCurrentValue(): void { const topic = this.searchControl.value.trim(); if (!topic) return; this.searchControl.setValue('', { emitEvent: false }); this.topics.set([]); const selectedTopic = this.getTopicByDisplayName(topic); if (selectedTopic) { this.topicSelected.emit(selectedTopic); } else { const newTopic: Topic = { display_name: topic, id: null, }; this.topicSelected.emit(newTopic); } } clear(): void { this.searchControl.setValue(''); this.topics.set([]); } }