diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html index fe3ddc210..0db905c40 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html @@ -173,12 +173,12 @@ - +
- @for (filterType of filterTypes; track filterType; let i = $index) { + @for (filterType of filterTypes; track trackByFilterType(i, filterType); let i = $index) { @@ -32,7 +32,7 @@
- @for (filter of filters; track filter) { + @for (filter of filters; track trackByFilter(j, filter); let j = $index) {
@if (searchQuery) { @@ -22,29 +22,6 @@ } - - @if (books.length > 0 || searchQuery) {
(); + #searchSubject = new BehaviorSubject(''); #subscription!: Subscription; private bookService = inject(BookService); private router = inject(Router); protected urlHelper = inject(UrlHelperService); - - isSearchDropdownOpen = false; - - toggleSearchInputDropdown() { - this.isSearchDropdownOpen = !this.isSearchDropdownOpen; - } - - closeSearchDropdown() { - this.isSearchDropdownOpen = false; - } + private headerFilter = new HeaderFilter(this.#searchSubject.asObservable()); ngOnInit(): void { this.initializeSearch(); } initializeSearch(): void { - this.#subscription = this.#searchSubject.pipe( - debounceTime(350), - distinctUntilChanged(), - switchMap((query) => { - const result = this.bookService.searchBooks(query); - return of(result); - }), + this.#subscription = this.bookService.bookState$.pipe( + switchMap(bookState => this.headerFilter.filter(bookState)), catchError((error) => { console.error('Error while searching books:', error); - return of([]); + return of({books: [], loaded: true, error: null}); }) ).subscribe({ - next: (result: Book[]) => this.books = result, + next: (filteredState) => { + this.books = this.searchQuery.trim() ? (filteredState.books || []) : []; + }, error: (error) => console.error('Subscription error:', error) }); } diff --git a/booklore-ui/src/app/book/service/book.service.ts b/booklore-ui/src/app/book/service/book.service.ts index 8a1dcef3f..9c7e3ede7 100644 --- a/booklore-ui/src/app/book/service/book.service.ts +++ b/booklore-ui/src/app/book/service/book.service.ts @@ -191,21 +191,6 @@ export class BookService { this.updateLastReadTime(book.id); } - searchBooks(query: string): Book[] { - if (query.length < 2) { - return []; - } - const normalize = (str: string): string => str.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase(); - const normalizedQuery = normalize(query); - const state = this.bookStateSubject.value; - return (state.books || []).filter(book => { - const title = book.metadata?.title; - const authors = book.metadata?.authors || []; - return (title && normalize(title).includes(normalizedQuery)) || - authors.some(author => normalize(author).includes(normalizedQuery)); - }); - } - getFileContent(bookId: number): Observable { return this.http.get(`${this.url}/${bookId}/content`, {responseType: 'blob' as 'json'}); } @@ -331,7 +316,7 @@ export class BookService { if (currentState.books) { const updatedBooks = currentState.books.map(book => { if (book.id === bookId) { - return { ...book, dateFinished: dateFinished || undefined }; + return {...book, dateFinished: dateFinished || undefined}; } return book; });