Add publisher filter

This commit is contained in:
aditya.chandel
2025-01-24 17:57:50 -07:00
parent 42f0d4bdcb
commit a7dcdccd0d
3 changed files with 96 additions and 9 deletions
@@ -82,6 +82,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
selectedAuthor = new BehaviorSubject<number | null>(null); selectedAuthor = new BehaviorSubject<number | null>(null);
selectedCategory = new BehaviorSubject<number | null>(null); selectedCategory = new BehaviorSubject<number | null>(null);
selectedPublisher = new BehaviorSubject<string | null>(null);
private activatedRoute = inject(ActivatedRoute); private activatedRoute = inject(ActivatedRoute);
private messageService = inject(MessageService); private messageService = inject(MessageService);
@@ -211,8 +212,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
} }
private sideBarFilter(bookState: BookState): Observable<BookState> { private sideBarFilter(bookState: BookState): Observable<BookState> {
return combineLatest([this.selectedAuthor, this.selectedCategory]).pipe( return combineLatest([this.selectedAuthor, this.selectedCategory, this.selectedPublisher]).pipe(
map(([authorId, categoryId]) => { map(([authorId, categoryId, publisher]) => {
let filteredBooks = bookState.books || []; let filteredBooks = bookState.books || [];
if (authorId !== null) { if (authorId !== null) {
filteredBooks = filteredBooks.filter(book => filteredBooks = filteredBooks.filter(book =>
@@ -224,6 +225,11 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
book.metadata?.categories?.some(category => category.id === categoryId) book.metadata?.categories?.some(category => category.id === categoryId)
); );
} }
if (publisher != null) {
filteredBooks = filteredBooks.filter(book =>
book.metadata?.publisher === publisher
);
}
return {...bookState, books: filteredBooks}; return {...bookState, books: filteredBooks};
}) })
); );
@@ -412,7 +418,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
} }
get isFilterActive(): boolean { 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() { clearFilter() {
@@ -422,8 +428,13 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
if (this.selectedCategory.value !== null) { if (this.selectedCategory.value !== null) {
this.selectedCategory.next(null); this.selectedCategory.next(null);
} }
if (this.selectedPublisher.value !== null) {
this.selectedPublisher.next(null);
}
this.bookFilterComponent.activeCategory = null; this.bookFilterComponent.activeCategory = null;
this.bookFilterComponent.activeAuthor = null; this.bookFilterComponent.activeAuthor = null;
this.bookFilterComponent.activePublisher = null;
this.clearSearch(); this.clearSearch();
} }
@@ -454,5 +465,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
this.bookFilterComponent.categorySelected.subscribe((categoryId: number) => { this.bookFilterComponent.categorySelected.subscribe((categoryId: number) => {
this.selectedCategory.next(categoryId); this.selectedCategory.next(categoryId);
}); });
this.bookFilterComponent.publisherSelected.subscribe((publisher: string) => {
this.selectedPublisher.next(publisher);
});
} }
} }
@@ -37,5 +37,22 @@
</div> </div>
</p-accordion-content> </p-accordion-content>
</p-accordion-panel> </p-accordion-panel>
<p-accordion-panel value="2">
<p-accordion-header>Publishers</p-accordion-header>
<p-accordion-content class="h-[40rem] !overflow-y-auto">
<div *ngIf="publisherBookCount$ | async as publishers">
<div *ngFor="let publisher of publishers">
<p
class="cursor-pointer transition-colors duration-200 ease-in-out pb-1"
[ngClass]="{'text-[var(--primary-color)]': activePublisher === publisher.publisher}"
(click)="publisherClicked(publisher)">
<p-badge [value]="publisher.bookCount"></p-badge>
{{ publisher.publisher }}
</p>
</div>
</div>
</p-accordion-content>
</p-accordion-panel>
</p-accordion> </p-accordion>
</div> </div>
@@ -29,6 +29,7 @@ import {EntityType} from '../book-browser.component';
export class BookFilterComponent implements OnInit { export class BookFilterComponent implements OnInit {
@Output() authorSelected = new EventEmitter<number | null>(); @Output() authorSelected = new EventEmitter<number | null>();
@Output() categorySelected = new EventEmitter<number | null>(); @Output() categorySelected = new EventEmitter<number | null>();
@Output() publisherSelected = new EventEmitter<string | null>();
@Input() showFilters: boolean = true; @Input() showFilters: boolean = true;
@Input() entity$!: Observable<Library | Shelf | null> | undefined; @Input() entity$!: Observable<Library | Shelf | null> | undefined;
@@ -36,9 +37,11 @@ export class BookFilterComponent implements OnInit {
activeAuthor: number | null = null; activeAuthor: number | null = null;
activeCategory: number | null = null; activeCategory: number | null = null;
activePublisher: string | null = null;
authorBookCount$!: Observable<{ author: Author; bookCount: number }[]>; authorBookCount$!: Observable<{ author: Author; bookCount: number }[]>;
categoryBookCount$!: Observable<{ category: Category; bookCount: number }[]>; categoryBookCount$!: Observable<{ category: Category; bookCount: number }[]>;
publisherBookCount$!: Observable<{ publisher: string; bookCount: number }[]>;
bookService = inject(BookService); bookService = inject(BookService);
@@ -46,14 +49,15 @@ export class BookFilterComponent implements OnInit {
if (this.entity$ && this.entityType$) { if (this.entity$ && this.entityType$) {
this.authorBookCount$ = this.getAuthorBookCountStream(); this.authorBookCount$ = this.getAuthorBookCountStream();
this.categoryBookCount$ = this.getCategoryBookCountStream(); this.categoryBookCount$ = this.getCategoryBookCountStream();
this.publisherBookCount$ = this.getPublisherBookCountStream();
} }
} }
private getAuthorBookCountStream(): Observable<{ author: Author; bookCount: number }[]> { private getAuthorBookCountStream(): Observable<{ author: Author; bookCount: number }[]> {
return combineLatest([ return combineLatest([
this.bookService.bookState$, this.bookService.bookState$,
this.entity$ ?? of(null), // Fallback to an observable emitting null this.entity$ ?? of(null),
this.entityType$ ?? of(EntityType.ALL_BOOKS) // Fallback to a default entity type this.entityType$ ?? of(EntityType.ALL_BOOKS)
]).pipe( ]).pipe(
map(([state, entity, entityType]) => { map(([state, entity, entityType]) => {
let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType); let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
@@ -85,8 +89,8 @@ export class BookFilterComponent implements OnInit {
private getCategoryBookCountStream(): Observable<{ category: Category; bookCount: number }[]> { private getCategoryBookCountStream(): Observable<{ category: Category; bookCount: number }[]> {
return combineLatest([ return combineLatest([
this.bookService.bookState$, this.bookService.bookState$,
this.entity$ ?? of(null), // Fallback to an observable emitting null this.entity$ ?? of(null),
this.entityType$ ?? of(EntityType.ALL_BOOKS) // Fallback to a default entity type this.entityType$ ?? of(EntityType.ALL_BOOKS)
]).pipe( ]).pipe(
map(([state, entity, entityType]) => { map(([state, entity, entityType]) => {
let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType); let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
@@ -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<string, { publisher: string; bookCount: number }>();
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[] { private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) { if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
return books.filter((book) => book.libraryId === entity.id); return books.filter((book) => book.libraryId === entity.id);
@@ -134,7 +172,9 @@ export class BookFilterComponent implements OnInit {
this.activeAuthor = author.author.id; this.activeAuthor = author.author.id;
} }
this.activeCategory = null; this.activeCategory = null;
this.activePublisher = null;
this.categorySelected.emit(null); this.categorySelected.emit(null);
this.publisherSelected.emit(null);
this.authorSelected.emit(this.activeAuthor); this.authorSelected.emit(this.activeAuthor);
} }
@@ -145,7 +185,22 @@ export class BookFilterComponent implements OnInit {
this.activeCategory = category.category.id; this.activeCategory = category.category.id;
} }
this.activeAuthor = null; this.activeAuthor = null;
this.activePublisher = null;
this.authorSelected.emit(null); this.authorSelected.emit(null);
this.publisherSelected.emit(null);
this.categorySelected.emit(this.activeCategory); 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);
}
} }