Implement edit shelf

This commit is contained in:
aditya.chandel
2025-01-24 22:22:36 -07:00
parent ba06e70177
commit 5d087928c6
6 changed files with 141 additions and 0 deletions
@@ -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));
}
@@ -0,0 +1,22 @@
<div class="flex flex-col py-4 px-4 gap-6 h-96 w-96">
<div class="flex flex-col gap-10">
<div class="flex items-center gap-4">
<p>Shelf Name: </p>
<input type="text" pInputText [(ngModel)]="shelfName" placeholder="Enter shelf name..."/>
</div>
<div class="flex items-center gap-5">
<p>Shelf Icon:</p>
<div *ngIf="!selectedIcon">
<p-button label="Select Icon" icon="pi pi-search" (onClick)="openIconPicker()"></p-button>
</div>
<div *ngIf="selectedIcon">
<i [class]="selectedIcon" class="text-xl mr-3"></i>
<p-button icon="pi pi-times" (onClick)="clearSelectedIcon()" [rounded]="true" [text]="true" [outlined]="true" severity="danger"></p-button>
</div>
</div>
</div>
<div class="flex justify-end">
<p-button label="Save" icon="pi pi-check" (onClick)="save()"></p-button>
</div>
</div>
<app-icon-picker (iconSelected)="onIconSelected($event)"></app-icon-picker>
@@ -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);
}
});
}
}
@@ -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',
@@ -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<Shelf> {
return this.http.post<Shelf>(this.url, shelf).pipe(
map(newShelf => {
@@ -62,6 +68,21 @@ export class ShelfService {
);
}
updateShelf(shelf: Shelf, shelfId: number | undefined): Observable<Shelf> {
return this.http.put<Shelf>(`${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<Shelf> {
return this.http.put<Shelf>(`${this.url}/${shelfId}/sort`, sort).pipe(
map(updatedShelf => {