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({ ).subscribe({
next: () => { next: () => {
uploadFile.status = 'Uploaded'; uploadFile.status = 'Uploaded';
this.cdr.detectChanges();
if (--pending === 0) { if (--pending === 0) {
this.isUploading = false; this.isUploading = false;
this.dialogRef.close({ success: true }); this.dialogRef.close({ success: true });
@@ -177,8 +178,10 @@ export class AdditionalFileUploaderComponent implements OnInit, OnDestroy {
uploadFile.status = 'Failed'; uploadFile.status = 'Failed';
uploadFile.errorMessage = err?.error?.message || this.t.translate('book.fileUploader.toast.uploadFailedUnknown'); uploadFile.errorMessage = err?.error?.message || this.t.translate('book.fileUploader.toast.uploadFailedUnknown');
console.error('Upload failed for', uploadFile.file.name, err); console.error('Upload failed for', uploadFile.file.name, err);
this.cdr.detectChanges();
if (--pending === 0) { if (--pending === 0) {
this.isUploading = false; 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 {FileSelectEvent, FileUpload, FileUploadHandlerEvent} from 'primeng/fileupload';
import {Button} from 'primeng/button'; import {Button} from 'primeng/button';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
@@ -58,6 +58,7 @@ export class BookUploaderComponent {
private readonly http = inject(HttpClient); private readonly http = inject(HttpClient);
private readonly ref = inject(DynamicDialogRef); private readonly ref = inject(DynamicDialogRef);
private readonly t = inject(TranslocoService); private readonly t = inject(TranslocoService);
private readonly cdr = inject(ChangeDetectorRef);
readonly libraries = this.libraryService.libraries; readonly libraries = this.libraryService.libraries;
maxFileSizeBytes?: number; maxFileSizeBytes?: number;
@@ -198,6 +199,7 @@ export class BookUploaderComponent {
if (batch.length === 0) { if (batch.length === 0) {
this.isUploading = false; this.isUploading = false;
this.uploadCompleted = true; this.uploadCompleted = true;
this.cdr.detectChanges();
if (destination === 'bookdrop') { if (destination === 'bookdrop') {
this.ref.close('uploaded_to_bookdrop'); this.ref.close('uploaded_to_bookdrop');
} }
@@ -231,11 +233,15 @@ export class BookUploaderComponent {
this.http.request(req).subscribe({ this.http.request(req).subscribe({
next: (event) => { next: (event) => {
if (event.type === HttpEventType.UploadProgress && event.total) { if (event.type === HttpEventType.UploadProgress) {
if (event.total) {
uploadFile.progress = Math.round((event.loaded / event.total) * 100); uploadFile.progress = Math.round((event.loaded / event.total) * 100);
}
this.cdr.detectChanges();
} else if (event.type === HttpEventType.Response) { } else if (event.type === HttpEventType.Response) {
uploadFile.status = 'Uploaded'; uploadFile.status = 'Uploaded';
uploadFile.progress = 100; uploadFile.progress = 100;
this.cdr.detectChanges();
if (--pending === 0) { if (--pending === 0) {
setTimeout(() => { setTimeout(() => {
this.uploadBatch(files, startIndex + batchSize, batchSize, destination, libraryId, pathId); this.uploadBatch(files, startIndex + batchSize, batchSize, destination, libraryId, pathId);
@@ -247,6 +253,7 @@ export class BookUploaderComponent {
uploadFile.status = 'Failed'; uploadFile.status = 'Failed';
uploadFile.progress = 0; uploadFile.progress = 0;
uploadFile.errorMessage = err?.error?.message || this.t.translate('shared.bookUploader.toast.uploadFailedDefault'); uploadFile.errorMessage = err?.error?.message || this.t.translate('shared.bookUploader.toast.uploadFailedDefault');
this.cdr.detectChanges();
if (--pending === 0) { if (--pending === 0) {
setTimeout(() => { setTimeout(() => {
this.uploadBatch(files, startIndex + batchSize, batchSize, destination, libraryId, pathId); this.uploadBatch(files, startIndex + batchSize, batchSize, destination, libraryId, pathId);