diff --git a/booklore-ui/src/app/book/component/books-browser/books-browser.component.ts b/booklore-ui/src/app/book/component/books-browser/books-browser.component.ts index 9062886c0..f74115179 100644 --- a/booklore-ui/src/app/book/component/books-browser/books-browser.component.ts +++ b/booklore-ui/src/app/book/component/books-browser/books-browser.component.ts @@ -1,17 +1,17 @@ -import { Component, OnInit } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; -import { ConfirmationService, MenuItem, MessageService } from 'primeng/api'; -import { LibraryService } from '../../service/library.service'; -import { BookService } from '../../service/book.service'; -import { map, switchMap } from 'rxjs/operators'; -import { Observable, of } from 'rxjs'; -import { Book } from '../../model/book.model'; -import { ShelfService } from '../../service/shelf.service'; -import { ShelfAssignerComponent } from '../shelf-assigner/shelf-assigner.component'; -import { DialogService } from 'primeng/dynamicdialog'; -import { Library } from '../../model/library.model'; -import { Shelf } from '../../model/shelf.model'; -import { SortService } from '../../service/sort.service'; +import {Component, OnInit} from '@angular/core'; +import {ActivatedRoute, Router} from '@angular/router'; +import {ConfirmationService, MenuItem, MessageService} from 'primeng/api'; +import {LibraryService} from '../../service/library.service'; +import {BookService} from '../../service/book.service'; +import {map, switchMap} from 'rxjs/operators'; +import {Observable, of} from 'rxjs'; +import {Book} from '../../model/book.model'; +import {ShelfService} from '../../service/shelf.service'; +import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component'; +import {DialogService} from 'primeng/dynamicdialog'; +import {Library} from '../../model/library.model'; +import {Shelf} from '../../model/shelf.model'; +import {SortService} from '../../service/sort.service'; import {SortOptionsHelper} from '../../service/sort-options.helper'; import {SortOption} from '../../model/sort.model'; @@ -43,7 +43,8 @@ export class BooksBrowserComponent implements OnInit { private shelfService: ShelfService, private dialogService: DialogService, private sortService: SortService - ) {} + ) { + } ngOnInit(): void { this.sortOptions = SortOptionsHelper.generateSortOptions(); @@ -51,15 +52,15 @@ export class BooksBrowserComponent implements OnInit { const routeParam$ = this.getRouteParams(); this.books$ = routeParam$.pipe( - switchMap(({ libraryId, shelfId }) => this.fetchBooks(libraryId, shelfId)) + switchMap(({libraryId, shelfId}) => this.fetchBooks(libraryId, shelfId)) ); this.entity$ = routeParam$.pipe( - switchMap(({ libraryId, shelfId }) => this.fetchEntity(libraryId, shelfId)) + switchMap(({libraryId, shelfId}) => this.fetchEntity(libraryId, shelfId)) ); this.entityType$ = routeParam$.pipe( - map(({ libraryId, shelfId }) => libraryId ? 'Library' : shelfId ? 'Shelf' : null) + map(({libraryId, shelfId}) => libraryId ? 'Library' : shelfId ? 'Shelf' : null) ); this.entity$.subscribe(entity => { @@ -75,7 +76,7 @@ export class BooksBrowserComponent implements OnInit { map(params => { const libraryId = Number(params.get('libraryId')); const shelfId = Number(params.get('shelfId')); - return { libraryId, shelfId }; + return {libraryId, shelfId}; }) ); } @@ -92,9 +93,11 @@ export class BooksBrowserComponent implements OnInit { private fetchEntity(libraryId: number, shelfId: number): Observable { if (libraryId) { this.entityType = 'Library'; + this.initializeLibraryMenuItems(); return this.fetchLibrary(libraryId); } else if (shelfId) { this.entityType = 'Shelf'; + this.initializeShelfMenuItems(); return this.fetchShelf(shelfId); } return of(null); @@ -146,11 +149,11 @@ export class BooksBrowserComponent implements OnInit { if (this.entity) { this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe( () => { - this.messageService.add({ severity: 'info', summary: 'Success', detail: 'Books shelves updated' }); + this.messageService.add({severity: 'info', summary: 'Success', detail: 'Books shelves updated'}); this.selectedBooks = new Set(); }, (error) => { - this.messageService.add({ severity: 'error', summary: 'Error', detail: 'Failed to update books shelves' }); + this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update books shelves'}); } ); } @@ -160,7 +163,7 @@ export class BooksBrowserComponent implements OnInit { this.selectedSort = sortOption; const routeParam$ = this.getRouteParams(); this.books$ = routeParam$.pipe( - switchMap(({ libraryId, shelfId }) => this.fetchBooks(libraryId, shelfId)) + switchMap(({libraryId, shelfId}) => this.fetchBooks(libraryId, shelfId)) ); if (this.entityType === 'Library') { this.libraryService.updateSort(this.entity?.id!, sortOption).subscribe(); @@ -171,7 +174,7 @@ export class BooksBrowserComponent implements OnInit { private setSelectedSortFromEntity(entity: Library | Shelf | null): void { if (entity?.sort) { - const { field, direction } = entity.sort; + const {field, direction} = entity.sort; this.selectedSort = this.sortOptions.find(option => option.field === field && option.direction === direction) || null; } else { this.selectedSort = null; @@ -192,7 +195,7 @@ export class BooksBrowserComponent implements OnInit { modal: true, width: '30%', height: '70%', - contentStyle: { overflow: 'auto' }, + contentStyle: {overflow: 'auto'}, baseZIndex: 10, data: { isMultiBooks: true, @@ -206,4 +209,88 @@ export class BooksBrowserComponent implements OnInit { ]; } + private initializeLibraryMenuItems(): void { + this.items = [ + { + icon: 'pi pi-trash', + tooltipOptions: { + tooltipLabel: 'Delete', + tooltipPosition: 'top' + }, + command: () => { + this.confirmationService.confirm({ + message: 'Sure you want to delete ' + this.entity?.name + "?", + header: 'Confirmation', + icon: 'pi pi-exclamation-triangle', + acceptIcon: 'none', + rejectIcon: 'none', + rejectButtonStyleClass: 'p-button-text', + accept: () => { + this.libraryService.deleteLibrary(this.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 + }); + } + }); + } + }); + } + } + ]; + } + + private initializeShelfMenuItems(): void { + this.items = [ + { + icon: 'pi pi-trash', + tooltipOptions: { + tooltipLabel: 'Delete', + tooltipPosition: 'top' + }, + command: () => { + this.confirmationService.confirm({ + message: 'Sure you want to delete ' + this.entity?.name + "?", + header: 'Confirmation', + icon: 'pi pi-exclamation-triangle', + acceptIcon: 'none', + rejectIcon: 'none', + rejectButtonStyleClass: 'p-button-text', + accept: () => { + this.shelfService.deleteShelf(this.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 + }); + } + }); + } + }); + } + } + ]; + } + } diff --git a/booklore-ui/src/app/book/service/book.service.ts b/booklore-ui/src/app/book/service/book.service.ts index b713218b7..9ee9f4e41 100644 --- a/booklore-ui/src/app/book/service/book.service.ts +++ b/booklore-ui/src/app/book/service/book.service.ts @@ -54,6 +54,16 @@ export class BookService { this.books.next(filteredBooks); } + removeBooksFromShelf(shelfId: number): void { + const books = this.books.value.filter(book => { + if(book.shelves) { + book.shelves = book.shelves.filter(shelf => shelf.id !== shelfId); + } + return book; + }) + this.books.next(books); + } + getBookById(bookId: number): Observable { return this.books$.pipe( map(books => books.find(book => book.id === bookId)) diff --git a/booklore-ui/src/app/book/service/library.service.ts b/booklore-ui/src/app/book/service/library.service.ts index eaa307ca3..8cde0a981 100644 --- a/booklore-ui/src/app/book/service/library.service.ts +++ b/booklore-ui/src/app/book/service/library.service.ts @@ -39,15 +39,15 @@ export class LibraryService { ); } - deleteLibrary(libraryId: number) { - return this.http.delete(`${this.url}/${libraryId}`).pipe( + deleteLibrary(libraryId: number): Observable { + return this.http.delete(`${this.url}/${libraryId}`).pipe( tap(() => { this.bookService.removeBooksByLibraryId(libraryId); const updatedLibraries = this.libraries.value.filter(library => library.id !== libraryId); this.libraries.next(updatedLibraries); }), catchError(error => { - return of(null); + return of(); }) ); } diff --git a/booklore-ui/src/app/book/service/shelf.service.ts b/booklore-ui/src/app/book/service/shelf.service.ts index e9cea9aa2..8d65459b5 100644 --- a/booklore-ui/src/app/book/service/shelf.service.ts +++ b/booklore-ui/src/app/book/service/shelf.service.ts @@ -1,9 +1,10 @@ import {Injectable} from '@angular/core'; -import {BehaviorSubject, Observable, of} from 'rxjs'; +import {BehaviorSubject, Observable, of, tap} from 'rxjs'; import {catchError, map} from 'rxjs/operators'; import {HttpClient} from '@angular/common/http'; import {Shelf} from '../model/shelf.model'; import {SortOption} from '../model/sort.model'; +import {BookService} from "./book.service"; @Injectable({ providedIn: 'root' @@ -14,7 +15,7 @@ export class ShelfService { private shelves = new BehaviorSubject([]); shelves$ = this.shelves.asObservable(); - constructor(private http: HttpClient) { + constructor(private http: HttpClient, private bookService: BookService) { this.http.get(this.url).pipe( catchError(error => { return of([]); @@ -53,4 +54,16 @@ export class ShelfService { ); } + deleteShelf(shelfId: number) { + return this.http.delete(`${this.url}/${shelfId}`).pipe( + tap(() => { + this.bookService.removeBooksByLibraryId(shelfId); + let shelves = this.shelves.value.filter(shelf => shelf.id !== shelfId); + this.shelves.next(shelves); + }), + catchError(error => { + return of(); + }) + ); + } }