No need to subscribe for book search.
This commit is contained in:
@@ -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()))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user