diff --git a/alfa-client/libs/vorgang-shared-ui/src/lib/vorgang-search-container/vorgang-search/vorgang-search.formservice.ts b/alfa-client/libs/vorgang-shared-ui/src/lib/vorgang-search-container/vorgang-search/vorgang-search.formservice.ts index fbbefc0fad156294062641d2fcb3e9e0729f13fb..1431bd69f434e06baeca3aff3827aca2fcb86861 100644 --- a/alfa-client/libs/vorgang-shared-ui/src/lib/vorgang-search-container/vorgang-search/vorgang-search.formservice.ts +++ b/alfa-client/libs/vorgang-shared-ui/src/lib/vorgang-search-container/vorgang-search/vorgang-search.formservice.ts @@ -28,7 +28,7 @@ import { Injectable, OnDestroy } from '@angular/core'; import { FormGroup, UntypedFormBuilder, UntypedFormControl } from '@angular/forms'; import { Params } from '@angular/router'; import { isEmpty } from 'lodash-es'; -import { BehaviorSubject, combineLatest, Observable, Subscription } from 'rxjs'; +import { filter, Observable, Subscription } from 'rxjs'; import { debounceTime, distinctUntilChanged, first, tap } from 'rxjs/operators'; @Injectable() @@ -44,7 +44,7 @@ export class VorgangSearchFormService implements OnDestroy { public lastSearchString: string; public hasSearchString: boolean = false; - private lastKeyPressed: BehaviorSubject<string> = new BehaviorSubject<string>(EMPTY_STRING); + private lastKeyPressed: string; constructor( private formBuilder: UntypedFormBuilder, @@ -68,22 +68,26 @@ export class VorgangSearchFormService implements OnDestroy { } subscribeToValueChanges(): void { - this.fromControlSubscription = combineLatest( - this.lastKeyPressed.asObservable(), - this.getSearchFormControl().valueChanges.pipe( + this.fromControlSubscription = this.getSearchFormControl() + .valueChanges.pipe( distinctUntilChanged(), debounceTime(300), tap((value) => this.setHasSearchString(value)), - ), - ).subscribe(([lastKeyPressed, value]) => { - if (lastKeyPressed !== 'Enter') this.handleValueChanges(value); - }); + filter(() => this.lastKeyPressedIsNotEnter()), + ) + .subscribe((value) => { + this.handleValueChanges(value); + }); } setHasSearchString(value: string): void { this.hasSearchString = isNotEmpty(value); } + private lastKeyPressedIsNotEnter(): boolean { + return this.lastKeyPressed !== 'Enter'; + } + handleValueChanges(value: string): void { if (this.shouldSearchForPreview(value)) { this.vorgangListService.setSearchString(value); @@ -171,6 +175,6 @@ export class VorgangSearchFormService implements OnDestroy { } public setLastKeyPressed(key: string): void { - this.lastKeyPressed.next(key); + this.lastKeyPressed = key; } }