Fix shelf browsing.

Todo: Fix menu
This commit is contained in:
aditya.chandel
2024-12-24 13:04:24 -07:00
parent 84724ec014
commit 7837a0b49d
4 changed files with 53 additions and 22 deletions
@@ -45,7 +45,7 @@ export class BookCardComponent implements OnInit {
label: 'Options', label: 'Options',
items: [ items: [
{ {
label: 'Add to shelf', label: 'Edit shelf',
icon: 'pi pi-folder', icon: 'pi pi-folder',
command: () => { command: () => {
this.openShelfDialog(this.book); this.openShelfDialog(this.book);
@@ -1,6 +1,6 @@
<div class="library-header"> <div class="library-header">
<div class="library-header-dial"> <div class="library-header-dial">
<h3 class="library-name">{{ (library$ | async)?.name }}</h3> <h3 class="library-name">{{ (entity$ | async)?.name }}</h3>
<p-speedDial <p-speedDial
[model]="items!" [model]="items!"
direction="right" direction="right"
@@ -7,6 +7,7 @@ import {map, switchMap} from 'rxjs/operators';
import {Observable, of} from 'rxjs'; import {Observable, of} from 'rxjs';
import {Book} from '../../model/book.model'; import {Book} from '../../model/book.model';
import {Library} from '../../model/library.model'; import {Library} from '../../model/library.model';
import {Shelf} from '../../model/book.model';
import {ShelfService} from '../../service/shelf.service'; import {ShelfService} from '../../service/shelf.service';
@Component({ @Component({
@@ -18,7 +19,7 @@ import {ShelfService} from '../../service/shelf.service';
export class BooksBrowserComponent implements OnInit { export class BooksBrowserComponent implements OnInit {
books$: Observable<Book[]> | undefined; books$: Observable<Book[]> | undefined;
library$: Observable<Library | null> | undefined; entity$: Observable<Library | Shelf | null> | undefined;
items: MenuItem[] | undefined; items: MenuItem[] | undefined;
constructor( constructor(
@@ -33,38 +34,51 @@ export class BooksBrowserComponent implements OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
const libraryId$ = this.activatedRoute.paramMap.pipe( const routeParam$ = this.activatedRoute.paramMap.pipe(
map(params => Number(params.get('libraryId'))) 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( this.books$ = routeParam$.pipe(
switchMap(libraryId => { switchMap(({libraryId, shelfId}) => {
if (!isNaN(libraryId)) { if (!isNaN(libraryId) && libraryId !== 0) {
return this.bookService.books$.pipe( return this.bookService.books$.pipe(
map(books => books.filter(book => book.libraryId === libraryId)) 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 { } else {
return of([]); return of([]);
} }
}) })
); );
this.library$ = this.activatedRoute.paramMap.pipe( this.entity$ = routeParam$.pipe(
switchMap(params => { switchMap(({libraryId, shelfId}) => {
const libraryId = Number(params.get('libraryId')); if (!isNaN(libraryId) && libraryId !== 0) {
if (!isNaN(libraryId)) {
return this.libraryService.libraries$.pipe( return this.libraryService.libraries$.pipe(
map(libraries => libraries.find(lib => lib.id === libraryId) || null) 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); return of(null);
}) })
); );
this.initializeLibraryMenuItems(); //this.initializeMenuItems();
} }
private initializeLibraryMenuItems(): void { /*private initializeMenuItems(): void {
this.items = [ this.items = [
{ {
icon: 'pi pi-trash', icon: 'pi pi-trash',
@@ -73,18 +87,18 @@ export class BooksBrowserComponent implements OnInit {
tooltipPosition: 'top' tooltipPosition: 'top'
}, },
command: () => { command: () => {
this.library$?.subscribe(library => { this.entity$?.subscribe(entity => {
if (library) { if (entity) {
this.confirmationService.confirm({ 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', header: 'Confirmation',
icon: 'pi pi-exclamation-triangle', icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none', acceptIcon: 'none',
rejectIcon: 'none', rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text', rejectButtonStyleClass: 'p-button-text',
accept: () => { accept: () => {
if (library.id) { if ('libraryId' in entity) { // Check if it's a Library
this.libraryService.deleteLibrary(library.id).subscribe({ this.libraryService.deleteLibrary(entity.id).subscribe({
complete: () => { complete: () => {
this.router.navigate(['/']); this.router.navigate(['/']);
this.messageService.add({ 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 {
} }
} }
]; ];
} }*/
} }
@@ -21,7 +21,6 @@ export class BookService {
return of([]); return of([]);
}) })
).subscribe((libraries) => { ).subscribe((libraries) => {
console.log('Books loaded:', libraries);
this.books.next(libraries); this.books.next(libraries);
}); });
} }