Bit better

This commit is contained in:
aditya.chandel
2025-01-23 23:28:59 -07:00
parent a884870190
commit 28cec000d8
@@ -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,20 +42,22 @@ 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();
this.categoryBookCount$ = this.getCategoryBookCountStream();
}
}
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
]).pipe(
map(([state, entity, entityType]) => { map(([state, entity, entityType]) => {
let filteredBooks = state.books || []; let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
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 }>(); const authorMap = new Map<number, { author: Author; bookCount: number }>();
filteredBooks.forEach((book) => { filteredBooks.forEach((book) => {
book.metadata?.authors.forEach((author) => { book.metadata?.authors.forEach((author) => {
@@ -79,18 +80,17 @@ export class BookFilterComponent implements OnInit {
}), }),
distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr)) distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr))
); );
}
this.categoryBookCount$ = combineLatest([this.bookService.bookState$, this.entity$, this.entityType$]).pipe( 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]) => { map(([state, entity, entityType]) => {
let filteredBooks = state.books || []; let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
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 categoryMap = new Map<number, { category: Category; bookCount: number }>(); const categoryMap = new Map<number, { category: Category; bookCount: number }>();
filteredBooks.forEach((book) => { filteredBooks.forEach((book) => {
book.metadata?.categories.forEach((category) => { book.metadata?.categories.forEach((category) => {
@@ -103,6 +103,7 @@ export class BookFilterComponent implements OnInit {
} }
}); });
}); });
return Array.from(categoryMap.values()).sort((a, b) => { return Array.from(categoryMap.values()).sort((a, b) => {
if (b.bookCount !== a.bookCount) { if (b.bookCount !== a.bookCount) {
return b.bookCount - a.bookCount; return b.bookCount - a.bookCount;
@@ -113,6 +114,17 @@ export class BookFilterComponent implements OnInit {
distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr)) 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);
} }
} }