Implement library update

This commit is contained in:
aditya.chandel
2025-01-19 02:56:25 -07:00
parent 6beae77c35
commit 67d1b75831
23 changed files with 261 additions and 124 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ export class AppComponent implements OnInit {
this.bookService.handleNewlyCreatedBook(JSON.parse(message.body));
});
this.rxStompService.watch('/topic/books-removed').subscribe((message: Message) => {
this.rxStompService.watch('/topic/books-remove').subscribe((message: Message) => {
this.bookService.handleRemovedBookIds(JSON.parse(message.body));
});
@@ -11,7 +11,7 @@
<div class="library-name-icon-parent">
<div class="library-name-div">
<p>Library Name: </p>
<input type="text" pInputText [(ngModel)]="libraryName" placeholder="Enter library name..."/>
<input type="text" pInputText [(ngModel)]="chosenLibraryName" placeholder="Enter library name..."/>
</div>
<div class="library-icon-div">
<p>Library Icon:</p>
@@ -64,7 +64,7 @@
</div>
<div class="flex pt-6 justify-between">
<p-button label="Back" icon="pi pi-arrow-left" iconPos="right" (onClick)="activateCallback(1)" />
<p-button severity="success" label="Save" icon="pi pi-save" [disabled]="!isDirectorySelectionValid()" (onClick)="addLibrary()"></p-button>
<p-button severity="success" label="Save" icon="pi pi-save" [disabled]="!isDirectorySelectionValid()" (onClick)="createOrUpdateLibrary()"></p-button>
</div>
</div>
</ng-template>
@@ -1,57 +1,60 @@
import {Component, inject, ViewChild} from '@angular/core';
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Component, inject, OnInit, ViewChild} from '@angular/core';
import {DialogService, DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {DirectoryPickerComponent} from '../../../utilities/component/directory-picker/directory-picker.component';
import {MessageService} from 'primeng/api';
import {Router} from '@angular/router';
import {LibraryService} from '../../service/library.service';
import {IconPickerComponent} from '../../../utilities/component/icon-picker/icon-picker.component';
import {take} from 'rxjs';
import {Button} from 'primeng/button';
import {TableModule} from 'primeng/table';
import {Step, StepList, StepPanel, StepPanels, Stepper} from 'primeng/stepper';
import {NgIf} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {InputText} from 'primeng/inputtext';
import {BookService} from '../../service/book.service';
import {Library, LibraryPath} from '../../model/library.model';
import {Library} from '../../model/library.model';
@Component({
selector: 'app-library-creator',
standalone: true,
templateUrl: './library-creator.component.html',
imports: [
Button,
TableModule,
StepPanel,
IconPickerComponent,
NgIf,
FormsModule,
InputText,
Stepper,
StepList,
Step,
StepPanels
],
imports: [Button, TableModule, StepPanel, IconPickerComponent, NgIf, FormsModule, InputText, Stepper, StepList, Step, StepPanels],
styleUrl: './library-creator.component.scss'
})
export class LibraryCreatorComponent {
export class LibraryCreatorComponent implements OnInit {
@ViewChild(IconPickerComponent) iconPicker: IconPickerComponent | undefined;
libraryName: string = '';
chosenLibraryName: string = '';
folders: string[] = [];
ref: DynamicDialogRef | undefined;
selectedIcon: string | null = null;
mode!: string;
library!: Library | undefined;
editModeLibraryName: string = '';
private dialogService = inject(DialogService);
private dynamicDialogRef = inject(DynamicDialogRef);
private dynamicDialogConfig = inject(DynamicDialogConfig);
private libraryService = inject(LibraryService);
private messageService = inject(MessageService);
private router = inject(Router);
ngOnInit(): void {
this.mode = this.dynamicDialogConfig.data.mode;
if (this.mode === 'edit') {
this.library = this.libraryService.findLibraryById(this.dynamicDialogConfig.data.libraryId);
if (this.library) {
this.chosenLibraryName = this.library.name;
this.editModeLibraryName = this.library.name;
this.selectedIcon = 'pi pi-' + this.library.icon;
this.folders = this.library.paths.map(path => path.path);
}
}
}
show() {
this.ref = this.dialogService.open(DirectoryPickerComponent, {
this.dynamicDialogRef = this.dialogService.open(DirectoryPickerComponent, {
header: 'Select Media Directory',
modal: true,
width: '50%',
@@ -60,7 +63,7 @@ export class LibraryCreatorComponent {
baseZIndex: 10
});
this.ref.onClose.subscribe((selectedFolder: string) => {
this.dynamicDialogRef.onClose.subscribe((selectedFolder: string) => {
if (selectedFolder) {
this.addFolder(selectedFolder);
}
@@ -90,49 +93,65 @@ export class LibraryCreatorComponent {
}
isLibraryDetailsValid(): boolean {
return !!this.libraryName.trim() && !!this.selectedIcon;
return !!this.chosenLibraryName.trim() && !!this.selectedIcon;
}
isDirectorySelectionValid(): boolean {
return this.folders.length > 0;
}
addLibrary() {
const library: Library = {
name: this.libraryName,
icon: this.selectedIcon?.replace('pi pi-', '') || 'heart',
paths: this.folders.map(folder => ({ path: folder }))
};
this.libraryService.createLibrary(library).subscribe({
next: (createdLibrary) => {
this.router.navigate(['/library', createdLibrary.id, 'books']);
},
error: (err) => {
console.error('Failed to create library:', err);
}
});
this.dynamicDialogRef.close();
createOrUpdateLibrary() {
if (this.mode === 'edit') {
const library: Library = {
name: this.chosenLibraryName,
icon: this.selectedIcon?.replace('pi pi-', '') || 'heart',
paths: this.folders.map(folder => ({path: folder}))
};
this.libraryService.updateLibrary(library, this.library?.id).subscribe({
next: () => {
this.messageService.add({severity: 'success', summary: 'Library Updated', detail: 'The library was updated successfully.'});
this.dynamicDialogRef.close();
},
error: (e) => {
this.messageService.add({severity: 'error', summary: 'Update Failed', detail: 'An error occurred while updating the library. Please try again.'});
console.error(e);
}
});
} else {
const library: Library = {
name: this.chosenLibraryName,
icon: this.selectedIcon?.replace('pi pi-', '') || 'heart',
paths: this.folders.map(folder => ({path: folder}))
};
this.libraryService.createLibrary(library).subscribe({
next: (createdLibrary) => {
this.router.navigate(['/library', createdLibrary.id, 'books']);
this.messageService.add({severity: 'success', summary: 'Library Created', detail: 'The library was created successfully.'});
this.dynamicDialogRef.close();
},
error: (e) => {
this.messageService.add({severity: 'error', summary: 'Creation Failed', detail: 'An error occurred while creating the library. Please try again.'});
console.error(e);
}
});
}
}
validateLibraryNameAndProceed(activateCallback: Function) {
if (this.libraryName.trim()) {
const libraryName = this.libraryName.trim();
this.libraryService.libraryState$
.pipe(take(1))
.subscribe(libraryState => {
const library = libraryState.libraries?.find(library => library.name === libraryName);
if (library) {
this.messageService.add({
severity: 'error',
summary: 'Library Name Exists',
detail: 'This library name is already taken.',
});
} else {
activateCallback(2);
}
let trimmedLibraryName = this.chosenLibraryName.trim();
if (trimmedLibraryName && trimmedLibraryName != this.editModeLibraryName) {
let exists = this.libraryService.doesLibraryExistByName(trimmedLibraryName);
if (exists) {
this.messageService.add({
severity: 'error',
summary: 'Library Name Exists',
detail: 'This library name is already taken.',
});
} else {
activateCallback(2);
}
} else {
activateCallback(2);
}
}
@@ -182,10 +182,10 @@ export class BookService {
this.bookStateSubject.next({...currentState, books: updatedBooks});
}
handleRemovedBookIds(removedBookIds: Set<number>): void {
handleRemovedBookIds(removedBookIds: number[]): void {
const currentState = this.bookStateSubject.value;
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.has(book.id));
this.bookStateSubject.next({...currentState, books: filteredBooks});
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.includes(book.id)); // Check using includes() method
this.bookStateSubject.next({ ...currentState, books: filteredBooks });
}
handleBookUpdate(updatedBook: Book) {
@@ -8,6 +8,7 @@ import {Shelf} from '../model/shelf.model';
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';
@Injectable({
providedIn: 'root',
@@ -21,12 +22,32 @@ export class LibraryShelfMenuService {
private router = inject(Router);
private dialogService = inject(DialogService);
initializeLibraryMenuItems(entity: Library | Shelf | null): MenuItem[] {
return [
{
label: 'Options',
items: [
{
label: 'Edit Library',
icon: 'pi pi-pen-to-square',
command: () => {
this.dialogService.open(LibraryCreatorComponent, {
header: 'Edit Library',
modal: true,
closable: true,
width: '675px',
height: '480px',
style: {
position: 'absolute',
top: '15%',
},
data: {
mode: 'edit',
libraryId: entity?.id
}
});
}
},
{
label: 'Delete Library',
icon: 'pi pi-trash',
@@ -56,7 +77,7 @@ export class LibraryShelfMenuService {
}
},
{
label: 'Refresh Library',
label: 'Re-scan Library',
icon: 'pi pi-refresh',
command: () => {
this.confirmationService.confirm({
@@ -46,6 +46,15 @@ export class LibraryService {
});
}
doesLibraryExistByName(name: string): boolean {
const libraries = this.libraryStateSubject.value.libraries || [];
return libraries.some(library => library.name === name);
}
findLibraryById(id: number): Library | undefined {
return this.libraryStateSubject.value.libraries?.find(library => library.id === id);
}
createLibrary(newLibrary: Library): Observable<Library> {
return this.http.post<Library>(this.url, newLibrary).pipe(
map(createdLibrary => {
@@ -55,8 +64,21 @@ export class LibraryService {
return createdLibrary;
}),
catchError(error => {
throw error;
})
);
}
updateLibrary(library: Library, libraryId: number | undefined): Observable<Library> {
return this.http.put<Library>(`${this.url}/${libraryId}`, library).pipe(
map(updatedLibrary => {
const currentState = this.libraryStateSubject.value;
this.libraryStateSubject.next({...currentState, error: error.message});
const updatedLibraries = currentState.libraries ? currentState.libraries.map(existingLibrary =>
existingLibrary.id === updatedLibrary.id ? updatedLibrary : existingLibrary) : [updatedLibrary];
this.libraryStateSubject.next({...currentState, libraries: updatedLibraries,});
return updatedLibrary;
}),
catchError(error => {
throw error;
})
);
@@ -128,7 +128,7 @@
<div class="form-row">
<label class="label"></label>
<div class="input-container">
<img *ngIf="!updateThumbnailUrl" [src]="urlHelper.getCoverUrl(metadata.bookId, metadata?.coverUpdatedOn)" alt="Book Thumbnail" class="thumbnail"/>
<img *ngIf="!updateThumbnailUrl" [src]="urlHelper.getCoverUrl(metadata.bookId, metadata.coverUpdatedOn)" alt="Book Thumbnail" class="thumbnail"/>
<img *ngIf="updateThumbnailUrl" [src]="fetchedMetadata.thumbnailUrl" alt="Fetched Thumbnail" class="thumbnail"/>
<p-button
[icon]="thumbnailSaved ? 'pi pi-check' : 'pi pi-arrow-left'"
@@ -1,4 +1,4 @@
import {Component, EventEmitter, Output} from '@angular/core';
import {Component, EventEmitter, Input, Output} from '@angular/core';
import {DialogModule} from 'primeng/dialog';
import {NgForOf} from '@angular/common';
import {FormsModule} from '@angular/forms';
@@ -14,9 +14,6 @@ import {FormsModule} from '@angular/forms';
],
})
export class IconPickerComponent {
iconDialogVisible: boolean = false;
selectedIcon: string | null = null;
searchText: string = '';
iconCategories: string[] = [
"address-book", "align-center", "align-justify", "align-left", "align-right", "android",
"angle-double-down", "angle-double-left", "angle-double-right", "angle-double-up", "angle-down", "angle-left",
@@ -51,7 +48,10 @@ export class IconPickerComponent {
];
icons: string[] = this.createIconList(this.iconCategories);
iconDialogVisible: boolean = false;
searchText: string = '';
selectedIcon: string | null = null;
@Output() iconSelected = new EventEmitter<string>();
createIconList(categories: string[]): string[] {