124 lines
3.7 KiB
TypeScript
124 lines
3.7 KiB
TypeScript
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';
|
|
|
|
@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<string>();
|
|
|
|
readonly searchControl = new FormControl('', { nonNullable: true });
|
|
readonly topics = signal<string[]>([]);
|
|
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([] as string[]);
|
|
}
|
|
|
|
this.loading.set(true);
|
|
return this.topicsService
|
|
.autocompleteTopics(
|
|
normalizedQuery,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
this.limit(),
|
|
)
|
|
.pipe(
|
|
map((response) =>
|
|
Array.from(
|
|
new Set(
|
|
(response.results ?? [])
|
|
.map((topic) => topic.display_name?.trim() ?? '')
|
|
.filter((topic) => !!topic),
|
|
),
|
|
),
|
|
),
|
|
catchError((error) => {
|
|
console.error(
|
|
'Error fetching topic autocomplete options:',
|
|
error,
|
|
);
|
|
return of([] as string[]);
|
|
}),
|
|
finalize(() => this.loading.set(false)),
|
|
);
|
|
}),
|
|
takeUntilDestroyed(this.destroyRef),
|
|
)
|
|
.subscribe((topics) => {
|
|
this.topics.set(topics);
|
|
});
|
|
}
|
|
|
|
selectTopic(topic: string): void {
|
|
this.searchControl.setValue('', { emitEvent: false });
|
|
this.topics.set([]);
|
|
this.topicSelected.emit(topic);
|
|
}
|
|
onKeydownTab(event: KeyboardEvent): void {
|
|
event.preventDefault();
|
|
this.searchControl.setValue(this.topics()[0], { emitEvent: false });
|
|
}
|
|
submitCurrentValue(): void {
|
|
const topic = this.searchControl.value.trim();
|
|
if (!topic) return;
|
|
this.searchControl.setValue('', { emitEvent: false });
|
|
this.topics.set([]);
|
|
this.topicSelected.emit(topic);
|
|
}
|
|
|
|
clear(): void {
|
|
this.searchControl.setValue('');
|
|
this.topics.set([]);
|
|
}
|
|
}
|