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
@@ -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;
})
);