Move filter bar to separate component

This commit is contained in:
aditya.chandel
2025-01-20 20:36:15 -07:00
parent c51be158a9
commit 0a6c9827bb
6 changed files with 201 additions and 120 deletions
@@ -1,6 +1,6 @@
<div class="flex flex-row">
<div class="flex flex-col flex-grow">
<div class="book-browser-header flex justify-between items-center p-2 rounded-t-xl mb-4">
<div class="flex justify-between items-center p-2 rounded-t-xl mb-4 bg-[var(--card-background)]">
<div class="flex items-center">
<ng-container *ngIf="entityType$ | async as entityType">
<p *ngIf="entityType !== EntityType.ALL_BOOKS" class="text-2xl">
@@ -18,12 +18,41 @@
<div class="flex items-center gap-8">
<input type="text" pInputText placeholder="Search title or author..." [(ngModel)]="bookTitle" (ngModelChange)="onBookTitleChange($event)"/>
<p-select [options]="sortOptions" optionLabel="label" placeholder="Select Sorting" [(ngModel)]="selectedSort" (onChange)="updateSortOption($event.value)"></p-select>
<div *ngFor="let option of stateOptions" class="field-checkbox">
<p-radioButton [inputId]="option.key" [value]="option.value" [(ngModel)]="value" (onClick)="viewChanged()"></p-radioButton>
<label [for]="option.key" class="ml-2">{{ option.label }}</label>
</div>
<p-button icon="pi pi-heart" [rounded]="true" severity="help" [outlined]="true" (click)="toggleFilters()"/>
<p-select
[options]="sortOptions"
optionLabel="label"
placeholder="Select Sorting"
[(ngModel)]="selectedSort"
(onChange)="updateSortOption($event.value)"/>
<p-button
[icon]="viewIcon"
size="large"
[rounded]="true"
text="true"
pTooltip="Toggle between Grid and Table view"
tooltipPosition="top"
(click)="toggleTableGrid()"/>
<p-button
*ngIf="isFilterActive"
icon="pi pi-filter-slash"
size="large"
[rounded]="true"
text="true"
(click)="clearFilter()"
pTooltip="Clear applied filters"
tooltipPosition="top"/>
<p-button
icon="pi pi-chevron-right"
size="large"
text="true"
[rounded]="true"
[outlined]="true"
(click)="toggleFilterSidebar()"
pTooltip="Toggle filter sidebar"
tooltipPosition="top"/>
</div>
</div>
@@ -32,7 +61,7 @@
<p-progressSpinner></p-progressSpinner>
</div>
<div class="virtual-scroller-container">
<div class="relative">
<div class="no-books-container" *ngIf="bookState?.error">
<p class="no-books-text">
{{ entityType === EntityType.LIBRARY ? "Failed to load library's books!" : "Failed to load shelf's books!" }}
@@ -45,10 +74,10 @@
</div>
<div class="book-card-container" *ngIf="bookState?.books as books">
<app-book-table *ngIf="value === 'table' && books.length > 0" [books]="books" (selectedBooksChange)="onSelectedBooksChange($event)"
<app-book-table *ngIf="gridOrTable === 'table' && books.length > 0" [books]="books" (selectedBooksChange)="onSelectedBooksChange($event)"
[sortOption]="selectedSort"></app-book-table>
<virtual-scroller *ngIf="value === 'grid'" class="virtual-scroller" #scroll [items]="books">
<virtual-scroller *ngIf="gridOrTable === 'grid'" class="virtual-scroller" #scroll [items]="books">
<div class="grid grid-cols-12 gap-4" #container>
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
<app-book-card
@@ -95,45 +124,6 @@
</div>
</div>
<div *ngIf="showFilters" class="w-[225px] ml-4 h-[calc(100vh-6.5rem)] rounded-xl book-browser-filters">
<p class="text-xl p-4">Filters:</p>
<p-accordion value="0">
<p-accordion-panel value="0">
<p-accordion-header>Authors</p-accordion-header>
<p-accordion-content class="h-[35rem] !overflow-y-auto">
<div *ngIf="authorBookCount$ | async as authors">
<div *ngFor="let author of authors">
<p
class="cursor-pointer transition-colors duration-200 ease-in-out"
[ngClass]="{'text-[var(--primary-color)]': activeAuthor === author.author.name}"
(click)="authorClicked(author)">
<p-badge [value]="author.bookCount"></p-badge>
{{ author.author.name }}
</p>
</div>
</div>
</p-accordion-content>
</p-accordion-panel>
<p-accordion-panel value="1">
<p-accordion-header>Genres</p-accordion-header>
<p-accordion-content class="h-[35rem] !overflow-y-auto">
<div *ngIf="categoryBookCount$ | async as categories">
<div *ngFor="let category of categories">
<p
class="cursor-pointer transition-colors duration-200 ease-in-out"
[ngClass]="{'text-[var(--primary-color)]': activeCategory === category.category.name}"
(click)="categoryClicked(category)">
<p-badge [value]="category.bookCount"></p-badge>
{{ category.category.name }}
</p>
</div>
</div>
</p-accordion-content>
</p-accordion-panel>
</p-accordion>
</div>
<app-book-filter></app-book-filter>
</div>
@@ -1,16 +1,3 @@
.book-browser-header {
background-color: var(--card-background);
}
.book-browser-filters {
background-color: var(--card-background);
overflow: hidden;
}
.virtual-scroller-container {
position: relative;
}
.no-books-text {
font-size: 1rem;
color: var(--text-color);
@@ -1,4 +1,4 @@
import {Component, inject, OnInit, ViewChild} from '@angular/core';
import {AfterViewInit, Component, inject, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {MenuItem, MessageService} from 'primeng/api';
import {LibraryService} from '../../service/library.service';
@@ -13,24 +13,23 @@ import {Shelf} from '../../model/shelf.model';
import {SortService} from '../../service/sort.service';
import {SortOption} from '../../model/sort.model';
import {BookState} from '../../model/state/book-state.model';
import {Author, Book, Category} from '../../model/book.model';
import {Book} from '../../model/book.model';
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
import {BookTableComponent} from './book-table/book-table.component';
import {animate, state, style, transition, trigger} from '@angular/animations';
import {MetadataFetchOptionsComponent} from '../../../metadata/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component';
import {MetadataRefreshType} from '../../../metadata/model/request/metadata-refresh-type.enum';
import {Button} from 'primeng/button';
import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common';
import {AsyncPipe, NgForOf, NgIf} from '@angular/common';
import {VirtualScrollerModule} from '@iharbeck/ngx-virtual-scroller';
import {BookCardComponent} from './book-card/book-card.component';
import {ProgressSpinner} from 'primeng/progressspinner';
import {Select} from 'primeng/select';
import {RadioButton} from 'primeng/radiobutton';
import {Menu} from 'primeng/menu';
import {InputText} from 'primeng/inputtext';
import {FormsModule} from '@angular/forms';
import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion';
import {Badge} from 'primeng/badge';
import {BookFilterComponent} from './book-filter/book-filter.component';
import {Tooltip} from 'primeng/tooltip';
export enum EntityType {
LIBRARY = 'Library',
@@ -43,7 +42,7 @@ export enum EntityType {
standalone: true,
templateUrl: './book-browser.component.html',
styleUrls: ['./book-browser.component.scss'],
imports: [Button, NgIf, VirtualScrollerModule, BookCardComponent, AsyncPipe, ProgressSpinner, Select, RadioButton, Menu, NgForOf, InputText, FormsModule, BookTableComponent, AccordionHeader, AccordionContent, AccordionPanel, Accordion, Badge, NgClass],
imports: [Button, NgIf, VirtualScrollerModule, BookCardComponent, AsyncPipe, ProgressSpinner, Select, Menu, NgForOf, InputText, FormsModule, BookTableComponent, BookFilterComponent, Tooltip],
animations: [
trigger('slideInOut', [
state('void', style({
@@ -61,7 +60,7 @@ export enum EntityType {
])
]
})
export class BookBrowserComponent implements OnInit {
export class BookBrowserComponent implements OnInit, AfterViewInit {
bookState$: Observable<BookState> | undefined;
entity$: Observable<Library | Shelf | null> | undefined;
entityType$: Observable<EntityType> | undefined;
@@ -77,13 +76,14 @@ export class BookBrowserComponent implements OnInit {
isDrawerVisible: boolean = false;
dynamicDialogRef: DynamicDialogRef | undefined;
EntityType = EntityType;
showFilters: boolean = false;
stateOptions: any[] = [{label: 'Grid', value: 'grid'}, {label: 'Table', value: 'table'}];
value: string = 'grid';
gridOrTable: string = 'grid';
@ViewChild(BookTableComponent) bookTableComponent!: BookTableComponent;
@ViewChild(BookFilterComponent) bookFilterComponent!: BookFilterComponent;
selectedAuthor = new BehaviorSubject<number | null>(null);
selectedCategory = new BehaviorSubject<number | null>(null);
private activatedRoute = inject(ActivatedRoute);
private messageService = inject(MessageService);
@@ -94,13 +94,6 @@ export class BookBrowserComponent implements OnInit {
private sortService = inject(SortService);
private libraryShelfMenuService = inject(LibraryShelfMenuService);
authorBookCount$: Observable<{ author: Author; bookCount: number; }[]> | undefined = this.bookService.authorBookCount$;
categoryBookCount$: Observable<{ category: Category; bookCount: number; }[]> | undefined = this.bookService.categoryBookCount$;
toggleFilters() {
this.showFilters = !this.showFilters;
}
ngOnInit(): void {
this.bookService.loadBooks();
this.sortOptions = SortService.generateSortOptions();
@@ -141,6 +134,14 @@ export class BookBrowserComponent implements OnInit {
});
}
toggleTableGrid() {
this.gridOrTable = this.gridOrTable === 'grid' ? 'table' : 'grid';
}
get viewIcon(): string {
return this.gridOrTable === 'grid' ? 'pi pi-table' : 'pi pi-objects-column';
}
private getEntityInfoFromRoute(): Observable<{ entityId: number; entityType: EntityType }> {
return this.activatedRoute.paramMap.pipe(
map(params => {
@@ -190,51 +191,21 @@ export class BookBrowserComponent implements OnInit {
private fetchAllBooks(): Observable<BookState> {
return this.bookService.bookState$.pipe(
map(bookState => this.processBookState(bookState)),
switchMap(bookState => this.filterBooks(bookState)),
switchMap(bookState => this.newFilter(bookState))
switchMap(bookState => this.headerFilter(bookState)),
switchMap(bookState => this.sideBarFilter(bookState))
);
}
author = new BehaviorSubject<number | null>(null);
category = new BehaviorSubject<number | null>(null);
activeAuthor: string | null = null;
activeCategory: string | null = null;
authorClicked(author: { author: Author; bookCount: number }) {
this.activeAuthor = author.author.name;
if (this.author.value === author.author.id) {
this.author.next(null);
this.activeAuthor = null;
} else {
this.category.next(null);
this.activeCategory = null;
this.author.next(author.author.id);
}
}
categoryClicked(category: { category: Category; bookCount: number }) {
this.activeCategory = category.category.name;
if (this.category.value === category.category.id) {
this.category.next(null);
this.activeCategory = null;
} else {
this.author.next(null);
this.activeAuthor = null;
this.category.next(category.category.id);
}
}
private newFilter(bookState: BookState): Observable<BookState> {
return combineLatest([this.author, this.category]).pipe(
private sideBarFilter(bookState: BookState): Observable<BookState> {
return combineLatest([this.selectedAuthor, this.selectedCategory]).pipe(
map(([authorId, categoryId]) => {
let filteredBooks = bookState.books || [];
if (authorId !== null) {
filteredBooks = filteredBooks.filter(book =>
book.metadata?.authors.some(author => author.id === authorId)
);
} else if (categoryId !== null) {
}
if (categoryId !== null) {
filteredBooks = filteredBooks.filter(book =>
book.metadata?.categories?.some(category => category.id === categoryId)
);
@@ -244,7 +215,7 @@ export class BookBrowserComponent implements OnInit {
);
}
private filterBooks(bookState: BookState): Observable<BookState> {
private headerFilter(bookState: BookState): Observable<BookState> {
return this.bookOrAuthor$.pipe(
map(title => {
if (title && title.trim() !== '') {
@@ -272,7 +243,7 @@ export class BookBrowserComponent implements OnInit {
}
return bookState;
}),
switchMap(bookState => this.filterBooks(bookState))
switchMap(bookState => this.headerFilter(bookState))
);
}
@@ -413,5 +384,33 @@ export class BookBrowserComponent implements OnInit {
})
}
toggleFilterSidebar() {
this.bookFilterComponent.showFilters = !this.bookFilterComponent.showFilters;
}
get isFilterActive(): boolean {
return this.selectedAuthor.value !== null || this.selectedCategory.value !== null;
}
clearFilter() {
if (this.selectedAuthor.value !== null) {
this.selectedAuthor.next(null);
}
if (this.selectedCategory.value !== null) {
this.selectedCategory.next(null);
}
this.bookFilterComponent.activeCategory = null;
this.bookFilterComponent.activeAuthor = null;
}
ngAfterViewInit() {
this.bookFilterComponent.authorSelected.subscribe((authorId: number) => {
this.selectedAuthor.next(authorId);
});
this.bookFilterComponent.categorySelected.subscribe((categoryId: number) => {
this.selectedCategory.next(categoryId);
});
}
}
@@ -0,0 +1,41 @@
<div *ngIf="showFilters"
class="w-[225px] ml-4 h-[calc(100vh-6.5rem)] rounded-xl bg-[var(--card-background)] overflow-hidden">
<p class="text-xl p-4">Filters:</p>
<p-accordion value="0">
<p-accordion-panel value="0">
<p-accordion-header>Authors</p-accordion-header>
<p-accordion-content class="h-[35rem] !overflow-y-auto">
<div *ngIf="authorBookCount$ | async as authors">
<div *ngFor="let author of authors">
<p
class="cursor-pointer transition-colors duration-200 ease-in-out"
[ngClass]="{'text-[var(--primary-color)]': activeAuthor === author.author.id}"
(click)="authorClicked(author)">
<p-badge [value]="author.bookCount"></p-badge>
{{ author.author.name }}
</p>
</div>
</div>
</p-accordion-content>
</p-accordion-panel>
<p-accordion-panel value="1">
<p-accordion-header>Genres</p-accordion-header>
<p-accordion-content class="h-[35rem] !overflow-y-auto">
<div *ngIf="categoryBookCount$ | async as categories">
<div *ngFor="let category of categories">
<p
class="cursor-pointer transition-colors duration-200 ease-in-out"
[ngClass]="{'text-[var(--primary-color)]': activeCategory === category.category.id}"
(click)="categoryClicked(category)">
<p-badge [value]="category.bookCount"></p-badge>
{{ category.category.name }}
</p>
</div>
</div>
</p-accordion-content>
</p-accordion-panel>
</p-accordion>
</div>
@@ -0,0 +1,64 @@
import {Component, EventEmitter, inject, Input, 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 {Author, Category} from '../../../model/book.model';
import {BookState} from '../../../model/state/book-state.model';
import {map} from 'rxjs/operators';
import {BookService} from '../../../service/book.service';
@Component({
selector: 'app-book-filter',
imports: [
Accordion,
AccordionContent,
AccordionHeader,
AccordionPanel,
AsyncPipe,
Badge,
NgForOf,
NgIf,
NgClass
],
templateUrl: './book-filter.component.html',
styleUrl: './book-filter.component.scss'
})
export class BookFilterComponent {
@Output() authorSelected = new EventEmitter<number | null>();
@Output() categorySelected = new EventEmitter<number | null>();
activeAuthor: number | null = null;
activeCategory: number | null = null;
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;
authorClicked(author: { author: Author; bookCount: number }) {
if (this.activeAuthor === author.author.id) {
this.activeAuthor = null;
} else {
this.activeAuthor = author.author.id;
}
this.activeCategory = null;
this.categorySelected.emit(null);
this.authorSelected.emit(this.activeAuthor);
}
categoryClicked(category: { category: Category; bookCount: number }) {
if (this.activeCategory === category.category.id) {
this.activeCategory = null;
} else {
this.activeCategory = category.category.id;
}
this.activeAuthor = null;
this.authorSelected.emit(null);
this.categorySelected.emit(this.activeCategory);
}
}