Enhance book card component
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user