Bit better
This commit is contained in:
+77
-66
@@ -2,8 +2,8 @@ import {Component, EventEmitter, inject, Input, OnInit, Output} from '@angular/c
|
|||||||
import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion';
|
import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion';
|
||||||
import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common';
|
import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common';
|
||||||
import {Badge} from 'primeng/badge';
|
import {Badge} from 'primeng/badge';
|
||||||
import {combineLatest, Observable} from 'rxjs';
|
import {combineLatest, Observable, of} from 'rxjs';
|
||||||
import {Author, Category} from '../../../model/book.model';
|
import {Author, Book, Category} from '../../../model/book.model';
|
||||||
import {distinctUntilChanged, map} from 'rxjs/operators';
|
import {distinctUntilChanged, map} from 'rxjs/operators';
|
||||||
import {BookService} from '../../../service/book.service';
|
import {BookService} from '../../../service/book.service';
|
||||||
import {Library} from '../../../model/library.model';
|
import {Library} from '../../../model/library.model';
|
||||||
@@ -27,7 +27,6 @@ import {EntityType} from '../book-browser.component';
|
|||||||
styleUrl: './book-filter.component.scss'
|
styleUrl: './book-filter.component.scss'
|
||||||
})
|
})
|
||||||
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>();
|
||||||
|
|
||||||
@@ -43,76 +42,89 @@ export class BookFilterComponent implements OnInit {
|
|||||||
|
|
||||||
bookService = inject(BookService);
|
bookService = inject(BookService);
|
||||||
|
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
if (this.entity$ && this.entityType$) {
|
if (this.entity$ && this.entityType$) {
|
||||||
this.authorBookCount$ = combineLatest([this.bookService.bookState$, this.entity$, this.entityType$]).pipe(
|
this.authorBookCount$ = this.getAuthorBookCountStream();
|
||||||
map(([state, entity, entityType]) => {
|
this.categoryBookCount$ = this.getCategoryBookCountStream();
|
||||||
let filteredBooks = state.books || [];
|
}
|
||||||
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
|
}
|
||||||
filteredBooks = filteredBooks.filter((book) => book.libraryId === entity.id);
|
|
||||||
}
|
|
||||||
if (entityType === EntityType.SHELF && entity && 'id' in entity) {
|
|
||||||
filteredBooks = filteredBooks.filter((book) =>
|
|
||||||
book.shelves?.some((shelf) => shelf.id === entity.id)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const authorMap = new Map<number, { author: Author; bookCount: number }>();
|
|
||||||
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) => {
|
private getAuthorBookCountStream(): Observable<{ author: Author; bookCount: number }[]> {
|
||||||
if (b.bookCount !== a.bookCount) {
|
return combineLatest([
|
||||||
return b.bookCount - a.bookCount;
|
this.bookService.bookState$,
|
||||||
}
|
this.entity$ ?? of(null), // Fallback to an observable emitting null
|
||||||
return a.author.name.localeCompare(b.author.name);
|
this.entityType$ ?? of(EntityType.ALL_BOOKS) // Fallback to a default entity type
|
||||||
});
|
]).pipe(
|
||||||
}),
|
map(([state, entity, entityType]) => {
|
||||||
distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr))
|
let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
|
||||||
);
|
|
||||||
|
|
||||||
this.categoryBookCount$ = combineLatest([this.bookService.bookState$, this.entity$, this.entityType$]).pipe(
|
const authorMap = new Map<number, { author: Author; bookCount: number }>();
|
||||||
map(([state, entity, entityType]) => {
|
filteredBooks.forEach((book) => {
|
||||||
let filteredBooks = state.books || [];
|
book.metadata?.authors.forEach((author) => {
|
||||||
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
|
if (!authorMap.has(author.id)) {
|
||||||
filteredBooks = filteredBooks.filter((book) => book.libraryId === entity.id);
|
authorMap.set(author.id, { author, bookCount: 0 });
|
||||||
}
|
}
|
||||||
if (entityType === EntityType.SHELF && entity && 'id' in entity) {
|
const authorData = authorMap.get(author.id);
|
||||||
filteredBooks = filteredBooks.filter((book) =>
|
if (authorData) {
|
||||||
book.shelves?.some((shelf) => shelf.id === entity.id)
|
authorData.bookCount += 1;
|
||||||
);
|
|
||||||
}
|
|
||||||
const categoryMap = new Map<number, { category: Category; bookCount: number }>();
|
|
||||||
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))
|
|
||||||
|
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), // Fallback to an observable emitting null
|
||||||
|
this.entityType$ ?? of(EntityType.ALL_BOOKS) // Fallback to a default entity type
|
||||||
|
]).pipe(
|
||||||
|
map(([state, entity, entityType]) => {
|
||||||
|
let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
|
||||||
|
|
||||||
|
const categoryMap = new Map<number, { category: Category; bookCount: number }>();
|
||||||
|
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 filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
|
||||||
|
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
authorClicked(author: { author: Author; bookCount: number }) {
|
authorClicked(author: { author: Author; bookCount: number }) {
|
||||||
@@ -136,5 +148,4 @@ export class BookFilterComponent implements OnInit {
|
|||||||
this.authorSelected.emit(null);
|
this.authorSelected.emit(null);
|
||||||
this.categorySelected.emit(this.activeCategory);
|
this.categorySelected.emit(this.activeCategory);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user