Checkpoint: Switch to using enums in BookBrowserComponent

This commit is contained in:
aditya.chandel
2024-12-30 14:52:03 -07:00
parent 48b6906f41
commit ea272ea1de
4 changed files with 233 additions and 206 deletions
@@ -1,82 +1,105 @@
<div>
<div class="book-browser-header">
<div class="left-items">
<ng-container *ngIf="entityType$ | async as entityType">
<p *ngIf="entityType !== 'all-books'" class="entity-name text-2xl">
{{ entityType }}: {{ (entity$ | async)?.name }}
</p>
<p *ngIf="entityType === 'all-books'" class="entity-name text-2xl">
All Books
</p>
<div *ngIf="entityType !== 'all-books'">
<p-button
icon="pi pi-ellipsis-v"
[rounded]="true"
[text]="true"
severity="info"
(click)="entitymenu.toggle($event)" />
<p-menu #entitymenu [model]="entityOptions" [popup]="true" appendTo="body"></p-menu>
</div>
</ng-container>
</div>
<div class="header-right-items">
<input type="text" pInputText placeholder="Search by title..." [(ngModel)]="bookTitle" (ngModelChange)="onBookTitleChange($event)"/>
<p-dropdown
[options]="sortOptions"
optionLabel="label"
placeholder="Select Sorting"
[(ngModel)]="selectedSort"
(onChange)="updateSortOption($event.value)">
</p-dropdown>
<div class="zoom-controls">
<p-button icon="pi pi-minus" [rounded]="true" [outlined]="true" severity="info"></p-button>
<p-button icon="pi pi-plus" [rounded]="true" [outlined]="true" severity="info"></p-button>
</div>
</div>
</div>
<div *ngIf="!(bookState$ | async)?.loaded && !(bookState$ | async)?.error">
<p-progressSpinner></p-progressSpinner>
</div>
<div class="virtual-scroller-container">
<div class="no-books-container" *ngIf="(bookState$ | async)?.error">
<p class="no-books-text">
{{ entityType === 'Library' ? "Failed to load library's books!" : "Failed to load shelf's books!" }}
<div class="book-browser-header">
<div class="left-items">
<ng-container *ngIf="entityType$ | async as entityType">
<!-- Display entity name for specific entity types -->
<p *ngIf="entityType !== EntityType.ALL_BOOKS" class="entity-name text-2xl">
{{ entityType }}: {{ (entity$ | async)?.name }}
</p>
</div>
<div class="no-books-container" *ngIf="!(bookState$ | async)?.error && (bookState$ | async)?.loaded && (bookState$ | async)?.books?.length === 0">
<p class="no-books-text">
{{ entityType === 'Library' ? "This library has no books!" : "This shelf has no books!" }}
<p *ngIf="entityType === EntityType.ALL_BOOKS" class="entity-name text-2xl">
All Books
</p>
</div>
<div class="book-card-container" *ngIf="(bookState$ | async)?.books as books">
<virtual-scroller *ngIf="books.length > 0" 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
[book]="book"
[isCheckboxEnabled]="true"
[onBookSelect]="handleBookSelect.bind(this)"
[isSelected]="selectedBooks.has(book.id)">
</app-book-card>
</div>
</div>
</virtual-scroller>
</div>
<div class="book-browser-footer" *ngIf="selectedBooks.size > 0">
<div class="footer-items-container">
<ng-container *ngIf="entityType$ | async as entityType">
<p-button *ngIf="entityType === 'Library' || entityType ==='all-books'" icon="pi pi-book" label="Assign Shelf" severity="info" (onClick)="openShelfAssigner()"></p-button>
<p-button *ngIf="entityType === 'Shelf'" icon="pi pi-minus-circle" label="Unshelf" severity="info" (click)="unshelfBooks()"></p-button>
<p-button icon="pi pi-times" label="Deselect" severity="warn" (click)="deselectAllBooks()"></p-button>
</ng-container>
<div *ngIf="entityType !== EntityType.ALL_BOOKS">
<!-- Entity menu for non-'All Books' entity types -->
<p-button
icon="pi pi-ellipsis-v"
[rounded]="true"
[text]="true"
severity="info"
(click)="entitymenu.toggle($event)">
</p-button>
<p-menu #entitymenu [model]="entityOptions" [popup]="true" appendTo="body"></p-menu>
</div>
</div>
</ng-container>
</div>
<div class="header-right-items">
<!-- Book title search input -->
<input type="text" pInputText placeholder="Search by title..." [(ngModel)]="bookTitle" (ngModelChange)="onBookTitleChange($event)"/>
<!-- Sorting dropdown -->
<p-dropdown
[options]="sortOptions"
optionLabel="label"
placeholder="Select Sorting"
[(ngModel)]="selectedSort"
(onChange)="updateSortOption($event.value)">
</p-dropdown>
<!-- Zoom control buttons -->
<div class="zoom-controls">
<p-button icon="pi pi-minus" [rounded]="true" [outlined]="true" severity="info"></p-button>
<p-button icon="pi pi-plus" [rounded]="true" [outlined]="true" severity="info"></p-button>
</div>
</div>
</div>
<!-- Loading spinner -->
<div *ngIf="!(bookState$ | async)?.loaded && !(bookState$ | async)?.error">
<p-progressSpinner></p-progressSpinner>
</div>
<!-- Error or empty state handling -->
<div class="virtual-scroller-container">
<div class="no-books-container" *ngIf="(bookState$ | async)?.error">
<p class="no-books-text">
{{ entityType === EntityType.LIBRARY ? "Failed to load library's books!" : "Failed to load shelf's books!" }}
</p>
</div>
<div class="no-books-container" *ngIf="!(bookState$ | async)?.error && (bookState$ | async)?.loaded && (bookState$ | async)?.books?.length === 0">
<p class="no-books-text">
{{ entityType === EntityType.LIBRARY ? "This library has no books!" : "This shelf has no books!" }}
</p>
</div>
<!-- Book card container -->
<div class="book-card-container" *ngIf="(bookState$ | async)?.books as books">
<virtual-scroller *ngIf="books.length > 0" 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
[book]="book"
[isCheckboxEnabled]="true"
[onBookSelect]="handleBookSelect.bind(this)"
[isSelected]="selectedBooks.has(book.id)">
</app-book-card>
</div>
</div>
</virtual-scroller>
</div>
<!-- Footer with actions based on selected books -->
<div class="book-browser-footer" *ngIf="selectedBooks.size > 0">
<div class="footer-items-container">
<ng-container *ngIf="entityType$ | async as entityType">
<p-button *ngIf="entityType === EntityType.LIBRARY || entityType === EntityType.ALL_BOOKS"
icon="pi pi-book"
label="Assign Shelf"
severity="info"
(onClick)="openShelfAssigner()">
</p-button>
<p-button *ngIf="entityType === EntityType.SHELF"
icon="pi pi-minus-circle"
label="Unshelf"
severity="info"
(click)="unshelfBooks()">
</p-button>
<p-button icon="pi pi-times"
label="Deselect"
severity="warn"
(click)="deselectAllBooks()">
</p-button>
</ng-container>
</div>
</div>
</div>
@@ -15,6 +15,13 @@ import {SortOptionsHelper} from '../../service/sort-options.helper';
import {SortOption} from '../../model/sort.model';
import {BookState} from '../../model/state/book-state.model';
import {Book} from '../../model/book.model';
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
export enum EntityType {
LIBRARY = 'Library',
SHELF = 'Shelf',
ALL_BOOKS = 'All Books'
}
@Component({
selector: 'app-book-browser',
@@ -25,11 +32,11 @@ import {Book} from '../../model/book.model';
export class BookBrowserComponent implements OnInit {
bookState$: Observable<BookState> | undefined;
entity$: Observable<Library | Shelf | null> | undefined;
entityType$: Observable<string | null> | undefined;
entityType$: Observable<EntityType> | undefined;
bookTitle$ = new BehaviorSubject<string>('');
entity: Library | Shelf | null = null;
entityType: string = '';
entityType: EntityType | undefined;
bookTitle: string = '';
entityOptions: MenuItem[] | undefined;
selectedBooks: Set<number> = new Set();
@@ -40,14 +47,13 @@ export class BookBrowserComponent implements OnInit {
constructor(
private activatedRoute: ActivatedRoute,
private router: Router,
private confirmationService: ConfirmationService,
private messageService: MessageService,
private libraryService: LibraryService,
private bookService: BookService,
private shelfService: ShelfService,
private dialogService: DialogService,
private sortService: SortService
private sortService: SortService,
private libraryShelfMenuService: LibraryShelfMenuService
) {
}
@@ -58,11 +64,10 @@ export class BookBrowserComponent implements OnInit {
if (isAllBooksRoute) {
this.entityType$ = this.activatedRoute.url.pipe(
map(segments => {
const fullPath = segments.map(segment => segment.path).join('/');
return fullPath === 'all-books' ? 'all-books' : null;
return EntityType.ALL_BOOKS;
})
);
this.entityType = 'all-books';
this.entityType = EntityType.ALL_BOOKS;
this.bookState$ = this.fetchAllBooks();
} else {
@@ -77,7 +82,7 @@ export class BookBrowserComponent implements OnInit {
);
this.entityType$ = routeParam$.pipe(
map(({libraryId, shelfId}) => libraryId ? 'Library' : shelfId ? 'Shelf' : null)
map(({libraryId, shelfId}) => libraryId ? EntityType.LIBRARY : shelfId ? EntityType.SHELF : EntityType.ALL_BOOKS)
);
this.entity$.subscribe(entity => {
@@ -105,12 +110,12 @@ export class BookBrowserComponent implements OnInit {
private fetchEntity(libraryId: number, shelfId: number): Observable<Library | Shelf | null> {
if (libraryId) {
this.entityType = 'Library';
this.initializeLibraryMenuItems();
this.entityType = EntityType.LIBRARY;
this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity);
return this.fetchLibrary(libraryId);
} else if (shelfId) {
this.entityType = 'Shelf';
this.initializeShelfMenuItems();
this.entityType = EntityType.SHELF;
this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity);
return this.fetchShelf(shelfId);
}
return of(null);
@@ -242,7 +247,7 @@ export class BookBrowserComponent implements OnInit {
updateSortOption(sortOption: SortOption): void {
this.selectedSort = sortOption;
if (this.entityType === 'all-books') {
if (this.entityType === EntityType.ALL_BOOKS) {
this.bookState$ = this.fetchAllBooks();
} else {
const routeParam$ = this.getRouteParams();
@@ -250,9 +255,9 @@ export class BookBrowserComponent implements OnInit {
switchMap(({libraryId, shelfId}) => this.fetchBooksByEntity(libraryId, shelfId))
);
}
if (this.entityType === 'Library') {
if (this.entityType === EntityType.LIBRARY) {
this.libraryService.updateSort(this.entity?.id!, sortOption).subscribe();
} else if (this.entityType === 'Shelf') {
} else if (this.entityType === EntityType.SHELF) {
this.shelfService.updateSort(this.entity?.id!, sortOption).subscribe();
}
}
@@ -293,116 +298,5 @@ export class BookBrowserComponent implements OnInit {
});
}
private initializeLibraryMenuItems(): void {
this.entityOptions = [
{
label: 'Options',
items: [
{
label: 'Delete Library',
icon: 'pi pi-trash',
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete library: ' + this.entity?.name + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
rejectIcon: 'none',
acceptButtonStyleClass: 'p-button-text',
accept: () => {
this.libraryService.deleteLibrary(this.entity?.id!).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library was deleted'});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete library',
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',
acceptButtonStyleClass: '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 {
this.entityOptions = [
{
label: 'Options',
items: [
{
label: 'Delete Shelf',
icon: 'pi pi-trash',
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete ' + this.entity?.name + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
rejectIcon: 'none',
acceptButtonStyleClass: 'p-button-text',
accept: () => {
this.shelfService.deleteShelf(this.entity?.id!).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf was deleted'});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete shelf',
life: 3000
});
}
});
}
});
}
}
],
},
];
}
protected readonly EntityType = EntityType;
}
@@ -17,13 +17,11 @@
.book-title-container {
display: flex;
align-items: center;
justify-content: space-between;
}
.book-title {
font-size: 0.85rem;
color: var(--text-color);
margin-bottom: 6px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
@@ -0,0 +1,112 @@
import { Injectable } from '@angular/core';
import { MenuItem, ConfirmationService, MessageService } from 'primeng/api';
import { Router } from '@angular/router';
import {LibraryService} from './library.service';
import {ShelfService} from './shelf.service';
@Injectable({
providedIn: 'root',
})
export class LibraryShelfMenuService {
constructor(
private confirmationService: ConfirmationService,
private messageService: MessageService,
private libraryService: LibraryService,
private shelfService: ShelfService,
private router: Router
) {}
initializeLibraryMenuItems(entity: any): MenuItem[] {
return [
{
label: 'Options',
items: [
{
label: 'Delete Library',
icon: 'pi pi-trash',
command: () => {
this.confirmationService.confirm({
message: `Are you sure you want to delete library: ${entity?.name}?`,
header: 'Confirmation',
accept: () => {
this.libraryService.deleteLibrary(entity?.id!).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library was deleted'});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete library',
});
}
});
}
});
}
},
{
label: 'Refresh Library',
icon: 'pi pi-refresh',
command: () => {
this.confirmationService.confirm({
message: `Are you sure you want to refresh library: ${entity?.name}?`,
header: 'Confirmation',
accept: () => {
this.libraryService.refreshLibrary(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',
});
}
});
}
});
}
}
]
}
];
}
initializeShelfMenuItems(entity: any): MenuItem[] {
return [
{
label: 'Options',
items: [
{
label: 'Delete Shelf',
icon: 'pi pi-trash',
command: () => {
this.confirmationService.confirm({
message: `Are you sure you want to delete shelf: ${entity?.name}?`,
header: 'Confirmation',
accept: () => {
this.shelfService.deleteShelf(entity?.id!).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf was deleted'});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete shelf',
});
}
});
}
});
}
}
]
}
];
}
}