Fix filter button not clearing the highlighted text

This commit is contained in:
aditya.chandel
2025-03-09 17:20:41 -06:00
parent 9206a00dd5
commit e5f9776b68
3 changed files with 21 additions and 5 deletions
@@ -155,6 +155,6 @@
</div>
</div>
<app-book-filter [entity$]="entity$" [entityType$]="entityType$"></app-book-filter>
<app-book-filter [entity$]="entity$" [entityType$]="entityType$" [resetFilter$]="resetFilterSubject"></app-book-filter>
</div>
@@ -4,7 +4,7 @@ import {MenuItem, MessageService, PrimeTemplate} from 'primeng/api';
import {LibraryService} from '../../service/library.service';
import {BookService} from '../../service/book.service';
import {map, switchMap} from 'rxjs/operators';
import {BehaviorSubject, combineLatest, Observable, of} from 'rxjs';
import {BehaviorSubject, combineLatest, Observable, of, Subject} from 'rxjs';
import {ShelfService} from '../../service/shelf.service';
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
@@ -83,6 +83,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
selectedFilter = new BehaviorSubject<{ type: string; value: any } | null>(null);
protected userService = inject(UserService);
private activatedRoute = inject(ActivatedRoute);
private messageService = inject(MessageService);
private libraryService = inject(LibraryService);
@@ -90,9 +91,11 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
private shelfService = inject(ShelfService);
private dialogService = inject(DialogService);
private sortService = inject(SortService);
protected userService = inject(UserService);
private libraryShelfMenuService = inject(LibraryShelfMenuService);
protected resetFilterSubject = new Subject<void>();
sortOptions: any[] = [
{label: 'Title', icon: '', field: 'title', command: () => this.sortBooks('title')},
{label: 'Author', icon: '', field: 'author', command: () => this.sortBooks('author')},
@@ -390,6 +393,11 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
clearSearch(): void {
this.bookTitle = '';
this.onBookTitleChange('');
this.resetFilters();
}
resetFilters() {
this.resetFilterSubject.next();
}
openShelfAssigner() {
@@ -1,5 +1,5 @@
import {Component, EventEmitter, inject, Input, OnInit, Output} from '@angular/core';
import {combineLatest, Observable, of} from 'rxjs';
import {combineLatest, Observable, of, Subject} from 'rxjs';
import {map} from 'rxjs/operators';
import {BookService} from '../../../service/book.service';
import {Library} from '../../../model/library.model';
@@ -37,6 +37,7 @@ export class BookFilterComponent implements OnInit {
@Input() showFilters: boolean = true;
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
@Input() entityType$!: Observable<EntityType> | undefined;
@Input() resetFilter$!: Subject<void>;
activeFilter: { type: string; value: any | null } = {type: '', value: null};
filterStreams: Record<string, Observable<Filter<any>[]>> = {};
@@ -44,7 +45,6 @@ export class BookFilterComponent implements OnInit {
bookService = inject(BookService);
ngOnInit(): void {
if (this.entity$ && this.entityType$) {
this.filterStreams = {
@@ -56,6 +56,9 @@ export class BookFilterComponent implements OnInit {
};
this.filterTypes = Object.keys(this.filterStreams);
}
if (this.resetFilter$) {
this.resetFilter$.subscribe(() => this.clearActiveFilter());
}
}
private getFilterStream<T>(extractor: (book: Book) => T[] | undefined, idKey: keyof T, nameKey: keyof T): Observable<Filter<T[keyof T]>[]> {
@@ -97,4 +100,9 @@ export class BookFilterComponent implements OnInit {
this.filterSelected.emit({type: filterType, value});
}
}
clearActiveFilter() {
this.activeFilter = { type: '', value: null };
this.filterSelected.emit(null);
}
}