- Filtering now works for all items.
- Todo: Cleanup ugly code.
This commit is contained in:
@@ -138,9 +138,9 @@
|
||||
(click)="updateMetadata()">
|
||||
</p-button>
|
||||
<p-button
|
||||
label="Deselect All"
|
||||
severity="warn"
|
||||
(click)="deselectAllBooks()">
|
||||
label="Deselect All"
|
||||
severity="warn"
|
||||
(click)="deselectAllBooks()">
|
||||
</p-button>
|
||||
</ng-container>
|
||||
</div>
|
||||
@@ -149,6 +149,6 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<app-book-filter></app-book-filter>
|
||||
<app-book-filter [entity$]="entity$" [entityType$]="entityType$"></app-book-filter>
|
||||
|
||||
</div>
|
||||
|
||||
@@ -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);
|
||||
|
||||
+84
-8
@@ -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<number | null>();
|
||||
@Output() categorySelected = new EventEmitter<number | null>();
|
||||
|
||||
@Input() showFilters: boolean = true;
|
||||
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
|
||||
@Input() entityType$!: Observable<EntityType> | 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<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) => {
|
||||
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<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))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
authorClicked(author: { author: Author; bookCount: number }) {
|
||||
if (this.activeAuthor === author.author.id) {
|
||||
|
||||
@@ -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<number, { author: Author; bookCount: number }>();
|
||||
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<number, { category: Category; bookCount: number }>();
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user