Checkpoint: Add series and awards metadata

This commit is contained in:
aditya.chandel
2025-01-25 16:21:29 -07:00
parent a188a5f795
commit 775f341c7b
19 changed files with 395 additions and 20 deletions
@@ -83,6 +83,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);
selectedAward = new BehaviorSubject<string | null>(null);
private activatedRoute = inject(ActivatedRoute);
private messageService = inject(MessageService);
@@ -212,8 +213,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
}
private sideBarFilter(bookState: BookState): Observable<BookState> {
return combineLatest([this.selectedAuthor, this.selectedCategory, this.selectedPublisher]).pipe(
map(([authorId, categoryId, publisher]) => {
return combineLatest([this.selectedAuthor, this.selectedCategory, this.selectedPublisher, this.selectedAward]).pipe(
map(([authorId, categoryId, publisher, awardName]) => {
let filteredBooks = bookState.books || [];
if (authorId !== null) {
filteredBooks = filteredBooks.filter(book =>
@@ -230,6 +231,11 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
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};
})
);
@@ -321,11 +327,6 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
}
}
viewChanged() {
this.selectedBooks.clear();
this.isDrawerVisible = false;
}
unshelfBooks() {
if (this.entity) {
this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe(
@@ -418,7 +419,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
}
get isFilterActive(): boolean {
return this.selectedAuthor.value !== null || this.selectedCategory.value !== null || this.selectedPublisher.value !== null;
return this.selectedAuthor.value !== null ||
this.selectedCategory.value !== null ||
this.selectedPublisher.value !== null ||
this.selectedAward.value !== null;
}
clearFilter() {
@@ -428,13 +432,16 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
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();
}
@@ -469,5 +476,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
this.bookFilterComponent.publisherSelected.subscribe((publisher: string) => {
this.selectedPublisher.next(publisher);
});
this.bookFilterComponent.awardSelected.subscribe((award: string) => {
this.selectedAward.next(award);
});
}
}
@@ -39,6 +39,23 @@
</p-accordion-panel>
<p-accordion-panel value="2">
<p-accordion-header>Awards</p-accordion-header>
<p-accordion-content class="h-[40rem] !overflow-y-auto">
<div *ngIf="awardBookCount$ | async as awards">
<div *ngFor="let award of awards">
<p
class="cursor-pointer transition-colors duration-200 ease-in-out pb-1"
[ngClass]="{'text-[var(--primary-color)]': activeAward === award.award}"
(click)="awardClicked(award)">
<p-badge [value]="award.bookCount"></p-badge>
{{ award.award }}
</p>
</div>
</div>
</p-accordion-content>
</p-accordion-panel>
<p-accordion-panel value="3">
<p-accordion-header>Publishers</p-accordion-header>
<p-accordion-content class="h-[40rem] !overflow-y-auto">
<div *ngIf="publisherBookCount$ | async as publishers">
@@ -30,6 +30,7 @@ export class BookFilterComponent implements OnInit {
@Output() authorSelected = new EventEmitter<number | null>();
@Output() categorySelected = new EventEmitter<number | null>();
@Output() publisherSelected = new EventEmitter<string | null>();
@Output() awardSelected = new EventEmitter<string | null>();
@Input() showFilters: boolean = true;
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
@@ -38,10 +39,12 @@ export class BookFilterComponent implements OnInit {
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 }[]>;
bookService = inject(BookService);
@@ -50,6 +53,7 @@ export class BookFilterComponent implements OnInit {
this.authorBookCount$ = this.getAuthorBookCountStream();
this.categoryBookCount$ = this.getCategoryBookCountStream();
this.publisherBookCount$ = this.getPublisherBookCountStream();
this.awardBookCount$ = this.getAwardBookCountStream();
}
}
@@ -153,6 +157,41 @@ export class BookFilterComponent implements OnInit {
);
}
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<string, { award: string; bookCount: number }>();
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;
}
});
});
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))
);
}
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);
@@ -172,9 +211,11 @@ export class BookFilterComponent implements OnInit {
this.activeAuthor = author.author.id;
}
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);
}
@@ -185,9 +226,11 @@ export class BookFilterComponent implements OnInit {
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);
}
@@ -198,9 +241,26 @@ export class BookFilterComponent implements OnInit {
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);
}
}