diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts index a7e20ef9b..cc5dda128 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts @@ -82,6 +82,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { selectedAuthor = new BehaviorSubject(null); selectedCategory = new BehaviorSubject(null); + selectedPublisher = new BehaviorSubject(null); private activatedRoute = inject(ActivatedRoute); private messageService = inject(MessageService); @@ -211,8 +212,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { } private sideBarFilter(bookState: BookState): Observable { - return combineLatest([this.selectedAuthor, this.selectedCategory]).pipe( - map(([authorId, categoryId]) => { + return combineLatest([this.selectedAuthor, this.selectedCategory, this.selectedPublisher]).pipe( + map(([authorId, categoryId, publisher]) => { let filteredBooks = bookState.books || []; if (authorId !== null) { filteredBooks = filteredBooks.filter(book => @@ -224,6 +225,11 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { book.metadata?.categories?.some(category => category.id === categoryId) ); } + if (publisher != null) { + filteredBooks = filteredBooks.filter(book => + book.metadata?.publisher === publisher + ); + } return {...bookState, books: filteredBooks}; }) ); @@ -412,7 +418,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { } get isFilterActive(): boolean { - return this.selectedAuthor.value !== null || this.selectedCategory.value !== null; + return this.selectedAuthor.value !== null || this.selectedCategory.value !== null || this.selectedPublisher.value !== null; } clearFilter() { @@ -422,8 +428,13 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { if (this.selectedCategory.value !== null) { this.selectedCategory.next(null); } + + if (this.selectedPublisher.value !== null) { + this.selectedPublisher.next(null); + } this.bookFilterComponent.activeCategory = null; this.bookFilterComponent.activeAuthor = null; + this.bookFilterComponent.activePublisher = null; this.clearSearch(); } @@ -454,5 +465,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { this.bookFilterComponent.categorySelected.subscribe((categoryId: number) => { this.selectedCategory.next(categoryId); }); + + this.bookFilterComponent.publisherSelected.subscribe((publisher: string) => { + this.selectedPublisher.next(publisher); + }); } } diff --git a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html index cdc9d8f0d..9532c25a0 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html +++ b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html @@ -37,5 +37,22 @@ + + + Publishers + +
+
+

+ + {{ publisher.publisher }} +

+
+
+
+
diff --git a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts index f82a95662..7e428a3e4 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts @@ -29,6 +29,7 @@ import {EntityType} from '../book-browser.component'; export class BookFilterComponent implements OnInit { @Output() authorSelected = new EventEmitter(); @Output() categorySelected = new EventEmitter(); + @Output() publisherSelected = new EventEmitter(); @Input() showFilters: boolean = true; @Input() entity$!: Observable | undefined; @@ -36,9 +37,11 @@ export class BookFilterComponent implements OnInit { activeAuthor: number | null = null; activeCategory: number | null = null; + activePublisher: string | null = null; authorBookCount$!: Observable<{ author: Author; bookCount: number }[]>; categoryBookCount$!: Observable<{ category: Category; bookCount: number }[]>; + publisherBookCount$!: Observable<{ publisher: string; bookCount: number }[]>; bookService = inject(BookService); @@ -46,14 +49,15 @@ export class BookFilterComponent implements OnInit { if (this.entity$ && this.entityType$) { this.authorBookCount$ = this.getAuthorBookCountStream(); this.categoryBookCount$ = this.getCategoryBookCountStream(); + this.publisherBookCount$ = this.getPublisherBookCountStream(); } } private getAuthorBookCountStream(): Observable<{ author: Author; bookCount: number }[]> { return combineLatest([ this.bookService.bookState$, - this.entity$ ?? of(null), // Fallback to an observable emitting null - this.entityType$ ?? of(EntityType.ALL_BOOKS) // Fallback to a default entity type + this.entity$ ?? of(null), + this.entityType$ ?? of(EntityType.ALL_BOOKS) ]).pipe( map(([state, entity, entityType]) => { let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType); @@ -62,7 +66,7 @@ export class BookFilterComponent implements OnInit { filteredBooks.forEach((book) => { book.metadata?.authors.forEach((author) => { if (!authorMap.has(author.id)) { - authorMap.set(author.id, { author, bookCount: 0 }); + authorMap.set(author.id, {author, bookCount: 0}); } const authorData = authorMap.get(author.id); if (authorData) { @@ -85,8 +89,8 @@ export class BookFilterComponent implements OnInit { private getCategoryBookCountStream(): Observable<{ category: Category; bookCount: number }[]> { return combineLatest([ this.bookService.bookState$, - this.entity$ ?? of(null), // Fallback to an observable emitting null - this.entityType$ ?? of(EntityType.ALL_BOOKS) // Fallback to a default entity type + this.entity$ ?? of(null), + this.entityType$ ?? of(EntityType.ALL_BOOKS) ]).pipe( map(([state, entity, entityType]) => { let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType); @@ -95,7 +99,7 @@ export class BookFilterComponent implements OnInit { filteredBooks.forEach((book) => { book.metadata?.categories.forEach((category) => { if (!categoryMap.has(category.id)) { - categoryMap.set(category.id, { category, bookCount: 0 }); + categoryMap.set(category.id, {category, bookCount: 0}); } const categoryData = categoryMap.get(category.id); if (categoryData) { @@ -115,6 +119,40 @@ export class BookFilterComponent implements OnInit { ); } + private getPublisherBookCountStream(): Observable<{ publisher: string; bookCount: number }[]> { + return combineLatest([ + this.bookService.bookState$, + this.entity$ ?? of(null), + this.entityType$ ?? of(EntityType.ALL_BOOKS) + ]).pipe( + map(([state, entity, entityType]) => { + let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType); + + const publisherMap = new Map(); + filteredBooks.forEach((book) => { + if (book.metadata?.publisher) { + const publisherName = book.metadata.publisher; + if (!publisherMap.has(publisherName)) { + publisherMap.set(publisherName, {publisher: publisherName, bookCount: 0}); + } + const publisherData = publisherMap.get(publisherName); + if (publisherData) { + publisherData.bookCount += 1; + } + } + }); + + return Array.from(publisherMap.values()).sort((a, b) => { + if (b.bookCount !== a.bookCount) { + return b.bookCount - a.bookCount; + } + return a.publisher.localeCompare(b.publisher); + }); + }), + distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr)) + ); + } + private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] { if (entityType === EntityType.LIBRARY && entity && 'id' in entity) { return books.filter((book) => book.libraryId === entity.id); @@ -134,7 +172,9 @@ export class BookFilterComponent implements OnInit { this.activeAuthor = author.author.id; } this.activeCategory = null; + this.activePublisher = null; this.categorySelected.emit(null); + this.publisherSelected.emit(null); this.authorSelected.emit(this.activeAuthor); } @@ -145,7 +185,22 @@ export class BookFilterComponent implements OnInit { this.activeCategory = category.category.id; } this.activeAuthor = null; + this.activePublisher = null; this.authorSelected.emit(null); + this.publisherSelected.emit(null); this.categorySelected.emit(this.activeCategory); } + + publisherClicked(publisher: { publisher: string; bookCount: number }) { + if (this.activePublisher === publisher.publisher) { + this.activePublisher = null; + } else { + this.activePublisher = publisher.publisher; + } + this.activeAuthor = null; + this.activeCategory = null; + this.authorSelected.emit(null); + this.categorySelected.emit(null); + this.publisherSelected.emit(this.activePublisher); + } }