From a8848701908814f5048b5d63ea78e9a8aa9cce47 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Thu, 23 Jan 2025 23:24:54 -0700 Subject: [PATCH] - Filtering now works for all items. - Todo: Cleanup ugly code. --- .../book-browser/book-browser.component.html | 8 +- .../book-browser/book-browser.component.ts | 7 +- .../book-filter/book-filter.component.ts | 92 +++++++++++++++++-- .../src/app/book/service/book.service.ts | 50 ---------- 4 files changed, 92 insertions(+), 65 deletions(-) diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html index 9bce8d759..9584323af 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html @@ -138,9 +138,9 @@ (click)="updateMetadata()"> + label="Deselect All" + severity="warn" + (click)="deselectAllBooks()"> @@ -149,6 +149,6 @@ - + diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts index 87004decc..09195d1a3 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts @@ -103,8 +103,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { if (isAllBooksRoute) { this.entityType = EntityType.ALL_BOOKS; this.entityType$ = of(EntityType.ALL_BOOKS); + this.entity$ = of(null); this.bookState$ = this.fetchAllBooks(); - } else { const routeEntityInfo$ = this.getEntityInfoFromRoute(); @@ -132,6 +132,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { this.bookOrAuthor$.next(''); this.bookTitle = ''; this.deselectAllBooks(); + this.clearFilter(); }); } @@ -244,7 +245,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { } return bookState; }), - switchMap(bookState => this.headerFilter(bookState)) + switchMap(bookState => this.headerFilter(bookState)), + switchMap(bookState => this.sideBarFilter(bookState)) ); } @@ -404,7 +406,6 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { this.bookFilterComponent.activeAuthor = null; } - ngAfterViewInit() { this.bookFilterComponent.authorSelected.subscribe((authorId: number) => { this.selectedAuthor.next(authorId); diff --git a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts index f76410b49..9c4d65dfd 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts @@ -1,12 +1,14 @@ -import {Component, EventEmitter, inject, Input, Output} from '@angular/core'; +import {Component, EventEmitter, inject, Input, OnInit, Output} from '@angular/core'; import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion'; import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common'; import {Badge} from 'primeng/badge'; -import {BehaviorSubject, combineLatest, Observable} from 'rxjs'; +import {combineLatest, Observable} from 'rxjs'; import {Author, Category} from '../../../model/book.model'; -import {BookState} from '../../../model/state/book-state.model'; -import {map} from 'rxjs/operators'; +import {distinctUntilChanged, map} from 'rxjs/operators'; import {BookService} from '../../../service/book.service'; +import {Library} from '../../../model/library.model'; +import {Shelf} from '../../../model/shelf.model'; +import {EntityType} from '../book-browser.component'; @Component({ selector: 'app-book-filter', @@ -24,20 +26,94 @@ import {BookService} from '../../../service/book.service'; templateUrl: './book-filter.component.html', styleUrl: './book-filter.component.scss' }) -export class BookFilterComponent { +export class BookFilterComponent implements OnInit { @Output() authorSelected = new EventEmitter(); @Output() categorySelected = new EventEmitter(); + @Input() showFilters: boolean = true; + @Input() entity$!: Observable | undefined; + @Input() entityType$!: Observable | undefined; + activeAuthor: number | null = null; activeCategory: number | null = null; + authorBookCount$!: Observable<{ author: Author; bookCount: number }[]>; + categoryBookCount$!: Observable<{ category: Category; bookCount: number }[]>; + bookService = inject(BookService); - authorBookCount$: Observable<{ author: Author; bookCount: number; }[]> | undefined = this.bookService.authorBookCount$; - categoryBookCount$: Observable<{ category: Category; bookCount: number; }[]> | undefined = this.bookService.categoryBookCount$; - @Input() showFilters: boolean = true; + ngOnInit(): void { + if (this.entity$ && this.entityType$) { + this.authorBookCount$ = combineLatest([this.bookService.bookState$, this.entity$, this.entityType$]).pipe( + map(([state, entity, entityType]) => { + 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(); + 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) => { + 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)) + ); + + this.categoryBookCount$ = combineLatest([this.bookService.bookState$, this.entity$, this.entityType$]).pipe( + map(([state, entity, entityType]) => { + 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 categoryMap = new Map(); + 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)) + ); + } + } authorClicked(author: { author: Author; bookCount: number }) { if (this.activeAuthor === author.author.id) { diff --git a/booklore-ui/src/app/book/service/book.service.ts b/booklore-ui/src/app/book/service/book.service.ts index 5c031b7c4..a5751cc1f 100644 --- a/booklore-ui/src/app/book/service/book.service.ts +++ b/booklore-ui/src/app/book/service/book.service.ts @@ -20,56 +20,6 @@ export class BookService { }); bookState$ = this.bookStateSubject.asObservable(); - authorBookCount$: Observable<{ author: Author; bookCount: number }[]> = this.bookState$.pipe( - map((state) => { - const books = state.books || []; - const authorMap = new Map(); - books.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) => { - 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)) - ); - - categoryBookCount$: Observable<{ category: Category; bookCount: number }[]> = this.bookState$.pipe( - map((state) => { - const books = state.books || []; - const categoryMap = new Map(); - books.forEach((book) => { - book.metadata?.categories.forEach((category) => { - if (!categoryMap.has(category.id)) { - categoryMap.set(category.id, {category, bookCount: 0}); - } - let 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 http = inject(HttpClient);