Enhance book card component
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<div class="book-card"
|
||||
[class.selected]="book.selected"
|
||||
(mouseover)="isHovered = true"
|
||||
(mouseout)="isHovered = false">
|
||||
[class.selected]="isSelected"
|
||||
(mouseover)="isHovered = true"
|
||||
(mouseout)="isHovered = false">
|
||||
<div class="cover-container">
|
||||
<img
|
||||
[src]="coverImageSrc(book)"
|
||||
@@ -9,11 +9,11 @@
|
||||
alt="Cover of {{ book.metadata?.title }}"
|
||||
loading="lazy"/>
|
||||
<p-checkbox
|
||||
*ngIf="isHovered || book.selected"
|
||||
[(ngModel)]="book.selected"
|
||||
*ngIf="isCheckboxEnabled && (isHovered || isSelected)"
|
||||
[(ngModel)]="isSelected"
|
||||
binary="true"
|
||||
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-book" class="info-btn" (click)="readBook(book)"></p-button>
|
||||
</div>
|
||||
|
||||
@@ -68,16 +68,14 @@
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
right: 8px;
|
||||
display: none; /* Hidden by default */
|
||||
display: none;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
/* Show checkbox on hover */
|
||||
.cover-container:hover .select-checkbox {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* Keep checkbox visible for selected books */
|
||||
.book-card.selected .select-checkbox {
|
||||
display: block;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import { Component, Input, OnInit } from '@angular/core';
|
||||
import { Book } from '../../model/book.model';
|
||||
import { Button } from 'primeng/button';
|
||||
import { Router } from '@angular/router';
|
||||
import { MenuModule } from 'primeng/menu';
|
||||
import { MenuItem } from 'primeng/api';
|
||||
import { DialogService } from 'primeng/dynamicdialog';
|
||||
import { ShelfAssignerComponent } from '../shelf-assigner/shelf-assigner.component';
|
||||
import { BookService } from '../../service/book.service';
|
||||
import { CheckboxModule } from 'primeng/checkbox';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { NgIf } from '@angular/common';
|
||||
import {Component, Input, OnInit} from '@angular/core';
|
||||
import {Book} from '../../model/book.model';
|
||||
import {Button} from 'primeng/button';
|
||||
import {Router} from '@angular/router';
|
||||
import {MenuModule} from 'primeng/menu';
|
||||
import {MenuItem} from 'primeng/api';
|
||||
import {DialogService} from 'primeng/dynamicdialog';
|
||||
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {CheckboxModule} from 'primeng/checkbox';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {NgIf} from '@angular/common';
|
||||
import {Observable} from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-card',
|
||||
@@ -25,18 +26,42 @@ import { NgIf } from '@angular/common';
|
||||
})
|
||||
export class BookCardComponent implements OnInit {
|
||||
@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;
|
||||
isHovered: boolean = false; // Track hover state
|
||||
isHovered: boolean = false;
|
||||
|
||||
constructor(
|
||||
private bookService: BookService,
|
||||
private router: Router,
|
||||
private dialogService: DialogService
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
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 = [
|
||||
{
|
||||
label: 'Options',
|
||||
@@ -56,38 +81,21 @@ 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 {
|
||||
this.dialogService.open(ShelfAssignerComponent, {
|
||||
header: `Update Shelves: ${book.metadata?.title}`,
|
||||
modal: true,
|
||||
width: '30%',
|
||||
height: '70%',
|
||||
contentStyle: { overflow: 'auto' },
|
||||
contentStyle: {overflow: 'auto'},
|
||||
baseZIndex: 10,
|
||||
data: {
|
||||
book: this.book
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
openBookInfo(book: Book): void {
|
||||
this.router.navigate(['/library', book.libraryId, 'book', book.id, 'info']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,15 +13,18 @@
|
||||
|
||||
<div *ngIf="isAnyBookSelected()">
|
||||
<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) === '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>
|
||||
<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) === '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>
|
||||
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
|
||||
</div>
|
||||
|
||||
<div class="library-header-right">
|
||||
<p-dropdown optionLabel="name" placeholder="Select Sorting" />
|
||||
<p-dropdown optionLabel="name" placeholder="Select Sorting"/>
|
||||
<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>
|
||||
@@ -36,7 +39,9 @@
|
||||
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
|
||||
<app-book-card
|
||||
[book]="book"
|
||||
[onBookSelect]="handleBookSelect.bind(this)">
|
||||
[isCheckboxEnabled]="true"
|
||||
[onBookSelect]="handleBookSelect.bind(this)"
|
||||
[isSelected]="selectedBooks.has(book.id)">
|
||||
</app-book-card>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,7 +4,7 @@ import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
|
||||
import {LibraryService} from '../../service/library.service';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {Observable, of, Subject} from 'rxjs';
|
||||
import {Book} from '../../model/book.model';
|
||||
import {Library} from '../../model/library.model';
|
||||
import {Shelf} from '../../model/book.model';
|
||||
@@ -24,8 +24,9 @@ export class BooksBrowserComponent implements OnInit {
|
||||
entityType$: Observable<string | null> | undefined;
|
||||
items: MenuItem[] | undefined;
|
||||
selectedBooks: Set<number> = new Set();
|
||||
book: Book | undefined;
|
||||
entity: Library | Shelf | null = null;
|
||||
private deselectAllSubject = new Subject<void>(); // Subject to notify deselection
|
||||
deselectAll$ = this.deselectAllSubject.asObservable();
|
||||
|
||||
constructor(
|
||||
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) {
|
||||
this.selectedBooks.add(book.id);
|
||||
this.book = book; // Set the selected book
|
||||
this.selectedBooks.add(bookId);
|
||||
this.setupMenu();
|
||||
} else {
|
||||
this.selectedBooks.delete(book.id);
|
||||
this.book = undefined; // Clear the selected book
|
||||
this.selectedBooks.delete(bookId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,9 +123,6 @@ export class BooksBrowserComponent implements OnInit {
|
||||
|
||||
deselectAllBooks(): void {
|
||||
this.selectedBooks.clear();
|
||||
this.books$?.subscribe(books => {
|
||||
books.forEach(book => book.selected = false);
|
||||
});
|
||||
}
|
||||
|
||||
isAnyBookSelected(): boolean {
|
||||
@@ -143,7 +139,6 @@ export class BooksBrowserComponent implements OnInit {
|
||||
baseZIndex: 10,
|
||||
data: {
|
||||
isMultiBooks: true,
|
||||
book: this.book,
|
||||
bookIds: this.selectedBooks
|
||||
},
|
||||
});
|
||||
@@ -151,7 +146,7 @@ export class BooksBrowserComponent implements OnInit {
|
||||
|
||||
unshelfBooks() {
|
||||
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.selectedBooks = new Set<number>();
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
#scrollContainer
|
||||
[horizontal]="true">
|
||||
<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>
|
||||
|
||||
@@ -5,16 +5,15 @@ import {MessageService} from 'primeng/api';
|
||||
import {ShelfService} from '../../service/shelf.service';
|
||||
import {Observable} from 'rxjs';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {map, tap} from 'rxjs/operators';
|
||||
|
||||
@Component({
|
||||
selector: 'app-shelf-assigner',
|
||||
standalone: false,
|
||||
|
||||
templateUrl: './shelf-assigner.component.html',
|
||||
styleUrl: './shelf-assigner.component.scss'
|
||||
styleUrls: ['./shelf-assigner.component.scss']
|
||||
})
|
||||
export class ShelfAssignerComponent implements OnInit {
|
||||
|
||||
shelves$: Observable<Shelf[]>;
|
||||
book: Book;
|
||||
selectedShelves: Shelf[] = [];
|
||||
@@ -24,9 +23,12 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
isMultiBooks: boolean = false;
|
||||
|
||||
constructor(
|
||||
private shelfService: ShelfService, private dynamicDialogConfig: DynamicDialogConfig,
|
||||
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService,
|
||||
private bookService: BookService) {
|
||||
private shelfService: ShelfService,
|
||||
private dynamicDialogConfig: DynamicDialogConfig,
|
||||
private dynamicDialogRef: DynamicDialogRef,
|
||||
private messageService: MessageService,
|
||||
private bookService: BookService
|
||||
) {
|
||||
this.shelves$ = this.shelfService.shelves$;
|
||||
this.book = this.dynamicDialogConfig.data.book;
|
||||
this.bookIds = this.dynamicDialogConfig.data.bookIds;
|
||||
@@ -35,63 +37,53 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
|
||||
ngOnInit(): void {
|
||||
if (!this.isMultiBooks) {
|
||||
this.shelves$.subscribe(shelves => {
|
||||
if (this.book.shelves) {
|
||||
const bookShelfIds = this.book.shelves.map(shelf => shelf.id);
|
||||
this.selectedShelves = shelves.filter(shelf => bookShelfIds.includes(shelf.id));
|
||||
}
|
||||
});
|
||||
this.shelves$.pipe(
|
||||
map(shelves => shelves.filter(shelf => this.book.shelves?.some(bShelf => bShelf.id === shelf.id))),
|
||||
tap(selectedShelves => this.selectedShelves = selectedShelves)
|
||||
).subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
saveNewShelf() {
|
||||
saveNewShelf(): void {
|
||||
this.shelfService.createShelf(this.shelfName).subscribe(
|
||||
() => {
|
||||
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf created: ' + this.shelfName});
|
||||
this.displayShelfDialog = false;
|
||||
},
|
||||
(error) => {
|
||||
error => {
|
||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to create shelf'});
|
||||
console.error('Error creating shelf:', error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
updateBooksShelves() {
|
||||
if (!this.isMultiBooks) {
|
||||
const shelfIds: (number | undefined)[] = this.selectedShelves.map((shelf) => shelf.id);
|
||||
this.bookService.assignShelvesToBook(new Set([this.book.id]), new Set(shelfIds), this.getIdsToUnAssign(this.book, new Set(shelfIds))).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();
|
||||
}
|
||||
);
|
||||
} 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();
|
||||
}
|
||||
);
|
||||
}
|
||||
updateBooksShelves(): void {
|
||||
const idsToAssign: Set<number | undefined> = new Set(this.selectedShelves.map(shelf => shelf.id));
|
||||
const idsToUnassign: Set<number> = this.isMultiBooks ? new Set() : this.getIdsToUnAssign(this.book, idsToAssign);
|
||||
const bookIds = this.isMultiBooks ? this.bookIds : new Set([this.book.id]);
|
||||
this.updateBookShelves(bookIds, idsToAssign, idsToUnassign);
|
||||
}
|
||||
|
||||
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>();
|
||||
book.shelves?.forEach(shelf => {
|
||||
if (!idsToAssign.has(shelf.id!)) {
|
||||
idsToUnassign.add(shelf.id!);
|
||||
}
|
||||
})
|
||||
});
|
||||
return idsToUnassign;
|
||||
}
|
||||
|
||||
@@ -103,7 +95,7 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
this.displayShelfDialog = false;
|
||||
}
|
||||
|
||||
closeDialog() {
|
||||
closeDialog(): void {
|
||||
this.dynamicDialogRef.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ export interface Book {
|
||||
shelves?: Shelf[];
|
||||
lastReadTime?: string;
|
||||
addedOn?: string;
|
||||
selected?: boolean;
|
||||
}
|
||||
|
||||
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 = {
|
||||
bookIds: Array.from(bookIds),
|
||||
shelvesToAssign: Array.from(shelvesToAssign),
|
||||
|
||||
Reference in New Issue
Block a user