feat(metadata-lock): add UI for toggling individual field locks with save/reset actions

This commit is contained in:
aditya.chandel
2025-05-31 14:32:44 -06:00
committed by Aditya Chandel
parent 8518e57d92
commit 128fa1c847
8 changed files with 205 additions and 24 deletions
@@ -616,8 +616,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
}
lockUnlockMetadata() {
const count = this.selectedBooks.size;
this.dynamicDialogRef = this.dialogService.open(LockUnlockMetadataDialogComponent, {
header: 'Toggle Metadata Lock',
header: `Lock or Unlock Metadata for ${count} Selected Book${count > 1 ? 's' : ''}`,
modal: true,
closable: true,
data: {
@@ -1,7 +1,27 @@
<div class="flex flex-col items-center text-center space-y-6">
<p class="pt-6 px-4">Lock or unlock all metadata for all the selected books</p>
<div class="flex gap-4 justify-center">
<p-button icon="pi pi-lock" label="Lock" outlined="true" severity="danger" (onClick)="toggleLock('LOCK')"></p-button>
<p-button icon="pi pi-lock-open" label="Unlock" outlined="true" severity="success" (onClick)="toggleLock('UNLOCK')"></p-button>
<div class="p-4">
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-x-24 gap-y-4">
<div *ngFor="let field of lockableFields" class="flex items-center justify-between">
<label class="capitalize">{{ fieldLabels[field] || field }}</label>
<p-button
[outlined]="true"
[label]="getLockLabel(field)"
[icon]="getLockIcon(field)"
[severity]="fieldLocks[field] === true ? 'danger' : fieldLocks[field] === false ? 'success' : 'secondary'"
(click)="cycleLockState(field)"
></p-button>
</div>
</div>
</div>
<p-divider></p-divider>
<div class="flex justify-end px-4">
<p-button icon="pi pi-refresh" label="Reset" outlined="true" severity="warn" (onClick)="resetFieldLocks()"></p-button>
<p-divider layout="vertical"></p-divider>
<div class="flex gap-4 justify-center">
<p-button icon="pi pi-lock" label="Lock All" outlined="true" severity="danger" (onClick)="toggleLockAll('LOCK')"></p-button>
<p-button icon="pi pi-lock-open" label="Unlock All" outlined="true" severity="success" (onClick)="toggleLockAll('UNLOCK')"></p-button>
</div>
<p-divider layout="vertical"></p-divider>
<p-button [label]="isSaving ? 'Saving...' : 'Save'" [outlined]="true" icon="pi pi-check" severity="info" (onClick)="applyFieldLocks()" [disabled]="bookIds.size === 0 || isSaving"></p-button>
</div>
@@ -1,41 +1,138 @@
import {Component, inject} from '@angular/core';
import {Component, inject, OnInit} from '@angular/core';
import {Button} from 'primeng/button';
import {FormsModule} from '@angular/forms';
import {NgForOf} from '@angular/common';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {MessageService} from 'primeng/api';
import {BookService} from '../../../service/book.service';
import {Divider} from 'primeng/divider';
@Component({
selector: 'app-lock-unlock-metadata-dialog',
standalone: true,
imports: [
Button
Button,
FormsModule,
NgForOf,
Divider
],
templateUrl: './lock-unlock-metadata-dialog.component.html',
styleUrl: './lock-unlock-metadata-dialog.component.scss'
})
export class LockUnlockMetadataDialogComponent {
export class LockUnlockMetadataDialogComponent implements OnInit {
private bookService = inject(BookService);
private dynamicDialogConfig = inject(DynamicDialogConfig);
private dialogRef = inject(DynamicDialogRef);
private messageService = inject(MessageService);
fieldLocks: Record<string, boolean | undefined> = {};
bookIds: Set<number> = this.dynamicDialogConfig.data.bookIds;
toggleLock(action: 'LOCK' | 'UNLOCK'): void {
this.bookService.toggleAllLock(this.bookIds, action).subscribe({
lockableFields: string[] = [
'titleLocked', 'subtitleLocked', 'publisherLocked', 'publishedDateLocked', 'descriptionLocked',
'isbn13Locked', 'isbn10Locked', 'asinLocked', 'pageCountLocked', 'thumbnailLocked', 'languageLocked',
'ratingLocked', 'reviewCountLocked', 'coverLocked',
'seriesNameLocked', 'seriesNumberLocked', 'seriesTotalLocked',
'authorsLocked', 'categoriesLocked',
'amazonRatingLocked', 'amazonReviewCountLocked',
'goodreadsRatingLocked', 'goodreadsReviewCountLocked',
'hardcoverRatingLocked', 'hardcoverReviewCountLocked'
];
fieldLabels: Record<string, string> = {
titleLocked: 'Title',
subtitleLocked: 'Subtitle',
publisherLocked: 'Publisher',
publishedDateLocked: 'Published Date',
descriptionLocked: 'Description',
isbn13Locked: 'ISBN-13',
isbn10Locked: 'ISBN-10',
asinLocked: 'ASIN',
pageCountLocked: 'Page Count',
thumbnailLocked: 'Thumbnail',
languageLocked: 'Language',
ratingLocked: 'Rating',
reviewCountLocked: 'Review Count',
coverLocked: 'Cover',
seriesNameLocked: 'Series Name',
seriesNumberLocked: 'Series Number',
seriesTotalLocked: 'Series Total',
authorsLocked: 'Authors',
categoriesLocked: 'Categories',
amazonRatingLocked: 'Amazon Rating',
amazonReviewCountLocked: 'Amazon Rating Count',
goodreadsRatingLocked: 'Goodreads Rating',
goodreadsReviewCountLocked: 'Goodreads Rating Count',
hardcoverRatingLocked: 'Hardcover Rating',
hardcoverReviewCountLocked: 'Hardcover Rating Count'
};
isSaving = false;
ngOnInit(): void {
this.lockableFields.forEach(field => this.fieldLocks[field] = undefined);
}
toggleLockAll(action: 'LOCK' | 'UNLOCK'): void {
const lockState = action === 'LOCK' ? true : false;
this.lockableFields.forEach(field => {
this.fieldLocks[field] = lockState;
});
}
getLockLabel(field: string): string {
const state = this.fieldLocks[field];
if (state === undefined) return 'Unselected';
return state ? 'Locked' : 'Unlocked';
}
getLockIcon(field: string): string {
const state = this.fieldLocks[field];
return state === undefined ? '' : state ? 'pi pi-lock' : 'pi pi-lock-open';
}
resetFieldLocks(): void {
this.lockableFields.forEach(field => {
this.fieldLocks[field] = undefined;
});
}
cycleLockState(field: string): void {
const current = this.fieldLocks[field];
if (current === undefined) {
this.fieldLocks[field] = true;
} else if (current) {
this.fieldLocks[field] = false;
} else {
this.fieldLocks[field] = undefined;
}
}
applyFieldLocks(): void {
const fieldActions: Record<string, 'LOCK' | 'UNLOCK'> = {};
for (const [field, locked] of Object.entries(this.fieldLocks)) {
if (locked !== undefined) {
fieldActions[field] = locked ? 'LOCK' : 'UNLOCK';
}
}
this.isSaving = true;
this.bookService.toggleFieldLocks(this.bookIds, fieldActions).subscribe({
next: () => {
const isLock = action === 'LOCK';
this.isSaving = false;
this.messageService.add({
severity: 'success',
summary: `Metadata ${isLock ? 'Locked' : 'Unlocked'}`,
detail: `All selected books have been ${isLock ? 'locked' : 'unlocked'} successfully.`,
summary: 'Field Locks Updated',
detail: 'Selected metadata fields have been updated successfully.'
});
this.dialogRef.close(action.toLowerCase());
this.dialogRef.close('fields-updated');
},
error: () => {
this.isSaving = false;
this.messageService.add({
severity: 'error',
summary: `Failed to ${action === 'LOCK' ? 'Lock' : 'Unlock'}`,
detail: `An error occurred while ${action === 'LOCK' ? 'locking' : 'unlocking'} metadata.`,
summary: 'Failed to Update Field Locks',
detail: 'An error occurred while updating field lock statuses.'
});
}
});
@@ -75,8 +75,6 @@ export class MetadataSearcherComponent implements OnInit, OnDestroy {
distinctUntilChanged(([prevBook], [currBook]) => prevBook?.id === currBook?.id)
).subscribe(([book, settings]) => {
console.log(settings!.metadataProviderSettings)
this.providers = Object.entries(settings!.metadataProviderSettings)
.filter(([key, value]) => value.enabled)
.map(([key]) => key.charAt(0).toUpperCase() + key.slice(1));
@@ -347,7 +347,7 @@ export class BookService {
handleRemovedBookIds(removedBookIds: number[]): void {
const currentState = this.bookStateSubject.value;
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.includes(book.id)); // Check using includes() method
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.includes(book.id));
this.bookStateSubject.next({...currentState, books: filteredBooks});
}
@@ -366,4 +366,30 @@ export class BookService {
});
this.bookStateSubject.next({...currentState, books: updatedBooks})
}
toggleFieldLocks(bookIds: Set<number>, fieldActions: Record<string, 'LOCK' | 'UNLOCK'>): Observable<void> {
const requestBody = {
bookIds: Array.from(bookIds),
fieldActions: fieldActions
};
return this.http.put<BookMetadata[]>(`${this.url}/metadata/toggle-field-locks`, requestBody).pipe(
tap((updatedMetadataList) => {
const currentState = this.bookStateSubject.value;
const updatedBooks = (currentState.books || []).map(book => {
const updatedMetadata = updatedMetadataList.find(meta => meta.bookId === book.id);
return updatedMetadata ? { ...book, metadata: updatedMetadata } : book;
});
this.bookStateSubject.next({ ...currentState, books: updatedBooks });
}),
map(() => void 0),
catchError((error) => {
this.messageService.add({
severity: 'error',
summary: 'Field Lock Update Failed',
detail: 'Failed to update metadata field locks. Please try again.',
});
throw error;
})
);
}
}