Basic filtering in book browser component

This commit is contained in:
aditya.chandel
2024-12-27 21:31:01 -07:00
parent d3a94062aa
commit f26190a61e
2 changed files with 132 additions and 112 deletions
@@ -1,17 +1,12 @@
<div class="book-browser-header"> <div class="book-browser-header">
<div class="flex"> <div class="flex align-items-center">
<h4 class="entity-name">{{ (entityType$ | async) }}: {{ (entity$ | async)?.name }}</h4> <h4 class="entity-name">{{ (entityType$ | async) }}: {{ (entity$ | async)?.name }}</h4>
<p-speedDial <p-button icon="pi pi-ellipsis-v" [rounded]="true" [text]="true" severity="info" (click)="entitymenu.toggle($event)"/>
[model]="speedDialItems!" <p-menu #entitymenu [model]="entityOptions" [popup]="true" appendTo="body"></p-menu>
direction="right"
transitionDelay="80"
showIcon="pi pi-ellipsis-v"
buttonClassName="p-button-text p-button-contrast"
hideIcon="pi pi-times">
</p-speedDial>
</div> </div>
<div class="header-right-items"> <div class="header-right-items">
<input type="text" pInputText placeholder="Search by title..." [(ngModel)]="bookTitle" (ngModelChange)="onBookTitleChange($event)"/>
<p-dropdown <p-dropdown
[options]="sortOptions" [options]="sortOptions"
optionLabel="label" optionLabel="label"
@@ -53,5 +48,3 @@
<p-menu #menu [model]="multiSelectItems" [popup]="true" appendTo="body"></p-menu> <p-menu #menu [model]="multiSelectItems" [popup]="true" appendTo="body"></p-menu>
</div> </div>
</div> </div>
@@ -4,7 +4,7 @@ import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
import {LibraryService} from '../../service/library.service'; import {LibraryService} from '../../service/library.service';
import {BookService} from '../../service/book.service'; import {BookService} from '../../service/book.service';
import {map, switchMap} from 'rxjs/operators'; import {map, switchMap} from 'rxjs/operators';
import {Observable, of} from 'rxjs'; import {BehaviorSubject, Observable, of} from 'rxjs';
import {Book} from '../../model/book.model'; import {Book} from '../../model/book.model';
import {ShelfService} from '../../service/shelf.service'; import {ShelfService} from '../../service/shelf.service';
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component'; import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
@@ -25,17 +25,19 @@ export class BooksBrowserComponent implements OnInit {
books$: Observable<Book[]> | undefined; books$: Observable<Book[]> | undefined;
entity$: Observable<Library | Shelf | null> | undefined; entity$: Observable<Library | Shelf | null> | undefined;
entityType$: Observable<string | null> | undefined; entityType$: Observable<string | null> | undefined;
bookTitle$ = new BehaviorSubject<string>('');
entity: Library | Shelf | null = null; entity: Library | Shelf | null = null;
entityType: string = ''; entityType: string = '';
speedDialItems: MenuItem[] | undefined; bookTitle: string = '';
entityOptions: MenuItem[] | undefined;
multiSelectItems: MenuItem[] | undefined; multiSelectItems: MenuItem[] | undefined;
selectedBooks: Set<number> = new Set(); selectedBooks: Set<number> = new Set();
selectedSort: SortOption | null = null; selectedSort: SortOption | null = null;
sortOptions: SortOption[] = []; sortOptions: SortOption[] = [];
ref: DynamicDialogRef | undefined;
isDrawerVisible: boolean = false; isDrawerVisible: boolean = false;
ref: DynamicDialogRef | undefined;
constructor( constructor(
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
@@ -52,11 +54,10 @@ export class BooksBrowserComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
this.sortOptions = SortOptionsHelper.generateSortOptions(); this.sortOptions = SortOptionsHelper.generateSortOptions();
const routeParam$ = this.getRouteParams(); const routeParam$ = this.getRouteParams();
this.books$ = routeParam$.pipe( this.books$ = routeParam$.pipe(
switchMap(({libraryId, shelfId}) => this.fetchBooks(libraryId, shelfId)) switchMap(({libraryId, shelfId}) => this.fetchBooks(libraryId, shelfId)),
); );
this.entity$ = routeParam$.pipe( this.entity$ = routeParam$.pipe(
@@ -72,6 +73,11 @@ export class BooksBrowserComponent implements OnInit {
this.setSelectedSortFromEntity(entity); this.setSelectedSortFromEntity(entity);
}); });
this.activatedRoute.paramMap.subscribe(() => {
this.bookTitle$.next('');
this.bookTitle = '';
});
this.setupMenu(); this.setupMenu();
} }
@@ -110,14 +116,30 @@ export class BooksBrowserComponent implements OnInit {
private fetchBooksByLibrary(libraryId: number): Observable<Book[]> { private fetchBooksByLibrary(libraryId: number): Observable<Book[]> {
return this.bookService.books$.pipe( return this.bookService.books$.pipe(
map(books => books.filter(book => book.libraryId === libraryId)), map(books => books.filter(book => book.libraryId === libraryId)),
map(books => this.sortService.applySort(books, this.selectedSort)) map(books => this.sortService.applySort(books, this.selectedSort)),
switchMap(books => this.bookTitle$.pipe(
map(title => {
if (title && title.trim() !== '') {
return books.filter(book => book.metadata?.title?.toLowerCase().includes(title.toLowerCase()));
}
return books;
})
))
); );
} }
private fetchBooksByShelf(shelfId: number): Observable<Book[]> { private fetchBooksByShelf(shelfId: number): Observable<Book[]> {
return this.bookService.books$.pipe( return this.bookService.books$.pipe(
map(books => books.filter(book => book.shelves?.some(shelf => shelf.id === shelfId))), map(books => books.filter(book => book.shelves?.some(shelf => shelf.id === shelfId))),
map(books => this.sortService.applySort(books, this.selectedSort)) map(books => this.sortService.applySort(books, this.selectedSort)),
switchMap(books => this.bookTitle$.pipe(
map(title => {
if (title && title.trim() !== '') {
return books.filter(book => book.metadata?.title?.toLowerCase().includes(title.toLowerCase()));
}
return books;
})
))
); );
} }
@@ -147,10 +169,6 @@ export class BooksBrowserComponent implements OnInit {
this.isDrawerVisible = false; this.isDrawerVisible = false;
} }
isAnyBookSelected(): boolean {
return this.selectedBooks.size > 0;
}
unshelfBooks() { unshelfBooks() {
if (this.entity) { if (this.entity) {
this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe( this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe(
@@ -187,6 +205,10 @@ export class BooksBrowserComponent implements OnInit {
} }
} }
onBookTitleChange(newTitle: string): void {
this.bookTitle$.next(newTitle);
}
setupMenu(): void { setupMenu(): void {
this.multiSelectItems = [ this.multiSelectItems = [
{ {
@@ -219,110 +241,115 @@ export class BooksBrowserComponent implements OnInit {
} }
private initializeLibraryMenuItems(): void { private initializeLibraryMenuItems(): void {
this.speedDialItems = [ this.entityOptions = [
{ {
icon: 'pi pi-trash', label: 'Options',
tooltipOptions: { items: [
tooltipLabel: 'Delete', {
tooltipPosition: 'top' label: 'Delete Library',
}, icon: 'pi pi-trash',
command: () => { command: () => {
this.confirmationService.confirm({ this.confirmationService.confirm({
message: 'Sure you want to delete library: ' + this.entity?.name + "?", message: 'Sure you want to delete library: ' + this.entity?.name + "?",
header: 'Confirmation', header: 'Confirmation',
icon: 'pi pi-exclamation-triangle', icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none', acceptIcon: 'none',
rejectIcon: 'none', rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text', rejectButtonStyleClass: 'p-button-text',
accept: () => { accept: () => {
this.libraryService.deleteLibrary(this.entity?.id!).subscribe({ this.libraryService.deleteLibrary(this.entity?.id!).subscribe({
complete: () => { complete: () => {
this.router.navigate(['/']); this.router.navigate(['/']);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library was deleted'}); this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library was deleted'});
}, },
error: () => { error: () => {
this.messageService.add({ this.messageService.add({
severity: 'error', severity: 'error',
summary: 'Failed', summary: 'Failed',
detail: 'Failed to delete library', detail: 'Failed to delete library',
life: 3000 life: 3000
});
}
}); });
} }
}); });
} }
}); },
} {
label: 'Refresh Library',
icon: 'pi pi-refresh',
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to refresh library: ' + this.entity?.name + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text',
accept: () => {
this.libraryService.refreshLibrary(this.entity?.id!).subscribe({
complete: () => {
this.messageService.add({
severity: 'info',
summary: 'Success',
detail: 'Library refresh scheduled'
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to refresh library',
life: 3000
});
}
});
}
});
}
}
],
}, },
{
icon: 'pi pi-refresh',
tooltipOptions: {
tooltipLabel: 'Refresh',
tooltipPosition: 'top'
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to refresh library: ' + this.entity?.name + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text',
accept: () => {
this.libraryService.refreshLibrary(this.entity?.id!).subscribe({
complete: () => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library refresh scheduled'});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to refresh library',
life: 3000
});
}
});
}
});
}
}
]; ];
} }
private initializeShelfMenuItems(): void { private initializeShelfMenuItems(): void {
this.speedDialItems = [ this.entityOptions = [
{ {
icon: 'pi pi-trash', label: 'Options',
tooltipOptions: { items: [
tooltipLabel: 'Delete', {
tooltipPosition: 'top' label: 'Delete Shelf',
}, icon: 'pi pi-trash',
command: () => { command: () => {
this.confirmationService.confirm({ this.confirmationService.confirm({
message: 'Sure you want to delete ' + this.entity?.name + "?", message: 'Sure you want to delete ' + this.entity?.name + "?",
header: 'Confirmation', header: 'Confirmation',
icon: 'pi pi-exclamation-triangle', icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none', acceptIcon: 'none',
rejectIcon: 'none', rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text', rejectButtonStyleClass: 'p-button-text',
accept: () => { accept: () => {
this.shelfService.deleteShelf(this.entity?.id!).subscribe({ this.shelfService.deleteShelf(this.entity?.id!).subscribe({
complete: () => { complete: () => {
this.router.navigate(['/']); this.router.navigate(['/']);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf was deleted'}); this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf was deleted'});
}, },
error: () => { error: () => {
this.messageService.add({ this.messageService.add({
severity: 'error', severity: 'error',
summary: 'Failed', summary: 'Failed',
detail: 'Failed to delete shelf', detail: 'Failed to delete shelf',
life: 3000 life: 3000
});
}
}); });
} }
}); });
} }
}); }
} ],
} },
]; ];
} }