Enhance book card component

This commit is contained in:
aditya.chandel
2024-12-25 13:39:00 -07:00
parent ebcd6cd908
commit cd2207f01e
9 changed files with 106 additions and 109 deletions
@@ -1,5 +1,5 @@
<div class="book-card" <div class="book-card"
[class.selected]="book.selected" [class.selected]="isSelected"
(mouseover)="isHovered = true" (mouseover)="isHovered = true"
(mouseout)="isHovered = false"> (mouseout)="isHovered = false">
<div class="cover-container"> <div class="cover-container">
@@ -9,11 +9,11 @@
alt="Cover of {{ book.metadata?.title }}" alt="Cover of {{ book.metadata?.title }}"
loading="lazy"/> loading="lazy"/>
<p-checkbox <p-checkbox
*ngIf="isHovered || book.selected" *ngIf="isCheckboxEnabled && (isHovered || isSelected)"
[(ngModel)]="book.selected" [(ngModel)]="isSelected"
binary="true" binary="true"
class="select-checkbox" class="select-checkbox"
(ngModelChange)="toggleSelection(book.selected || false)"></p-checkbox> (ngModelChange)="toggleSelection(isSelected || false)"></p-checkbox>
<p-button [rounded]="true" icon="pi pi-info" class="read-btn" (click)="openBookInfo(book)"></p-button> <p-button [rounded]="true" icon="pi pi-info" class="read-btn" (click)="openBookInfo(book)"></p-button>
<p-button [rounded]="true" icon="pi pi-book" class="info-btn" (click)="readBook(book)"></p-button> <p-button [rounded]="true" icon="pi pi-book" class="info-btn" (click)="readBook(book)"></p-button>
</div> </div>
@@ -68,16 +68,14 @@
position: absolute; position: absolute;
top: 8px; top: 8px;
right: 8px; right: 8px;
display: none; /* Hidden by default */ display: none;
z-index: 1; z-index: 1;
} }
/* Show checkbox on hover */
.cover-container:hover .select-checkbox { .cover-container:hover .select-checkbox {
display: block; display: block;
} }
/* Keep checkbox visible for selected books */
.book-card.selected .select-checkbox { .book-card.selected .select-checkbox {
display: block; display: block;
} }
@@ -10,6 +10,7 @@ import { BookService } from '../../service/book.service';
import {CheckboxModule} from 'primeng/checkbox'; import {CheckboxModule} from 'primeng/checkbox';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {NgIf} from '@angular/common'; import {NgIf} from '@angular/common';
import {Observable} from 'rxjs';
@Component({ @Component({
selector: 'app-book-card', selector: 'app-book-card',
@@ -25,18 +26,42 @@ import { NgIf } from '@angular/common';
}) })
export class BookCardComponent implements OnInit { export class BookCardComponent implements OnInit {
@Input() book!: Book; @Input() book!: Book;
@Input() onBookSelect: (book: Book, selected: boolean) => void = () => {}; @Input() isCheckboxEnabled: boolean = false;
@Input() onBookSelect?: (bookId: number, selected: boolean) => void;
@Input() isSelected: boolean = false;
items: MenuItem[] | undefined; items: MenuItem[] | undefined;
isHovered: boolean = false; // Track hover state isHovered: boolean = false;
constructor( constructor(
private bookService: BookService, private bookService: BookService,
private router: Router, private router: Router,
private dialogService: DialogService private dialogService: DialogService
) {} ) {
}
ngOnInit(): void { ngOnInit(): void {
this.initMenu();
}
coverImageSrc(book: Book): string {
return this.bookService.getBookCoverUrl(book.id);
}
readBook(book: Book): void {
this.bookService.readBook(book);
}
toggleSelection(selected: boolean): void {
if (this.isCheckboxEnabled) {
this.isSelected = selected;
if (this.onBookSelect) {
this.onBookSelect(this.book.id, selected);
}
}
}
private initMenu() {
this.items = [ this.items = [
{ {
label: 'Options', label: 'Options',
@@ -56,27 +81,6 @@ export class BookCardComponent implements OnInit {
]; ];
} }
coverImageSrc(book: Book): string {
return this.bookService.getBookCoverUrl(book.id);
}
readBook(book: Book): void {
this.bookService.readBook(book);
}
openBookInfo(book: Book): void {
this.router.navigate(['/library', book.libraryId, 'book', book.id, 'info']);
}
toggleSelection(selected: boolean): void {
this.book.selected = selected;
this.onBookSelect(this.book, selected); // Notify the parent component
}
shouldShowCheckbox(): boolean {
return this.book.selected || this.isHovered;
}
private openShelfDialog(book: Book): void { private openShelfDialog(book: Book): void {
this.dialogService.open(ShelfAssignerComponent, { this.dialogService.open(ShelfAssignerComponent, {
header: `Update Shelves: ${book.metadata?.title}`, header: `Update Shelves: ${book.metadata?.title}`,
@@ -90,4 +94,8 @@ export class BookCardComponent implements OnInit {
}, },
}); });
} }
openBookInfo(book: Book): void {
this.router.navigate(['/library', book.libraryId, 'book', book.id, 'info']);
}
} }
@@ -13,9 +13,12 @@
<div *ngIf="isAnyBookSelected()"> <div *ngIf="isAnyBookSelected()">
<div class="button-container"> <div class="button-container">
<p-button *ngIf="(entityType$ | async) === 'Library'" icon="pi pi-ellipsis-v" label="Options" severity="primary" [rounded]="true" [outlined]="true" (click)="menu.toggle($event)"></p-button> <p-button *ngIf="(entityType$ | async) === 'Library'" icon="pi pi-ellipsis-v" label="Options" severity="primary"
<p-button *ngIf="(entityType$ | async) === 'Shelf'" icon="pi pi-minus-circle" label="Unshelf" severity="primary" [rounded]="true" [outlined]="true" (click)="unshelfBooks()"></p-button> [rounded]="true" [outlined]="true" (click)="menu.toggle($event)"></p-button>
<p-button icon="pi pi-times" label="Deselect" severity="warning" [rounded]="true" [outlined]="true" (click)="deselectAllBooks()"></p-button> <p-button *ngIf="(entityType$ | async) === 'Shelf'" icon="pi pi-minus-circle" label="Unshelf" severity="primary"
[rounded]="true" [outlined]="true" (click)="unshelfBooks()"></p-button>
<p-button icon="pi pi-times" label="Deselect" severity="warning" [rounded]="true" [outlined]="true"
(click)="deselectAllBooks()"></p-button>
</div> </div>
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu> <p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
</div> </div>
@@ -36,7 +39,9 @@
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems"> <div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
<app-book-card <app-book-card
[book]="book" [book]="book"
[onBookSelect]="handleBookSelect.bind(this)"> [isCheckboxEnabled]="true"
[onBookSelect]="handleBookSelect.bind(this)"
[isSelected]="selectedBooks.has(book.id)">
</app-book-card> </app-book-card>
</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 {Observable, of, Subject} from 'rxjs';
import {Book} from '../../model/book.model'; import {Book} from '../../model/book.model';
import {Library} from '../../model/library.model'; import {Library} from '../../model/library.model';
import {Shelf} from '../../model/book.model'; import {Shelf} from '../../model/book.model';
@@ -24,8 +24,9 @@ export class BooksBrowserComponent implements OnInit {
entityType$: Observable<string | null> | undefined; entityType$: Observable<string | null> | undefined;
items: MenuItem[] | undefined; items: MenuItem[] | undefined;
selectedBooks: Set<number> = new Set(); selectedBooks: Set<number> = new Set();
book: Book | undefined;
entity: Library | Shelf | null = null; entity: Library | Shelf | null = null;
private deselectAllSubject = new Subject<void>(); // Subject to notify deselection
deselectAll$ = this.deselectAllSubject.asObservable();
constructor( constructor(
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
@@ -96,14 +97,12 @@ export class BooksBrowserComponent implements OnInit {
} }
handleBookSelect(book: Book, selected: boolean): void { handleBookSelect(bookId: number, selected: boolean): void {
if (selected) { if (selected) {
this.selectedBooks.add(book.id); this.selectedBooks.add(bookId);
this.book = book; // Set the selected book
this.setupMenu(); this.setupMenu();
} else { } else {
this.selectedBooks.delete(book.id); this.selectedBooks.delete(bookId);
this.book = undefined; // Clear the selected book
} }
} }
@@ -124,9 +123,6 @@ export class BooksBrowserComponent implements OnInit {
deselectAllBooks(): void { deselectAllBooks(): void {
this.selectedBooks.clear(); this.selectedBooks.clear();
this.books$?.subscribe(books => {
books.forEach(book => book.selected = false);
});
} }
isAnyBookSelected(): boolean { isAnyBookSelected(): boolean {
@@ -143,7 +139,6 @@ export class BooksBrowserComponent implements OnInit {
baseZIndex: 10, baseZIndex: 10,
data: { data: {
isMultiBooks: true, isMultiBooks: true,
book: this.book,
bookIds: this.selectedBooks bookIds: this.selectedBooks
}, },
}); });
@@ -151,7 +146,7 @@ export class BooksBrowserComponent implements OnInit {
unshelfBooks() { unshelfBooks() {
if (this.entity) { if (this.entity) {
this.bookService.assignShelvesToBook(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe( this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe(
() => { () => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'}); this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
this.selectedBooks = new Set<number>(); this.selectedBooks = new Set<number>();
@@ -13,7 +13,7 @@
#scrollContainer #scrollContainer
[horizontal]="true"> [horizontal]="true">
<div class="book-item" *ngFor="let book of books$ | async"> <div class="book-item" *ngFor="let book of books$ | async">
<app-book-card [book]="book"></app-book-card> <app-book-card [book]="book" [isCheckboxEnabled]="false"></app-book-card>
</div> </div>
</div> </div>
</div> </div>
@@ -5,16 +5,15 @@ import {MessageService} from 'primeng/api';
import {ShelfService} from '../../service/shelf.service'; import {ShelfService} from '../../service/shelf.service';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {BookService} from '../../service/book.service'; import {BookService} from '../../service/book.service';
import {map, tap} from 'rxjs/operators';
@Component({ @Component({
selector: 'app-shelf-assigner', selector: 'app-shelf-assigner',
standalone: false, standalone: false,
templateUrl: './shelf-assigner.component.html', templateUrl: './shelf-assigner.component.html',
styleUrl: './shelf-assigner.component.scss' styleUrls: ['./shelf-assigner.component.scss']
}) })
export class ShelfAssignerComponent implements OnInit { export class ShelfAssignerComponent implements OnInit {
shelves$: Observable<Shelf[]>; shelves$: Observable<Shelf[]>;
book: Book; book: Book;
selectedShelves: Shelf[] = []; selectedShelves: Shelf[] = [];
@@ -24,9 +23,12 @@ export class ShelfAssignerComponent implements OnInit {
isMultiBooks: boolean = false; isMultiBooks: boolean = false;
constructor( constructor(
private shelfService: ShelfService, private dynamicDialogConfig: DynamicDialogConfig, private shelfService: ShelfService,
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService, private dynamicDialogConfig: DynamicDialogConfig,
private bookService: BookService) { private dynamicDialogRef: DynamicDialogRef,
private messageService: MessageService,
private bookService: BookService
) {
this.shelves$ = this.shelfService.shelves$; this.shelves$ = this.shelfService.shelves$;
this.book = this.dynamicDialogConfig.data.book; this.book = this.dynamicDialogConfig.data.book;
this.bookIds = this.dynamicDialogConfig.data.bookIds; this.bookIds = this.dynamicDialogConfig.data.bookIds;
@@ -35,63 +37,53 @@ export class ShelfAssignerComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
if (!this.isMultiBooks) { if (!this.isMultiBooks) {
this.shelves$.subscribe(shelves => { this.shelves$.pipe(
if (this.book.shelves) { map(shelves => shelves.filter(shelf => this.book.shelves?.some(bShelf => bShelf.id === shelf.id))),
const bookShelfIds = this.book.shelves.map(shelf => shelf.id); tap(selectedShelves => this.selectedShelves = selectedShelves)
this.selectedShelves = shelves.filter(shelf => bookShelfIds.includes(shelf.id)); ).subscribe();
}
});
} }
} }
saveNewShelf() { saveNewShelf(): void {
this.shelfService.createShelf(this.shelfName).subscribe( this.shelfService.createShelf(this.shelfName).subscribe(
() => { () => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf created: ' + this.shelfName}); this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf created: ' + this.shelfName});
this.displayShelfDialog = false; this.displayShelfDialog = false;
}, },
(error) => { error => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to create shelf'}); this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to create shelf'});
console.error('Error creating shelf:', error); console.error('Error creating shelf:', error);
} }
); );
} }
updateBooksShelves() { updateBooksShelves(): void {
if (!this.isMultiBooks) { const idsToAssign: Set<number | undefined> = new Set(this.selectedShelves.map(shelf => shelf.id));
const shelfIds: (number | undefined)[] = this.selectedShelves.map((shelf) => shelf.id); const idsToUnassign: Set<number> = this.isMultiBooks ? new Set() : this.getIdsToUnAssign(this.book, idsToAssign);
this.bookService.assignShelvesToBook(new Set([this.book.id]), new Set(shelfIds), this.getIdsToUnAssign(this.book, new Set(shelfIds))).subscribe( const bookIds = this.isMultiBooks ? this.bookIds : new Set([this.book.id]);
() => { this.updateBookShelves(bookIds, idsToAssign, idsToUnassign);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
this.dynamicDialogRef.close();
},
(error) => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book\'s shelves'});
this.dynamicDialogRef.close();
}
);
} else {
const shelfIds: (number | undefined)[] = this.selectedShelves.map((shelf) => shelf.id);
this.bookService.assignShelvesToBook(this.bookIds, new Set(shelfIds), new Set()).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
this.dynamicDialogRef.close();
},
(error) => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book\'s shelves'});
this.dynamicDialogRef.close();
}
);
}
} }
getIdsToUnAssign(book: Book, idsToAssign: Set<number | undefined>): Set<number | undefined> { private updateBookShelves(bookIds: Set<number>, idsToAssign: Set<number | undefined>, idsToUnassign: Set<number>): void {
this.bookService.updateBookShelves(bookIds, idsToAssign, idsToUnassign).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book shelves updated'});
this.dynamicDialogRef.close();
},
error => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book shelves'});
this.dynamicDialogRef.close();
}
);
}
private getIdsToUnAssign(book: Book, idsToAssign: Set<number | undefined>): Set<number> {
const idsToUnassign = new Set<number>(); const idsToUnassign = new Set<number>();
book.shelves?.forEach(shelf => { book.shelves?.forEach(shelf => {
if (!idsToAssign.has(shelf.id!)) { if (!idsToAssign.has(shelf.id!)) {
idsToUnassign.add(shelf.id!); idsToUnassign.add(shelf.id!);
} }
}) });
return idsToUnassign; return idsToUnassign;
} }
@@ -103,7 +95,7 @@ export class ShelfAssignerComponent implements OnInit {
this.displayShelfDialog = false; this.displayShelfDialog = false;
} }
closeDialog() { closeDialog(): void {
this.dynamicDialogRef.close(); this.dynamicDialogRef.close();
} }
} }
@@ -5,7 +5,6 @@ export interface Book {
shelves?: Shelf[]; shelves?: Shelf[];
lastReadTime?: string; lastReadTime?: string;
addedOn?: string; addedOn?: string;
selected?: boolean;
} }
export interface BookMetadata { export interface BookMetadata {
@@ -24,7 +24,7 @@ export class BookService {
}); });
} }
assignShelvesToBook(bookIds: Set<number | undefined>, shelvesToAssign: Set<number | undefined>, shelvesToUnassign: Set<number | undefined>): Observable<Book[]> { updateBookShelves(bookIds: Set<number | undefined>, shelvesToAssign: Set<number | undefined>, shelvesToUnassign: Set<number | undefined>): Observable<Book[]> {
const requestPayload = { const requestPayload = {
bookIds: Array.from(bookIds), bookIds: Array.from(bookIds),
shelvesToAssign: Array.from(shelvesToAssign), shelvesToAssign: Array.from(shelvesToAssign),