Add publisher filter
This commit is contained in:
@@ -82,6 +82,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
|
||||
selectedAuthor = new BehaviorSubject<number | null>(null);
|
||||
selectedCategory = new BehaviorSubject<number | null>(null);
|
||||
selectedPublisher = new BehaviorSubject<string | null>(null);
|
||||
|
||||
private activatedRoute = inject(ActivatedRoute);
|
||||
private messageService = inject(MessageService);
|
||||
@@ -211,8 +212,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
private sideBarFilter(bookState: BookState): Observable<BookState> {
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -37,5 +37,22 @@
|
||||
</div>
|
||||
</p-accordion-content>
|
||||
</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>
|
||||
</div>
|
||||
|
||||
+61
-6
@@ -29,6 +29,7 @@ import {EntityType} from '../book-browser.component';
|
||||
export class BookFilterComponent implements OnInit {
|
||||
@Output() authorSelected = new EventEmitter<number | null>();
|
||||
@Output() categorySelected = new EventEmitter<number | null>();
|
||||
@Output() publisherSelected = new EventEmitter<string | null>();
|
||||
|
||||
@Input() showFilters: boolean = true;
|
||||
@Input() entity$!: Observable<Library | Shelf | null> | 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<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[] {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user