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 63f89134a..39b0f8c41 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 @@ -80,10 +80,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { @ViewChild(BookTableComponent) bookTableComponent!: BookTableComponent; @ViewChild(BookFilterComponent) bookFilterComponent!: BookFilterComponent; - selectedAuthor = new BehaviorSubject(null); - selectedCategory = new BehaviorSubject(null); - selectedPublisher = new BehaviorSubject(null); - selectedAward = new BehaviorSubject(null); + selectedFilter = new BehaviorSubject<{ type: string; value: any } | null>(null); private activatedRoute = inject(ActivatedRoute); private messageService = inject(MessageService); @@ -213,30 +210,44 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { } private sideBarFilter(bookState: BookState): Observable { - return combineLatest([this.selectedAuthor, this.selectedCategory, this.selectedPublisher, this.selectedAward]).pipe( - map(([authorId, categoryId, publisher, awardName]) => { + return this.selectedFilter.pipe( + map(selectedFilter => { + if (!selectedFilter) { + return bookState; + } let filteredBooks = bookState.books || []; - if (authorId !== null) { - filteredBooks = filteredBooks.filter(book => - book.metadata?.authors.some(author => author.id === authorId) - ); + switch (selectedFilter.type) { + case 'author': + filteredBooks = filteredBooks.filter(book => + book.metadata?.authors?.some(author => author.id === selectedFilter.value) + ); + break; + case 'category': + filteredBooks = filteredBooks.filter(book => + book.metadata?.categories?.some(category => category.id === selectedFilter.value) + ); + break; + case 'publisher': + filteredBooks = filteredBooks.filter(book => + book.metadata?.publisher === selectedFilter.value + ); + break; + case 'award': + filteredBooks = filteredBooks.filter(book => + book.metadata?.awards?.some( + award => award.name === selectedFilter.value.name && award.designation === 'WINNER' + ) + ); + break; + case 'series': + filteredBooks = filteredBooks.filter(book => + book.metadata?.seriesName === selectedFilter.value + ); + break; + default: + break; } - if (categoryId !== null) { - filteredBooks = filteredBooks.filter(book => - book.metadata?.categories?.some(category => category.id === categoryId) - ); - } - if (publisher != null) { - filteredBooks = filteredBooks.filter(book => - book.metadata?.publisher === publisher - ); - } - if (awardName !== null) { - filteredBooks = filteredBooks.filter(book => - book.metadata?.awards?.some(award => award.name === awardName && award.designation === 'WINNER') - ); - } - return {...bookState, books: filteredBooks}; + return { ...bookState, books: filteredBooks }; }) ); } @@ -419,29 +430,13 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { } get isFilterActive(): boolean { - return this.selectedAuthor.value !== null || - this.selectedCategory.value !== null || - this.selectedPublisher.value !== null || - this.selectedAward.value !== null; + return this.selectedFilter.value !== null; } clearFilter() { - if (this.selectedAuthor.value !== null) { - this.selectedAuthor.next(null); + if (this.selectedFilter.value !== null) { + this.selectedFilter.next(null); } - if (this.selectedCategory.value !== null) { - this.selectedCategory.next(null); - } - if (this.selectedPublisher.value !== null) { - this.selectedPublisher.next(null); - } - if (this.selectedAward.value !== null) { - this.selectedAward.next(null); - } - this.bookFilterComponent.activeCategory = null; - this.bookFilterComponent.activeAuthor = null; - this.bookFilterComponent.activePublisher = null; - this.bookFilterComponent.activeAward = null; this.clearSearch(); } @@ -465,20 +460,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { } ngAfterViewInit() { - this.bookFilterComponent.authorSelected.subscribe((authorId: number) => { - this.selectedAuthor.next(authorId); - }); - - this.bookFilterComponent.categorySelected.subscribe((categoryId: number) => { - this.selectedCategory.next(categoryId); - }); - - this.bookFilterComponent.publisherSelected.subscribe((publisher: string) => { - this.selectedPublisher.next(publisher); - }); - - this.bookFilterComponent.awardSelected.subscribe((award: string) => { - this.selectedAward.next(award); + this.bookFilterComponent.filterSelected.subscribe((item) => { + this.selectedFilter.next(item); }); } } 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 b53a56971..67628613e 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 @@ -1,71 +1,17 @@ -
- +

Filters:

- - - - Authors + + + {{ filterType | titlecase }} -
-
+
+

- - {{ author.author.name }} -

-
-
- - - - - Genres - -
-
-

- - {{ category.category.name }} -

-
-
-
-
- - - Awards - -
-
-

- - {{ award.award }} -

-
-
-
-
- - - Publishers - -
-
-

- - {{ publisher.publisher }} + [ngClass]="{'text-[var(--primary-color)]': activeFilter.type === filterType && activeFilter.value === (filter.value?.id || filter.value)}" + (click)="handleFilterClick(filterType, filter.value?.id || filter.value)"> + + {{ filter.value.name || filter.value }}

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 b3df46454..df6771d43 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 @@ -1,195 +1,78 @@ import {Component, EventEmitter, inject, Input, OnInit, Output} from '@angular/core'; -import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion'; -import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common'; -import {Badge} from 'primeng/badge'; import {combineLatest, Observable, of} from 'rxjs'; -import {Author, Book, Category} from '../../../model/book.model'; -import {distinctUntilChanged, map} from 'rxjs/operators'; +import {map} from 'rxjs/operators'; import {BookService} from '../../../service/book.service'; import {Library} from '../../../model/library.model'; import {Shelf} from '../../../model/shelf.model'; import {EntityType} from '../book-browser.component'; +import {Book} from '../../../model/book.model'; +import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion'; +import {AsyncPipe, NgClass, NgForOf, NgIf, TitleCasePipe} from '@angular/common'; +import {Badge} from 'primeng/badge'; + + +type Filter = { value: T; bookCount: number }; @Component({ selector: 'app-book-filter', + templateUrl: './book-filter.component.html', + styleUrls: ['./book-filter.component.scss'], imports: [ Accordion, - AccordionContent, - AccordionHeader, AccordionPanel, - AsyncPipe, - Badge, - NgForOf, + AccordionHeader, + AccordionContent, NgIf, - NgClass - ], - templateUrl: './book-filter.component.html', - styleUrl: './book-filter.component.scss' + NgForOf, + NgClass, + Badge, + AsyncPipe, + TitleCasePipe + ] }) export class BookFilterComponent implements OnInit { - @Output() authorSelected = new EventEmitter(); - @Output() categorySelected = new EventEmitter(); - @Output() publisherSelected = new EventEmitter(); - @Output() awardSelected = new EventEmitter(); + @Output() filterSelected = new EventEmitter<{ type: string; value: any } | null>(); @Input() showFilters: boolean = true; @Input() entity$!: Observable | undefined; @Input() entityType$!: Observable | undefined; - activeAuthor: number | null = null; - activeCategory: number | null = null; - activePublisher: string | null = null; - activeAward: string | null = null; - - authorBookCount$!: Observable<{ author: Author; bookCount: number }[]>; - categoryBookCount$!: Observable<{ category: Category; bookCount: number }[]>; - publisherBookCount$!: Observable<{ publisher: string; bookCount: number }[]>; - awardBookCount$!: Observable<{ award: string; bookCount: number }[]>; + activeFilter: { type: string; value: any | null } = {type: '', value: null}; + filterStreams: Record[]>> = {}; bookService = inject(BookService); + ngOnInit(): void { if (this.entity$ && this.entityType$) { - this.authorBookCount$ = this.getAuthorBookCountStream(); - this.categoryBookCount$ = this.getCategoryBookCountStream(); - this.publisherBookCount$ = this.getPublisherBookCountStream(); - this.awardBookCount$ = this.getAwardBookCountStream(); + this.filterStreams = { + author: this.getFilterStream((book) => book.metadata?.authors, 'id', 'name'), + category: this.getFilterStream((book) => book.metadata?.categories, 'id', 'name'), + series: this.getFilterStream((book) => (book.metadata?.seriesName ? [{id: book.metadata.seriesName, name: book.metadata.seriesName}] : []), 'id', 'name'), + award: this.getFilterStream((book) => book.metadata?.awards?.filter((award) => award.designation === 'WINNER'), 'name', 'name'), + publisher: this.getFilterStream((book) => (book.metadata?.publisher ? [{id: book.metadata.publisher, name: book.metadata.publisher}] : []), 'id', 'name'), + }; } } - private getAuthorBookCountStream(): Observable<{ author: Author; 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 authorMap = new Map(); - filteredBooks.forEach((book) => { - book.metadata?.authors.forEach((author) => { - if (!authorMap.has(author.id)) { - authorMap.set(author.id, {author, bookCount: 0}); - } - const authorData = authorMap.get(author.id); - if (authorData) { - authorData.bookCount += 1; - } - }); - }); - - return Array.from(authorMap.values()).sort((a, b) => { - if (b.bookCount !== a.bookCount) { - return b.bookCount - a.bookCount; - } - return a.author.name.localeCompare(b.author.name); - }); - }), - distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr)) - ); - } - - private getCategoryBookCountStream(): Observable<{ category: Category; 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 categoryMap = new Map(); - filteredBooks.forEach((book) => { - book.metadata?.categories.forEach((category) => { - if (!categoryMap.has(category.id)) { - categoryMap.set(category.id, {category, bookCount: 0}); - } - const categoryData = categoryMap.get(category.id); - if (categoryData) { - categoryData.bookCount += 1; - } - }); - }); - - return Array.from(categoryMap.values()).sort((a, b) => { - if (b.bookCount !== a.bookCount) { - return b.bookCount - a.bookCount; - } - return a.category.name.localeCompare(b.category.name); - }); - }), - distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr)) - ); - } - - 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 getAwardBookCountStream(): Observable<{ award: 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 awardMap = new Map(); - filteredBooks.forEach((book) => { - book.metadata?.awards - ?.filter((award) => award.designation === 'WINNER') - .forEach((award) => { - if (!awardMap.has(award.name)) { - awardMap.set(award.name, {award: award.name, bookCount: 0}); - } - const awardData = awardMap.get(award.name); - if (awardData) { - awardData.bookCount += 1; + private getFilterStream(extractor: (book: Book) => T[] | undefined, idKey: keyof T, nameKey: keyof T): Observable[]> { + return combineLatest([this.bookService.bookState$, this.entity$ ?? of(null), this.entityType$ ?? of(EntityType.ALL_BOOKS)]) + .pipe( + map(([state, entity, entityType]) => { + const filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType); + const filterMap = new Map>(); + filteredBooks.forEach((book) => { + (extractor(book) || []).forEach((item) => { + const id = item[idKey]; + if (!filterMap.has(id)) { + filterMap.set(id, {value: item, bookCount: 0}); } + filterMap.get(id)!.bookCount += 1; }); - }); - - return Array.from(awardMap.values()).sort((a, b) => { - if (b.bookCount !== a.bookCount) { - return b.bookCount - a.bookCount; - } - return a.award.localeCompare(b.award); - }); - }), - distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr)) - ); + }); + return Array.from(filterMap.values()).sort((a, b) => b.bookCount - a.bookCount || a.value[nameKey].localeCompare(b.value[nameKey])); + }) + ); } private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] { @@ -197,70 +80,18 @@ export class BookFilterComponent implements OnInit { return books.filter((book) => book.libraryId === entity.id); } if (entityType === EntityType.SHELF && entity && 'id' in entity) { - return books.filter((book) => - book.shelves?.some((shelf) => shelf.id === entity.id) - ); + return books.filter((book) => book.shelves?.some((shelf) => shelf.id === entity.id)); } return books; } - authorClicked(author: { author: Author; bookCount: number }) { - if (this.activeAuthor === author.author.id) { - this.activeAuthor = null; + handleFilterClick(filterType: string, value: any) { + if (this.activeFilter.type === filterType && this.activeFilter.value === value) { + this.activeFilter = {type: '', value: null}; + this.filterSelected.emit(null); } else { - this.activeAuthor = author.author.id; + this.activeFilter = {type: filterType, value}; + this.filterSelected.emit({type: filterType, value}); } - this.activeCategory = null; - this.activeAward = null; - this.activePublisher = null; - this.categorySelected.emit(null); - this.publisherSelected.emit(null); - this.awardSelected.emit(null); - this.authorSelected.emit(this.activeAuthor); - } - - categoryClicked(category: { category: Category; bookCount: number }) { - if (this.activeCategory === category.category.id) { - this.activeCategory = null; - } else { - this.activeCategory = category.category.id; - } - this.activeAuthor = null; - this.activeAward = null; - this.activePublisher = null; - this.authorSelected.emit(null); - this.publisherSelected.emit(null); - this.awardSelected.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.activeAward = null; - this.activeCategory = null; - this.authorSelected.emit(null); - this.categorySelected.emit(null); - this.awardSelected.emit(null); - this.publisherSelected.emit(this.activePublisher); - } - - awardClicked(award: { award: string; bookCount: number }) { - if (this.activeAward === award.award) { - this.activeAward = null; - } else { - this.activeAward = award.award; - } - this.activeAuthor = null; - this.activeCategory = null; - this.activePublisher = null; - this.authorSelected.emit(null); - this.categorySelected.emit(null); - this.publisherSelected.emit(null); - this.awardSelected.emit(this.activeAward); } } diff --git a/booklore-ui/src/app/book/model/book.model.ts b/booklore-ui/src/app/book/model/book.model.ts index 9443ba4f5..001156b00 100644 --- a/booklore-ui/src/app/book/model/book.model.ts +++ b/booklore-ui/src/app/book/model/book.model.ts @@ -21,6 +21,9 @@ export interface BookMetadata { publisher?: string; publishedDate?: string; description?: string; + seriesName?: string; + seriesNumber?: number | null; + seriesTotal?: number | null; isbn13?: string; isbn10?: string; asin?: string; @@ -45,6 +48,9 @@ export interface FetchedMetadata { publisher: string | null; publishedDate: string | null; description: string | null; + seriesName: string | null; + seriesNumber: number | null; + seriesTotal: number | null; isbn13: string | null; isbn10: string | null; asin: string | null;