From 5d087928c60cef42e5a3e960dda977bc03fff314 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Fri, 24 Jan 2025 22:22:36 -0700 Subject: [PATCH] Implement edit shelf --- .../booklore/service/ShelfService.java | 1 + .../shelf-edit-dialog.component.html | 22 ++++++ .../shelf-edit-dialog.component.scss | 0 .../shelf-edit-dialog.component.ts | 78 +++++++++++++++++++ .../service/library-shelf-menu.service.ts | 19 +++++ .../src/app/book/service/shelf.service.ts | 21 +++++ 6 files changed, 141 insertions(+) create mode 100644 booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.html create mode 100644 booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.scss create mode 100644 booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.ts diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/ShelfService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/ShelfService.java index 8e3f741c3..bd2f290b0 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/ShelfService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/ShelfService.java @@ -38,6 +38,7 @@ public class ShelfService { ShelfEntity shelfEntity = shelfRepository.findById(id) .orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(id)); shelfEntity.setName(request.getName()); + shelfEntity.setIcon(request.getIcon()); return shelfMapper.toShelf(shelfRepository.save(shelfEntity)); } diff --git a/booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.html b/booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.html new file mode 100644 index 000000000..b300404ae --- /dev/null +++ b/booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.html @@ -0,0 +1,22 @@ +
+
+
+

Shelf Name:

+ +
+
+

Shelf Icon:

+
+ +
+
+ + +
+
+
+
+ +
+
+ diff --git a/booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.scss b/booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.scss new file mode 100644 index 000000000..e69de29bb diff --git a/booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.ts b/booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.ts new file mode 100644 index 000000000..b48effa48 --- /dev/null +++ b/booklore-ui/src/app/book/components/shelf-edit-dialog/shelf-edit-dialog.component.ts @@ -0,0 +1,78 @@ +import {Component, inject, OnInit, ViewChild} from '@angular/core'; +import {ShelfService} from '../../service/shelf.service'; +import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog'; +import {Button} from 'primeng/button'; +import {IconPickerComponent} from '../../../utilities/component/icon-picker/icon-picker.component'; +import {InputText} from 'primeng/inputtext'; +import {NgIf} from '@angular/common'; +import {FormsModule, ReactiveFormsModule} from '@angular/forms'; +import {Shelf} from '../../model/shelf.model'; +import {MessageService} from 'primeng/api'; + +@Component({ + selector: 'app-shelf-edit-dialog', + imports: [ + Button, + IconPickerComponent, + InputText, + NgIf, + ReactiveFormsModule, + FormsModule + ], + templateUrl: './shelf-edit-dialog.component.html', + styleUrl: './shelf-edit-dialog.component.scss' +}) +export class ShelfEditDialogComponent implements OnInit{ + + private shelfService = inject(ShelfService); + private dynamicDialogConfig = inject(DynamicDialogConfig); + private dynamicDialogRef = inject(DynamicDialogRef); + private messageService = inject(MessageService); + + shelfName: string = ''; + selectedIcon: string | null = null; + shelf!: Shelf | undefined; + + @ViewChild(IconPickerComponent) iconPicker: IconPickerComponent | undefined; + + ngOnInit(): void { + const shelfId = this.dynamicDialogConfig?.data.shelfId; + this.shelf = this.shelfService.getShelfById(shelfId); + if(this.shelf) { + this.shelfName = this.shelf.name; + this.selectedIcon = 'pi pi-' + this.shelf.icon; + } + } + + openIconPicker() { + if (this.iconPicker) { + this.iconPicker.open(); + } + } + + clearSelectedIcon() { + this.selectedIcon = null; + } + + onIconSelected(icon: string) { + this.selectedIcon = icon; + } + + save() { + const shelf: Shelf = { + name: this.shelfName, + icon: this.selectedIcon?.replace('pi pi-', '') || 'heart' + }; + + this.shelfService.updateShelf(shelf, this.shelf?.id).subscribe({ + next: () => { + this.messageService.add({severity: 'success', summary: 'Shelf Updated', detail: 'The shelf was updated successfully.'}); + this.dynamicDialogRef.close(); + }, + error: (e) => { + this.messageService.add({severity: 'error', summary: 'Update Failed', detail: 'An error occurred while updating the shelf. Please try again.'}); + console.error(e); + } + }); + } +} 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 index e98356aef..1d204bf44 100644 --- a/booklore-ui/src/app/book/service/library-shelf-menu.service.ts +++ b/booklore-ui/src/app/book/service/library-shelf-menu.service.ts @@ -9,6 +9,7 @@ import {DialogService} from 'primeng/dynamicdialog'; import {MetadataFetchOptionsComponent} from '../../metadata/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component'; import {MetadataRefreshType} from '../../metadata/model/request/metadata-refresh-type.enum'; import {LibraryCreatorComponent} from '../components/library-creator/library-creator.component'; +import {ShelfEditDialogComponent} from '../components/shelf-edit-dialog/shelf-edit-dialog.component'; @Injectable({ providedIn: 'root', @@ -126,6 +127,24 @@ export class LibraryShelfMenuService { { label: 'Options', items: [ + { + label: 'Edit Shelf', + icon: 'pi pi-pen-to-square', + command: () => { + this.dialogService.open(ShelfEditDialogComponent, { + header: 'Edit Shelf', + modal: true, + closable: true, + data: { + shelfId: entity?.id + }, + style: { + position: 'absolute', + top: '15%', + } + }) + } + }, { label: 'Delete Shelf', icon: 'pi pi-trash', diff --git a/booklore-ui/src/app/book/service/shelf.service.ts b/booklore-ui/src/app/book/service/shelf.service.ts index 0219e9a61..719a9d5a3 100644 --- a/booklore-ui/src/app/book/service/shelf.service.ts +++ b/booklore-ui/src/app/book/service/shelf.service.ts @@ -7,6 +7,7 @@ import { SortOption } from '../model/sort.model'; import { BookService } from './book.service'; import { ShelfState } from '../model/state/shelf-state.model'; import {API_CONFIG} from '../../config/api-config'; +import {Library} from '../model/library.model'; @Injectable({ providedIn: 'root', @@ -46,6 +47,11 @@ export class ShelfService { }); } + getShelfById(shelfId: number): Shelf | undefined { + let shelfState = this.shelfStateSubject.value; + return shelfState.shelves?.find(shelf => shelf.id === shelfId); + } + createShelf(shelf: Shelf): Observable { return this.http.post(this.url, shelf).pipe( map(newShelf => { @@ -62,6 +68,21 @@ export class ShelfService { ); } + updateShelf(shelf: Shelf, shelfId: number | undefined): Observable { + return this.http.put(`${this.url}/${shelfId}`, shelf).pipe( + map(updatedShelf => { + const currentState = this.shelfStateSubject.value; + const updatedShelves = currentState.shelves ? currentState.shelves.map(existingShelf => + existingShelf.id === updatedShelf.id ? updatedShelf : existingShelf) : [updatedShelf]; + this.shelfStateSubject.next({...currentState, shelves: updatedShelves,}); + return updatedShelf; + }), + catchError(error => { + throw error; + }) + ); + } + updateSort(shelfId: number, sort: SortOption): Observable { return this.http.put(`${this.url}/${shelfId}/sort`, sort).pipe( map(updatedShelf => {