Add loader for long running operations (#1790)

This commit is contained in:
Aditya Chandel
2025-12-07 20:23:47 -07:00
committed by GitHub
parent b00bc5f908
commit 42d2e83599
8 changed files with 239 additions and 116 deletions
@@ -5,6 +5,8 @@ import {BookService} from './book.service';
import {readStatusLabels} from '../components/book-browser/book-filter/book-filter.component';
import {ReadStatus} from '../model/book.model';
import {ResetProgressTypes} from '../../../shared/constants/reset-progress-type';
import {finalize} from 'rxjs';
import {LoadingService} from '../../../core/services/loading.service';
@Injectable({
providedIn: 'root'
@@ -14,7 +16,7 @@ export class BookMenuService {
confirmationService = inject(ConfirmationService);
messageService = inject(MessageService);
bookService = inject(BookService);
loadingService = inject(LoadingService);
getMetadataMenuItems(
autoFetchMetadata: () => void,
@@ -46,6 +48,8 @@ export class BookMenuService {
}
getTieredMenuItems(selectedBooks: Set<number>): MenuItem[] {
const count = selectedBooks.size;
return [
{
label: 'Update Read Status',
@@ -54,30 +58,34 @@ export class BookMenuService {
label,
command: () => {
this.confirmationService.confirm({
message: `Are you sure you want to mark selected books as "${label}"?`,
message: `Are you sure you want to mark ${count} book(s) as "${label}"?`,
header: 'Confirm Read Status Update',
icon: 'pi pi-exclamation-triangle',
acceptLabel: 'Yes',
rejectLabel: 'No',
accept: () => {
this.bookService.updateBookReadStatus(Array.from(selectedBooks), status as ReadStatus).subscribe({
next: () => {
this.messageService.add({
severity: 'success',
summary: 'Read Status Updated',
detail: `Marked as "${label}"`,
life: 2000
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Update Failed',
detail: 'Could not update read status.',
life: 3000
});
}
});
const loader = this.loadingService.show(`Updating read status for ${count} book(s)...`);
this.bookService.updateBookReadStatus(Array.from(selectedBooks), status as ReadStatus)
.pipe(finalize(() => this.loadingService.hide(loader)))
.subscribe({
next: () => {
this.messageService.add({
severity: 'success',
summary: 'Read Status Updated',
detail: `Marked as "${label}"`,
life: 2000
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Update Failed',
detail: 'Could not update read status.',
life: 3000
});
}
});
}
});
}
@@ -88,30 +96,34 @@ export class BookMenuService {
icon: 'pi pi-undo',
command: () => {
this.confirmationService.confirm({
message: 'Are you sure you want to reset Booklore reading progress for selected books?',
message: `Are you sure you want to reset Booklore reading progress for ${count} book(s)?`,
header: 'Confirm Reset',
icon: 'pi pi-exclamation-triangle',
acceptLabel: 'Yes',
rejectLabel: 'No',
accept: () => {
this.bookService.resetProgress(Array.from(selectedBooks), ResetProgressTypes.BOOKLORE).subscribe({
next: () => {
this.messageService.add({
severity: 'success',
summary: 'Progress Reset',
detail: 'Booklore reading progress has been reset.',
life: 1500
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Could not reset progress.',
life: 1500
});
}
});
const loader = this.loadingService.show(`Resetting Booklore progress for ${count} book(s)...`);
this.bookService.resetProgress(Array.from(selectedBooks), ResetProgressTypes.BOOKLORE)
.pipe(finalize(() => this.loadingService.hide(loader)))
.subscribe({
next: () => {
this.messageService.add({
severity: 'success',
summary: 'Progress Reset',
detail: 'Booklore reading progress has been reset.',
life: 1500
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Could not reset progress.',
life: 1500
});
}
});
}
});
}
@@ -121,30 +133,34 @@ export class BookMenuService {
icon: 'pi pi-undo',
command: () => {
this.confirmationService.confirm({
message: 'Are you sure you want to reset KOReader reading progress for selected books?',
message: `Are you sure you want to reset KOReader reading progress for ${count} book(s)?`,
header: 'Confirm Reset',
icon: 'pi pi-exclamation-triangle',
acceptLabel: 'Yes',
rejectLabel: 'No',
accept: () => {
this.bookService.resetProgress(Array.from(selectedBooks), ResetProgressTypes.KOREADER).subscribe({
next: () => {
this.messageService.add({
severity: 'success',
summary: 'Progress Reset',
detail: 'KOReader reading progress has been reset.',
life: 1500
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Could not reset progress.',
life: 1500
});
}
});
const loader = this.loadingService.show(`Resetting KOReader progress for ${count} book(s)...`);
this.bookService.resetProgress(Array.from(selectedBooks), ResetProgressTypes.KOREADER)
.pipe(finalize(() => this.loadingService.hide(loader)))
.subscribe({
next: () => {
this.messageService.add({
severity: 'success',
summary: 'Progress Reset',
detail: 'KOReader reading progress has been reset.',
life: 1500
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Could not reset progress.',
life: 1500
});
}
});
}
});
}
@@ -16,6 +16,8 @@ import {TaskCreateRequest, TaskType} from '../../settings/task-management/task.s
import {MetadataRefreshRequest} from '../../metadata/model/request/metadata-refresh-request.model';
import {TaskHelperService} from '../../settings/task-management/task-helper.service';
import {UserService} from "../../settings/user-management/user.service";
import {LoadingService} from '../../../core/services/loading.service';
import {finalize} from 'rxjs';
@Injectable({
providedIn: 'root',
@@ -31,6 +33,7 @@ export class LibraryShelfMenuService {
private dialogService = inject(DialogService);
private magicShelfService = inject(MagicShelfService);
private userService = inject(UserService);
private loadingService = inject(LoadingService);
initializeLibraryMenuItems(entity: Library | Shelf | MagicShelf | null): MenuItem[] {
return [
@@ -130,19 +133,23 @@ export class LibraryShelfMenuService {
severity: 'danger',
},
accept: () => {
this.libraryService.deleteLibrary(entity?.id!).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library was deleted'});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete library',
});
}
});
const loader = this.loadingService.show(`Deleting library '${entity?.name}'...`);
this.libraryService.deleteLibrary(entity?.id!)
.pipe(finalize(() => this.loadingService.hide(loader)))
.subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Library was deleted'});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete library',
});
}
});
}
});
}