WIP: Multi select
This commit is contained in:
@@ -35,6 +35,7 @@ import {BookCardComponent} from './book/component/book-card/book-card.component'
|
||||
import {SpeedDialModule} from 'primeng/speeddial';
|
||||
import {RouteReuseStrategy} from '@angular/router';
|
||||
import {CustomReuseStrategy} from './custom-reuse-strategy';
|
||||
import {MenuModule} from 'primeng/menu';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
@@ -69,6 +70,7 @@ import {CustomReuseStrategy} from './custom-reuse-strategy';
|
||||
DialogModule,
|
||||
BookCardComponent,
|
||||
SpeedDialModule,
|
||||
MenuModule,
|
||||
],
|
||||
providers: [
|
||||
DialogService,
|
||||
|
||||
@@ -1,14 +1,32 @@
|
||||
<div class="book-card">
|
||||
<div class="book-card"
|
||||
[class.selected]="book.selected"
|
||||
(mouseover)="isHovered = true"
|
||||
(mouseout)="isHovered = false">
|
||||
<div class="cover-container">
|
||||
<img [src]="coverImageSrc(book)" class="book-cover placeholder" alt="Cover of {{ book.metadata?.title }}" loading="lazy" />
|
||||
<img
|
||||
[src]="coverImageSrc(book)"
|
||||
class="book-cover placeholder"
|
||||
alt="Cover of {{ book.metadata?.title }}"
|
||||
loading="lazy"/>
|
||||
<p-checkbox
|
||||
*ngIf="isHovered || book.selected"
|
||||
[(ngModel)]="book.selected"
|
||||
binary="true"
|
||||
class="select-checkbox"
|
||||
(ngModelChange)="toggleSelection(book.selected || 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>
|
||||
|
||||
<div class="book-info">
|
||||
<div class="book-title-container">
|
||||
<h4 class="book-title">{{ book.metadata?.title }}</h4>
|
||||
<p-menu #menu [model]="items" [popup]="true" appendTo="body" />
|
||||
<p-button [text]="true" (click)="menu.toggle($event)" icon="pi pi-ellipsis-v" />
|
||||
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
|
||||
<p-button
|
||||
[text]="true"
|
||||
(click)="menu.toggle($event)"
|
||||
icon="pi pi-ellipsis-v">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
.book-card {
|
||||
background-color: var(--surface-card);
|
||||
border-radius: 8px 8px 0 0;
|
||||
border-radius: 8px;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
transition: border 0.2s;
|
||||
|
||||
&.selected {
|
||||
border: 2px solid var(--primary-color);
|
||||
}
|
||||
}
|
||||
|
||||
.cover-container {
|
||||
@@ -13,7 +18,6 @@
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.book-title {
|
||||
@@ -24,19 +28,18 @@
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
text-align: center;
|
||||
padding: 6px 6px 3px;
|
||||
padding: 6px;
|
||||
}
|
||||
|
||||
.book-cover {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
max-height: 100%;
|
||||
object-fit: cover;
|
||||
border-radius: 8px 8px 0 0;
|
||||
transition: filter 0.2s ease-in-out;
|
||||
}
|
||||
|
||||
.info-btn, .read-btn {
|
||||
.info-btn,
|
||||
.read-btn {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
@@ -45,15 +48,11 @@
|
||||
}
|
||||
|
||||
.info-btn {
|
||||
top: 25%; /* 30% below the top of the image */
|
||||
top: 25%;
|
||||
}
|
||||
|
||||
.read-btn {
|
||||
bottom: 25%; /* 30% above the bottom of the image */
|
||||
}
|
||||
|
||||
.cover-container {
|
||||
position: relative; /* Ensures buttons are positioned relative to the image */
|
||||
bottom: 25%;
|
||||
}
|
||||
|
||||
.cover-container:hover .book-cover {
|
||||
@@ -65,3 +64,20 @@
|
||||
display: block;
|
||||
}
|
||||
|
||||
.select-checkbox {
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
right: 8px;
|
||||
display: none; /* Hidden by default */
|
||||
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,79 +1,93 @@
|
||||
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 { 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';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-card',
|
||||
templateUrl: './book-card.component.html',
|
||||
styleUrls: ['./book-card.component.scss'],
|
||||
imports: [
|
||||
Button,
|
||||
MenuModule
|
||||
]
|
||||
selector: 'app-book-card',
|
||||
templateUrl: './book-card.component.html',
|
||||
styleUrls: ['./book-card.component.scss'],
|
||||
imports: [
|
||||
Button,
|
||||
MenuModule,
|
||||
CheckboxModule,
|
||||
FormsModule,
|
||||
NgIf,
|
||||
],
|
||||
})
|
||||
export class BookCardComponent implements OnInit {
|
||||
@Input() book!: Book;
|
||||
@Input() book!: Book;
|
||||
@Input() onBookSelect: (book: Book, selected: boolean) => void = () => {};
|
||||
|
||||
items: MenuItem[] | undefined;
|
||||
items: MenuItem[] | undefined;
|
||||
isHovered: boolean = false; // Track hover state
|
||||
|
||||
constructor(
|
||||
private bookService: BookService, private router: Router,
|
||||
private dialogService: DialogService) {
|
||||
}
|
||||
constructor(
|
||||
private bookService: BookService,
|
||||
private router: Router,
|
||||
private dialogService: DialogService
|
||||
) {}
|
||||
|
||||
coverImageSrc(book: Book): string {
|
||||
return this.bookService.getBookCoverUrl(book.id);
|
||||
}
|
||||
ngOnInit(): void {
|
||||
this.items = [
|
||||
{
|
||||
label: 'Options',
|
||||
items: [
|
||||
{
|
||||
label: 'Edit shelf',
|
||||
icon: 'pi pi-folder',
|
||||
command: () => this.openShelfDialog(this.book),
|
||||
},
|
||||
{
|
||||
label: 'View metadata',
|
||||
icon: 'pi pi-info-circle',
|
||||
command: () => this.openBookInfo(this.book),
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
readBook(book: Book) {
|
||||
this.bookService.readBook(book);
|
||||
}
|
||||
coverImageSrc(book: Book): string {
|
||||
return this.bookService.getBookCoverUrl(book.id);
|
||||
}
|
||||
|
||||
openBookInfo(book: Book) {
|
||||
this.router.navigate(['/library', book.libraryId, 'book', book.id, 'info']);
|
||||
}
|
||||
readBook(book: Book): void {
|
||||
this.bookService.readBook(book);
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.items = [
|
||||
{
|
||||
label: 'Options',
|
||||
items: [
|
||||
{
|
||||
label: 'Edit shelf',
|
||||
icon: 'pi pi-folder',
|
||||
command: () => {
|
||||
this.openShelfDialog(this.book);
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'View metadata',
|
||||
icon: 'pi pi-info-circle',
|
||||
command: () => {
|
||||
this.openBookInfo(this.book);
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
];
|
||||
}
|
||||
openBookInfo(book: Book): void {
|
||||
this.router.navigate(['/library', book.libraryId, 'book', book.id, 'info']);
|
||||
}
|
||||
|
||||
private openShelfDialog(book: Book) {
|
||||
this.dialogService.open(ShelfAssignerComponent, {
|
||||
header: 'Update Shelves: ' + book.metadata?.title,
|
||||
modal: true,
|
||||
width: '30%',
|
||||
height: '70%',
|
||||
contentStyle: {overflow: 'auto'},
|
||||
baseZIndex: 10,
|
||||
data: {
|
||||
book: this.book
|
||||
},
|
||||
});
|
||||
}
|
||||
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' },
|
||||
baseZIndex: 10,
|
||||
data: {
|
||||
book: this.book
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,15 @@
|
||||
hideIcon="pi pi-times">
|
||||
</p-speedDial>
|
||||
</div>
|
||||
|
||||
<div *ngIf="isAnyBookSelected()">
|
||||
<p-button icon="pi pi-ellipsis-v" label="Options" severity="primary" [rounded]="true" [outlined]="true" (click)="menu.toggle($event)"></p-button>
|
||||
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
|
||||
<p-button icon="pi pi-times" label="Deselect All" severity="warning" [rounded]="true" [outlined]="true" (click)="deselectAllBooks()"></p-button>
|
||||
</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>
|
||||
@@ -26,7 +31,10 @@
|
||||
<virtual-scroller *ngIf="books.length > 0" class="virtual-scroller" #scroll [items]="books">
|
||||
<div class="grid" #container>
|
||||
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
|
||||
<app-book-card [book]="book"></app-book-card>
|
||||
<app-book-card
|
||||
[book]="book"
|
||||
[onBookSelect]="handleBookSelect.bind(this)">
|
||||
</app-book-card>
|
||||
</div>
|
||||
</div>
|
||||
</virtual-scroller>
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
gap: 2rem;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.library-name {
|
||||
margin: 0;
|
||||
color: var(--text-color);
|
||||
|
||||
@@ -9,6 +9,8 @@ import {Book} from '../../model/book.model';
|
||||
import {Library} from '../../model/library.model';
|
||||
import {Shelf} from '../../model/book.model';
|
||||
import {ShelfService} from '../../service/shelf.service';
|
||||
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
||||
import {DialogService} from 'primeng/dynamicdialog';
|
||||
|
||||
@Component({
|
||||
selector: 'app-books-browser',
|
||||
@@ -17,11 +19,12 @@ import {ShelfService} from '../../service/shelf.service';
|
||||
styleUrls: ['./books-browser.component.scss']
|
||||
})
|
||||
export class BooksBrowserComponent implements OnInit {
|
||||
|
||||
books$: Observable<Book[]> | undefined;
|
||||
entity$: Observable<Library | Shelf | null> | undefined;
|
||||
entityType$: Observable<string | null> | undefined;
|
||||
items: MenuItem[] | undefined;
|
||||
selectedBooks: Set<number> = new Set();
|
||||
book: Book | undefined;
|
||||
|
||||
constructor(
|
||||
private activatedRoute: ActivatedRoute,
|
||||
@@ -30,7 +33,8 @@ export class BooksBrowserComponent implements OnInit {
|
||||
private messageService: MessageService,
|
||||
private libraryService: LibraryService,
|
||||
private bookService: BookService,
|
||||
private shelfService: ShelfService
|
||||
private shelfService: ShelfService,
|
||||
private dialogService: DialogService
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -84,75 +88,58 @@ export class BooksBrowserComponent implements OnInit {
|
||||
return of(null);
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
//this.initializeMenuItems();
|
||||
}
|
||||
|
||||
/*private initializeMenuItems(): void {
|
||||
handleBookSelect(book: Book, selected: boolean): void {
|
||||
if (selected) {
|
||||
this.selectedBooks.add(book.id);
|
||||
this.book = book; // Set the selected book
|
||||
this.setupMenu();
|
||||
} else {
|
||||
this.selectedBooks.delete(book.id);
|
||||
this.book = undefined; // Clear the selected book
|
||||
}
|
||||
}
|
||||
|
||||
setupMenu(): void {
|
||||
this.items = [
|
||||
{
|
||||
icon: 'pi pi-trash',
|
||||
tooltipOptions: {
|
||||
tooltipLabel: 'Delete',
|
||||
tooltipPosition: 'top'
|
||||
},
|
||||
command: () => {
|
||||
this.entity$?.subscribe(entity => {
|
||||
if (entity) {
|
||||
this.confirmationService.confirm({
|
||||
message: `Are you sure you want to delete ${entity.name}?`,
|
||||
header: 'Confirmation',
|
||||
icon: 'pi pi-exclamation-triangle',
|
||||
acceptIcon: 'none',
|
||||
rejectIcon: 'none',
|
||||
rejectButtonStyleClass: 'p-button-text',
|
||||
accept: () => {
|
||||
if ('libraryId' in entity) { // Check if it's a Library
|
||||
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',
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if ('id' in entity) { // Check if it's a Shelf
|
||||
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',
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
label: 'Options',
|
||||
items: [
|
||||
{
|
||||
label: 'Edit shelf',
|
||||
icon: 'pi pi-folder',
|
||||
command: () => this.openShelfDialog(),
|
||||
}
|
||||
],
|
||||
},
|
||||
];
|
||||
}*/
|
||||
}
|
||||
|
||||
deselectAllBooks(): void {
|
||||
this.selectedBooks.clear();
|
||||
this.books$?.subscribe(books => {
|
||||
books.forEach(book => book.selected = false);
|
||||
});
|
||||
}
|
||||
|
||||
isAnyBookSelected(): boolean {
|
||||
return this.selectedBooks.size > 0;
|
||||
}
|
||||
|
||||
openShelfDialog(): void {
|
||||
this.dialogService.open(ShelfAssignerComponent, {
|
||||
header: `Update Books Shelves`,
|
||||
modal: true,
|
||||
width: '30%',
|
||||
height: '70%',
|
||||
contentStyle: {overflow: 'auto'},
|
||||
baseZIndex: 10,
|
||||
data: {
|
||||
isMultiBooks: true,
|
||||
book: this.book,
|
||||
bookIds: this.selectedBooks
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,11 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
|
||||
shelves$: Observable<Shelf[]>;
|
||||
book: Book;
|
||||
selectedShelves: any[] = [];
|
||||
selectedShelves: Shelf[] = [];
|
||||
displayShelfDialog: boolean = false;
|
||||
shelfName: string = '';
|
||||
bookIds: Set<number> = new Set();
|
||||
isMultiBooks: boolean = false;
|
||||
|
||||
constructor(
|
||||
private shelfService: ShelfService, private dynamicDialogConfig: DynamicDialogConfig,
|
||||
@@ -27,15 +29,19 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
private bookService: BookService) {
|
||||
this.shelves$ = this.shelfService.shelves$;
|
||||
this.book = this.dynamicDialogConfig.data.book;
|
||||
this.bookIds = this.dynamicDialogConfig.data.bookIds;
|
||||
this.isMultiBooks = this.dynamicDialogConfig.data.isMultiBooks;
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
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));
|
||||
}
|
||||
});
|
||||
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));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
saveNewShelf() {
|
||||
@@ -52,17 +58,41 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
}
|
||||
|
||||
updateBooksShelves() {
|
||||
const shelfIds = this.selectedShelves.map((shelf) => shelf.id);
|
||||
this.bookService.assignShelvesToBook(this.book, 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();
|
||||
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();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
getIdsToUnAssign(book: Book, idsToAssign: Set<number | undefined>): Set<number | undefined> {
|
||||
const idsToUnassign = new Set<number>();
|
||||
book.shelves?.forEach(shelf => {
|
||||
if (!idsToAssign.has(shelf.id!)) {
|
||||
idsToUnassign.add(shelf.id!);
|
||||
}
|
||||
);
|
||||
})
|
||||
return idsToUnassign;
|
||||
}
|
||||
|
||||
createShelfDialog(): void {
|
||||
|
||||
@@ -5,6 +5,7 @@ export interface Book {
|
||||
shelves?: Shelf[];
|
||||
lastReadTime?: string;
|
||||
addedOn?: string;
|
||||
selected?: boolean;
|
||||
}
|
||||
|
||||
export interface BookMetadata {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {BehaviorSubject, filter, Observable, of, tap} from 'rxjs';
|
||||
import {BehaviorSubject, Observable, of} from 'rxjs';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {catchError, map, switchMap} from 'rxjs/operators';
|
||||
import {Book, BookMetadata, BookSetting, BookWithNeighborsDTO} from '../model/book.model';
|
||||
import {ShelfService} from './shelf.service';
|
||||
import {Book, BookMetadata, BookSetting} from '../model/book.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -14,7 +13,7 @@ export class BookService {
|
||||
private books = new BehaviorSubject<Book[]>([]);
|
||||
books$ = this.books.asObservable();
|
||||
|
||||
constructor(private http: HttpClient, private shelfService: ShelfService) {
|
||||
constructor(private http: HttpClient) {
|
||||
this.http.get<Book[]>(this.url).pipe(
|
||||
catchError(error => {
|
||||
console.error('Error loading books:', error);
|
||||
@@ -25,23 +24,26 @@ export class BookService {
|
||||
});
|
||||
}
|
||||
|
||||
assignShelvesToBook(book: Book, shelfIds: number[]): Observable<Book> {
|
||||
assignShelvesToBook(bookIds: Set<number | undefined>, shelvesToAssign: Set<number | undefined>, shelvesToUnassign: Set<number | undefined>): Observable<Book[]> {
|
||||
const requestPayload = {
|
||||
bookId: book.id,
|
||||
shelfIds: shelfIds,
|
||||
bookIds: Array.from(bookIds),
|
||||
shelvesToAssign: Array.from(shelvesToAssign),
|
||||
shelvesToUnassign: Array.from(shelvesToUnassign),
|
||||
};
|
||||
return this.http.post<Book>(`${this.url}/assign-shelves`, requestPayload).pipe(
|
||||
map((updatedBook) => {
|
||||
return this.http.post<Book[]>(`${this.url}/assign-shelves`, requestPayload).pipe(
|
||||
map((updatedBooks) => {
|
||||
const currentBooks = this.books.value;
|
||||
const bookIndex = currentBooks.findIndex(b => b.id === updatedBook.id);
|
||||
if (bookIndex !== -1) {
|
||||
currentBooks[bookIndex] = updatedBook;
|
||||
this.books.next([...currentBooks]);
|
||||
}
|
||||
return updatedBook;
|
||||
updatedBooks.forEach(updatedBook => {
|
||||
const bookIndex = currentBooks.findIndex(b => b.id === updatedBook.id);
|
||||
if (bookIndex !== -1) {
|
||||
currentBooks[bookIndex] = updatedBook;
|
||||
}
|
||||
});
|
||||
this.books.next([...currentBooks]);
|
||||
return updatedBooks;
|
||||
}),
|
||||
catchError(error => {
|
||||
console.error('Error assigning shelves to book:', error);
|
||||
console.error('Error assigning shelves to books:', error);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user