No need to subscribe for book search.

This commit is contained in:
aditya.chandel
2025-01-23 13:22:56 -07:00
parent 53d394fbbe
commit 0a7140e5aa
3 changed files with 16 additions and 26 deletions
@@ -1,5 +1,5 @@
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
import {Subject, Subscription} from 'rxjs';
import {of, Subject, Subscription} from 'rxjs';
import {catchError, debounceTime, distinctUntilChanged, switchMap} from 'rxjs/operators';
import {Author, Book} from '../../model/book.model';
import {FormsModule} from '@angular/forms';
@@ -44,22 +44,20 @@ export class BookSearcherComponent implements OnInit, OnDestroy {
this.#subscription = this.#searchSubject.pipe(
debounceTime(350),
distinctUntilChanged(),
switchMap((query) => this.bookService.searchBooks(query).pipe(
catchError((error) => {
console.error('Error while searching books:', error);
return [];
})
))
switchMap((query) => {
const result = this.bookService.searchBooks(query);
return of(result);
}),
catchError((error) => {
console.error('Error while searching books:', error);
return of([]);
})
).subscribe({
next: (result: Book[]) => this.books = result,
error: (error) => console.error('Subscription error:', error)
});
}
getLibraryName(libraryId: number): string {
return `${libraryId}`;
}
getAuthorNames(authors: Author[] | undefined): string {
return authors?.map(author => author.name).join(', ') || 'Unknown Author';
}
@@ -73,10 +71,6 @@ export class BookSearcherComponent implements OnInit, OnDestroy {
this.metadataDialogService.openBookMetadataCenterDialog(book.id, 'view');
}
getBookCoverUrl(bookId: number): string {
return this.bookService.getBookCoverUrl(bookId);
}
clearSearch(): void {
this.searchQuery = '';
this.books = [];
@@ -178,17 +178,14 @@ export class BookService {
}
}
searchBooks(query: string): Observable<Book[]> {
searchBooks(query: string): Book[] {
if (query.length < 2) {
return of([]);
return [];
}
return this.bookState$.pipe(
map(state =>
(state.books || []).filter(book =>
(book.metadata?.title?.toLowerCase().includes(query.toLowerCase()) ||
book.metadata?.authors.some(author => author.name.toLowerCase().includes(query.toLowerCase())))
)
)
const state = this.bookStateSubject.value;
return (state.books || []).filter(book =>
book.metadata?.title?.toLowerCase().includes(query.toLowerCase()) ||
book.metadata?.authors.some(author => author.name.toLowerCase().includes(query.toLowerCase()))
);
}
@@ -3,7 +3,7 @@ import {DynamicDialogRef} from 'primeng/dynamicdialog';
import {UtilityService} from './utility.service';
import {Button} from 'primeng/button';
import {TableModule} from 'primeng/table';
import {NgIf, NgStyle} from '@angular/common';
import {NgIf} from '@angular/common';
import {InputText} from 'primeng/inputtext';
@Component({
@@ -13,7 +13,6 @@ import {InputText} from 'primeng/inputtext';
imports: [
Button,
TableModule,
NgStyle,
InputText,
NgIf
],