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 c46c7c9ef..a253db5e0 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 @@ -1,6 +1,6 @@
-
+

@@ -18,12 +18,41 @@

- -
- - -
- + + + + + + +
@@ -32,7 +61,7 @@
-
+

{{ entityType === EntityType.LIBRARY ? "Failed to load library's books!" : "Failed to load shelf's books!" }} @@ -45,10 +74,10 @@

- - +
-
-

Filters:

- - - - Authors - -
-
-

- - {{ author.author.name }} -

-
-
-
-
- - - Genres - -
-
-

- - {{ category.category.name }} -

-
-
-
-
-
-
- +
diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.scss b/booklore-ui/src/app/book/components/book-browser/book-browser.component.scss index 0219ca8fe..4fc692c01 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.scss +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.scss @@ -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); 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 d0ab7316a..b2f016960 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 @@ -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 | undefined; entity$: Observable | undefined; entityType$: Observable | 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(null); + selectedCategory = new BehaviorSubject(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 { 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(null); - category = new BehaviorSubject(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 { - return combineLatest([this.author, this.category]).pipe( + private sideBarFilter(bookState: BookState): Observable { + 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 { + private headerFilter(bookState: BookState): Observable { 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); + }); + } } diff --git a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html new file mode 100644 index 000000000..eaa805ddd --- /dev/null +++ b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html @@ -0,0 +1,41 @@ +
+ +

Filters:

+ + + + Authors + +
+
+

+ + {{ author.author.name }} +

+
+
+
+
+ + + Genres + +
+
+

+ + {{ category.category.name }} +

+
+
+
+
+
+
diff --git a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.scss b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.scss new file mode 100644 index 000000000..e69de29bb 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 new file mode 100644 index 000000000..f76410b49 --- /dev/null +++ b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts @@ -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(); + @Output() categorySelected = new EventEmitter(); + + 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); + } + +}