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 {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 {catchError, debounceTime, distinctUntilChanged, switchMap} from 'rxjs/operators';
import {Author, Book} from '../../model/book.model'; import {Author, Book} from '../../model/book.model';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
@@ -44,22 +44,20 @@ export class BookSearcherComponent implements OnInit, OnDestroy {
this.#subscription = this.#searchSubject.pipe( this.#subscription = this.#searchSubject.pipe(
debounceTime(350), debounceTime(350),
distinctUntilChanged(), distinctUntilChanged(),
switchMap((query) => this.bookService.searchBooks(query).pipe( switchMap((query) => {
catchError((error) => { const result = this.bookService.searchBooks(query);
console.error('Error while searching books:', error); return of(result);
return []; }),
}) catchError((error) => {
)) console.error('Error while searching books:', error);
return of([]);
})
).subscribe({ ).subscribe({
next: (result: Book[]) => this.books = result, next: (result: Book[]) => this.books = result,
error: (error) => console.error('Subscription error:', error) error: (error) => console.error('Subscription error:', error)
}); });
} }
getLibraryName(libraryId: number): string {
return `${libraryId}`;
}
getAuthorNames(authors: Author[] | undefined): string { getAuthorNames(authors: Author[] | undefined): string {
return authors?.map(author => author.name).join(', ') || 'Unknown Author'; 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'); this.metadataDialogService.openBookMetadataCenterDialog(book.id, 'view');
} }
getBookCoverUrl(bookId: number): string {
return this.bookService.getBookCoverUrl(bookId);
}
clearSearch(): void { clearSearch(): void {
this.searchQuery = ''; this.searchQuery = '';
this.books = []; this.books = [];
@@ -178,17 +178,14 @@ export class BookService {
} }
} }
searchBooks(query: string): Observable<Book[]> { searchBooks(query: string): Book[] {
if (query.length < 2) { if (query.length < 2) {
return of([]); return [];
} }
return this.bookState$.pipe( const state = this.bookStateSubject.value;
map(state => return (state.books || []).filter(book =>
(state.books || []).filter(book => book.metadata?.title?.toLowerCase().includes(query.toLowerCase()) ||
(book.metadata?.title?.toLowerCase().includes(query.toLowerCase()) || book.metadata?.authors.some(author => author.name.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 {UtilityService} from './utility.service';
import {Button} from 'primeng/button'; import {Button} from 'primeng/button';
import {TableModule} from 'primeng/table'; import {TableModule} from 'primeng/table';
import {NgIf, NgStyle} from '@angular/common'; import {NgIf} from '@angular/common';
import {InputText} from 'primeng/inputtext'; import {InputText} from 'primeng/inputtext';
@Component({ @Component({
@@ -13,7 +13,6 @@ import {InputText} from 'primeng/inputtext';
imports: [ imports: [
Button, Button,
TableModule, TableModule,
NgStyle,
InputText, InputText,
NgIf NgIf
], ],