fix(upload): add ChangeDetectorRef calls to ensure UI updates during file upload process (#557)

This commit is contained in:
brios
2026-04-22 17:27:38 -04:00
committed by Zach Yale
parent 13f282fe72
commit bf4fab3785
2 changed files with 13 additions and 3 deletions
@@ -168,6 +168,7 @@ export class AdditionalFileUploaderComponent implements OnInit, OnDestroy {
).subscribe({
next: () => {
uploadFile.status = 'Uploaded';
this.cdr.detectChanges();
if (--pending === 0) {
this.isUploading = false;
this.dialogRef.close({ success: true });
@@ -177,8 +178,10 @@ export class AdditionalFileUploaderComponent implements OnInit, OnDestroy {
uploadFile.status = 'Failed';
uploadFile.errorMessage = err?.error?.message || this.t.translate('book.fileUploader.toast.uploadFailedUnknown');
console.error('Upload failed for', uploadFile.file.name, err);
this.cdr.detectChanges();
if (--pending === 0) {
this.isUploading = false;
this.cdr.detectChanges();
}
}
});
@@ -1,4 +1,4 @@
import {Component, inject, ViewChild, effect} from '@angular/core';
import {ChangeDetectorRef, Component, inject, ViewChild, effect} from '@angular/core';
import {FileSelectEvent, FileUpload, FileUploadHandlerEvent} from 'primeng/fileupload';
import {Button} from 'primeng/button';
import {FormsModule} from '@angular/forms';
@@ -58,6 +58,7 @@ export class BookUploaderComponent {
private readonly http = inject(HttpClient);
private readonly ref = inject(DynamicDialogRef);
private readonly t = inject(TranslocoService);
private readonly cdr = inject(ChangeDetectorRef);
readonly libraries = this.libraryService.libraries;
maxFileSizeBytes?: number;
@@ -198,6 +199,7 @@ export class BookUploaderComponent {
if (batch.length === 0) {
this.isUploading = false;
this.uploadCompleted = true;
this.cdr.detectChanges();
if (destination === 'bookdrop') {
this.ref.close('uploaded_to_bookdrop');
}
@@ -231,11 +233,15 @@ export class BookUploaderComponent {
this.http.request(req).subscribe({
next: (event) => {
if (event.type === HttpEventType.UploadProgress && event.total) {
uploadFile.progress = Math.round((event.loaded / event.total) * 100);
if (event.type === HttpEventType.UploadProgress) {
if (event.total) {
uploadFile.progress = Math.round((event.loaded / event.total) * 100);
}
this.cdr.detectChanges();
} else if (event.type === HttpEventType.Response) {
uploadFile.status = 'Uploaded';
uploadFile.progress = 100;
this.cdr.detectChanges();
if (--pending === 0) {
setTimeout(() => {
this.uploadBatch(files, startIndex + batchSize, batchSize, destination, libraryId, pathId);
@@ -247,6 +253,7 @@ export class BookUploaderComponent {
uploadFile.status = 'Failed';
uploadFile.progress = 0;
uploadFile.errorMessage = err?.error?.message || this.t.translate('shared.bookUploader.toast.uploadFailedDefault');
this.cdr.detectChanges();
if (--pending === 0) {
setTimeout(() => {
this.uploadBatch(files, startIndex + batchSize, batchSize, destination, libraryId, pathId);