diff --git a/booklore-ui/src/app/book/component/book-browser/book-browser.component.html b/booklore-ui/src/app/book/component/book-browser/book-browser.component.html index 0ff1fe16c..17ddad877 100644 --- a/booklore-ui/src/app/book/component/book-browser/book-browser.component.html +++ b/booklore-ui/src/app/book/component/book-browser/book-browser.component.html @@ -1,82 +1,105 @@ -
-
-
- - -

- {{ entityType }}: {{ (entity$ | async)?.name }} -

-

- All Books -

- -
- - -
-
- -
- -
- - - -
- - -
-
-
- -
- -
- -
-
-

- {{ entityType === 'Library' ? "Failed to load library's books!" : "Failed to load shelf's books!" }} +

+
+ + +

+ {{ entityType }}: {{ (entity$ | async)?.name }}

-
-
-

- {{ entityType === 'Library' ? "This library has no books!" : "This shelf has no books!" }} +

+ All Books

-
-
- -
-
- - -
-
-
-
- +
+ + + + + + + + +
+ + +
+
+
+ + +
+ +
+ + +
+
+

+ {{ entityType === EntityType.LIBRARY ? "Failed to load library's books!" : "Failed to load shelf's books!" }} +

+
+
+

+ {{ entityType === EntityType.LIBRARY ? "This library has no books!" : "This shelf has no books!" }} +

+
+ + +
+ +
+
+ + +
+
+
+
+ + +
diff --git a/booklore-ui/src/app/book/component/book-browser/book-browser.component.ts b/booklore-ui/src/app/book/component/book-browser/book-browser.component.ts index 1e75e0222..543a978d5 100644 --- a/booklore-ui/src/app/book/component/book-browser/book-browser.component.ts +++ b/booklore-ui/src/app/book/component/book-browser/book-browser.component.ts @@ -15,6 +15,13 @@ import {SortOptionsHelper} from '../../service/sort-options.helper'; import {SortOption} from '../../model/sort.model'; import {BookState} from '../../model/state/book-state.model'; import {Book} from '../../model/book.model'; +import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service'; + +export enum EntityType { + LIBRARY = 'Library', + SHELF = 'Shelf', + ALL_BOOKS = 'All Books' +} @Component({ selector: 'app-book-browser', @@ -25,11 +32,11 @@ import {Book} from '../../model/book.model'; export class BookBrowserComponent implements OnInit { bookState$: Observable | undefined; entity$: Observable | undefined; - entityType$: Observable | undefined; + entityType$: Observable | undefined; bookTitle$ = new BehaviorSubject(''); entity: Library | Shelf | null = null; - entityType: string = ''; + entityType: EntityType | undefined; bookTitle: string = ''; entityOptions: MenuItem[] | undefined; selectedBooks: Set = new Set(); @@ -40,14 +47,13 @@ export class BookBrowserComponent implements OnInit { constructor( private activatedRoute: ActivatedRoute, - private router: Router, - private confirmationService: ConfirmationService, private messageService: MessageService, private libraryService: LibraryService, private bookService: BookService, private shelfService: ShelfService, private dialogService: DialogService, - private sortService: SortService + private sortService: SortService, + private libraryShelfMenuService: LibraryShelfMenuService ) { } @@ -58,11 +64,10 @@ export class BookBrowserComponent implements OnInit { if (isAllBooksRoute) { this.entityType$ = this.activatedRoute.url.pipe( map(segments => { - const fullPath = segments.map(segment => segment.path).join('/'); - return fullPath === 'all-books' ? 'all-books' : null; + return EntityType.ALL_BOOKS; }) ); - this.entityType = 'all-books'; + this.entityType = EntityType.ALL_BOOKS; this.bookState$ = this.fetchAllBooks(); } else { @@ -77,7 +82,7 @@ export class BookBrowserComponent implements OnInit { ); this.entityType$ = routeParam$.pipe( - map(({libraryId, shelfId}) => libraryId ? 'Library' : shelfId ? 'Shelf' : null) + map(({libraryId, shelfId}) => libraryId ? EntityType.LIBRARY : shelfId ? EntityType.SHELF : EntityType.ALL_BOOKS) ); this.entity$.subscribe(entity => { @@ -105,12 +110,12 @@ export class BookBrowserComponent implements OnInit { private fetchEntity(libraryId: number, shelfId: number): Observable { if (libraryId) { - this.entityType = 'Library'; - this.initializeLibraryMenuItems(); + this.entityType = EntityType.LIBRARY; + this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity); return this.fetchLibrary(libraryId); } else if (shelfId) { - this.entityType = 'Shelf'; - this.initializeShelfMenuItems(); + this.entityType = EntityType.SHELF; + this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity); return this.fetchShelf(shelfId); } return of(null); @@ -242,7 +247,7 @@ export class BookBrowserComponent implements OnInit { updateSortOption(sortOption: SortOption): void { this.selectedSort = sortOption; - if (this.entityType === 'all-books') { + if (this.entityType === EntityType.ALL_BOOKS) { this.bookState$ = this.fetchAllBooks(); } else { const routeParam$ = this.getRouteParams(); @@ -250,9 +255,9 @@ export class BookBrowserComponent implements OnInit { switchMap(({libraryId, shelfId}) => this.fetchBooksByEntity(libraryId, shelfId)) ); } - if (this.entityType === 'Library') { + if (this.entityType === EntityType.LIBRARY) { this.libraryService.updateSort(this.entity?.id!, sortOption).subscribe(); - } else if (this.entityType === 'Shelf') { + } else if (this.entityType === EntityType.SHELF) { this.shelfService.updateSort(this.entity?.id!, sortOption).subscribe(); } } @@ -293,116 +298,5 @@ export class BookBrowserComponent implements OnInit { }); } - private initializeLibraryMenuItems(): void { - this.entityOptions = [ - { - label: 'Options', - items: [ - { - label: 'Delete Library', - icon: 'pi pi-trash', - command: () => { - this.confirmationService.confirm({ - message: 'Sure you want to delete library: ' + this.entity?.name + "?", - header: 'Confirmation', - icon: 'pi pi-exclamation-triangle', - acceptIcon: 'none', - rejectIcon: 'none', - acceptButtonStyleClass: '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 - }); - } - }); - } - }); - } - }, - { - label: 'Refresh Library', - icon: 'pi pi-refresh', - command: () => { - this.confirmationService.confirm({ - message: 'Sure you want to refresh library: ' + this.entity?.name + "?", - header: 'Confirmation', - icon: 'pi pi-exclamation-triangle', - acceptIcon: 'none', - rejectIcon: 'none', - acceptButtonStyleClass: 'p-button-text', - accept: () => { - this.libraryService.refreshLibrary(this.entity?.id!).subscribe({ - complete: () => { - this.messageService.add({ - severity: 'info', - summary: 'Success', - detail: 'Library refresh scheduled' - }); - }, - error: () => { - this.messageService.add({ - severity: 'error', - summary: 'Failed', - detail: 'Failed to refresh library', - life: 3000 - }); - } - }); - } - }); - } - } - ], - }, - ]; - } - - private initializeShelfMenuItems(): void { - this.entityOptions = [ - { - label: 'Options', - items: [ - { - label: 'Delete Shelf', - icon: 'pi pi-trash', - command: () => { - this.confirmationService.confirm({ - message: 'Sure you want to delete ' + this.entity?.name + "?", - header: 'Confirmation', - icon: 'pi pi-exclamation-triangle', - acceptIcon: 'none', - rejectIcon: 'none', - acceptButtonStyleClass: '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 - }); - } - }); - } - }); - } - } - ], - }, - ]; - } + protected readonly EntityType = EntityType; } diff --git a/booklore-ui/src/app/book/component/book-card/book-card.component.scss b/booklore-ui/src/app/book/component/book-card/book-card.component.scss index 32d24fe0a..a8dcec03a 100644 --- a/booklore-ui/src/app/book/component/book-card/book-card.component.scss +++ b/booklore-ui/src/app/book/component/book-card/book-card.component.scss @@ -17,13 +17,11 @@ .book-title-container { display: flex; align-items: center; - justify-content: space-between; } .book-title { font-size: 0.85rem; color: var(--text-color); - margin-bottom: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; diff --git a/booklore-ui/src/app/book/service/library-shelf-menu.service.ts b/booklore-ui/src/app/book/service/library-shelf-menu.service.ts new file mode 100644 index 000000000..19894f699 --- /dev/null +++ b/booklore-ui/src/app/book/service/library-shelf-menu.service.ts @@ -0,0 +1,112 @@ +import { Injectable } from '@angular/core'; +import { MenuItem, ConfirmationService, MessageService } from 'primeng/api'; +import { Router } from '@angular/router'; +import {LibraryService} from './library.service'; +import {ShelfService} from './shelf.service'; + +@Injectable({ + providedIn: 'root', +}) +export class LibraryShelfMenuService { + constructor( + private confirmationService: ConfirmationService, + private messageService: MessageService, + private libraryService: LibraryService, + private shelfService: ShelfService, + private router: Router + ) {} + + initializeLibraryMenuItems(entity: any): MenuItem[] { + return [ + { + label: 'Options', + items: [ + { + label: 'Delete Library', + icon: 'pi pi-trash', + command: () => { + this.confirmationService.confirm({ + message: `Are you sure you want to delete library: ${entity?.name}?`, + header: 'Confirmation', + accept: () => { + this.libraryService.deleteLibrary(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', + }); + } + }); + } + }); + } + }, + { + label: 'Refresh Library', + icon: 'pi pi-refresh', + command: () => { + this.confirmationService.confirm({ + message: `Are you sure you want to refresh library: ${entity?.name}?`, + header: 'Confirmation', + accept: () => { + this.libraryService.refreshLibrary(entity?.id!).subscribe({ + complete: () => { + this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library refresh scheduled'}); + }, + error: () => { + this.messageService.add({ + severity: 'error', + summary: 'Failed', + detail: 'Failed to refresh library', + }); + } + }); + } + }); + } + } + ] + } + ]; + } + + initializeShelfMenuItems(entity: any): MenuItem[] { + return [ + { + label: 'Options', + items: [ + { + label: 'Delete Shelf', + icon: 'pi pi-trash', + command: () => { + this.confirmationService.confirm({ + message: `Are you sure you want to delete shelf: ${entity?.name}?`, + header: 'Confirmation', + accept: () => { + 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', + }); + } + }); + } + }); + } + } + ] + } + ]; + } +}