WIP: Multi select
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user