From 7837a0b49ddecb96f736851e9949ae13008517e1 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Tue, 24 Dec 2024 13:04:24 -0700 Subject: [PATCH] Fix shelf browsing. Todo: Fix menu --- .../book-card/book-card.component.ts | 2 +- .../books-browser.component.html | 2 +- .../books-browser/books-browser.component.ts | 70 ++++++++++++++----- .../src/app/book/service/book.service.ts | 1 - 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/booklore-ui/src/app/book/component/book-card/book-card.component.ts b/booklore-ui/src/app/book/component/book-card/book-card.component.ts index f0999ed4e..0b45f9d89 100644 --- a/booklore-ui/src/app/book/component/book-card/book-card.component.ts +++ b/booklore-ui/src/app/book/component/book-card/book-card.component.ts @@ -45,7 +45,7 @@ export class BookCardComponent implements OnInit { label: 'Options', items: [ { - label: 'Add to shelf', + label: 'Edit shelf', icon: 'pi pi-folder', command: () => { this.openShelfDialog(this.book); diff --git a/booklore-ui/src/app/book/component/books-browser/books-browser.component.html b/booklore-ui/src/app/book/component/books-browser/books-browser.component.html index c62a8e309..ac7635b7d 100644 --- a/booklore-ui/src/app/book/component/books-browser/books-browser.component.html +++ b/booklore-ui/src/app/book/component/books-browser/books-browser.component.html @@ -1,6 +1,6 @@
-

{{ (library$ | async)?.name }}

+

{{ (entity$ | async)?.name }}

| undefined; - library$: Observable | undefined; + entity$: Observable | undefined; items: MenuItem[] | undefined; constructor( @@ -33,38 +34,51 @@ export class BooksBrowserComponent implements OnInit { } ngOnInit(): void { - const libraryId$ = this.activatedRoute.paramMap.pipe( - map(params => Number(params.get('libraryId'))) + const routeParam$ = this.activatedRoute.paramMap.pipe( + map(params => { + const libraryId = Number(params.get('libraryId')); + const shelfId = Number(params.get('shelfId')); + + console.log(shelfId + '---' + libraryId) + return {libraryId, shelfId}; + }) ); - this.books$ = libraryId$.pipe( - switchMap(libraryId => { - if (!isNaN(libraryId)) { + this.books$ = routeParam$.pipe( + switchMap(({libraryId, shelfId}) => { + if (!isNaN(libraryId) && libraryId !== 0) { return this.bookService.books$.pipe( map(books => books.filter(book => book.libraryId === libraryId)) ); + } else if (!isNaN(shelfId) && shelfId !== 0) { + return this.bookService.books$.pipe( + map(books => books.filter(book => book.shelves?.some(shelf => shelf.id === shelfId))) + ); } else { return of([]); } }) ); - this.library$ = this.activatedRoute.paramMap.pipe( - switchMap(params => { - const libraryId = Number(params.get('libraryId')); - if (!isNaN(libraryId)) { + this.entity$ = routeParam$.pipe( + switchMap(({libraryId, shelfId}) => { + if (!isNaN(libraryId) && libraryId !== 0) { return this.libraryService.libraries$.pipe( map(libraries => libraries.find(lib => lib.id === libraryId) || null) ); + } else if (!isNaN(shelfId) && shelfId !== 0) { + return this.shelfService.shelves$.pipe( + map(shelves => shelves.find(shelf => shelf.id === shelfId) || null) + ); } return of(null); }) ); - this.initializeLibraryMenuItems(); + //this.initializeMenuItems(); } - private initializeLibraryMenuItems(): void { + /*private initializeMenuItems(): void { this.items = [ { icon: 'pi pi-trash', @@ -73,18 +87,18 @@ export class BooksBrowserComponent implements OnInit { tooltipPosition: 'top' }, command: () => { - this.library$?.subscribe(library => { - if (library) { + this.entity$?.subscribe(entity => { + if (entity) { this.confirmationService.confirm({ - message: `Are you sure you want to delete ${library.name}?`, + 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 (library.id) { - this.libraryService.deleteLibrary(library.id).subscribe({ + if ('libraryId' in entity) { // Check if it's a Library + this.libraryService.deleteLibrary(entity.id).subscribe({ complete: () => { this.router.navigate(['/']); this.messageService.add({ @@ -102,6 +116,25 @@ export class BooksBrowserComponent implements OnInit { }); } }); + } 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 + }); + } + }); } } }); @@ -110,6 +143,5 @@ export class BooksBrowserComponent implements OnInit { } } ]; - } - + }*/ } diff --git a/booklore-ui/src/app/book/service/book.service.ts b/booklore-ui/src/app/book/service/book.service.ts index 9b4839d29..007dc3c76 100644 --- a/booklore-ui/src/app/book/service/book.service.ts +++ b/booklore-ui/src/app/book/service/book.service.ts @@ -21,7 +21,6 @@ export class BookService { return of([]); }) ).subscribe((libraries) => { - console.log('Books loaded:', libraries); this.books.next(libraries); }); }