fix(loading): update loading checks to use signal() for reactive state management (#727)

This commit is contained in:
brios
2026-04-22 17:27:43 -04:00
committed by Zach Yale
parent 7a88b12430
commit 284e7bec49
59 changed files with 577 additions and 345 deletions
@@ -118,7 +118,7 @@
[label]="t('clearBtn')"
severity="danger"
[outlined]="true"
[disabled]="!filesPresent() || isUploading"
[disabled]="!filesPresent() || isUploading()"
[pTooltip]="t('clearTooltip')"
tooltipPosition="top"/>
</div>
@@ -127,11 +127,11 @@
</ng-template>
<ng-template #content let-files let-removeFileCallback="removeFileCallback">
@if (files?.length > 0) {
@if (isUploading || uploadCompleted) {
@if (isUploading() || uploadCompleted()) {
<div class="upload-progress-summary">
<div class="progress-row">
<span class="progress-status">
@if (isUploading) {
@if (isUploading()) {
<i class="pi pi-spin pi-spinner"></i>
{{ t('uploading') }}
} @else {
@@ -1,4 +1,4 @@
import {ChangeDetectorRef, Component, inject, ViewChild, effect} from '@angular/core';
import {ChangeDetectorRef, Component, inject, ViewChild, effect, signal} from '@angular/core';
import {FileSelectEvent, FileUpload, FileUploadHandlerEvent} from 'primeng/fileupload';
import {Button} from 'primeng/button';
import {FormsModule} from '@angular/forms';
@@ -47,8 +47,8 @@ export class BookUploaderComponent {
@ViewChild(FileUpload) fileUpload!: FileUpload;
files: UploadingFile[] = [];
isUploading: boolean = false;
uploadCompleted: boolean = false;
isUploading = signal(false);
uploadCompleted = signal(false);
_selectedLibrary: Library | null = null;
selectedPath: LibraryPath | null = null;
@@ -112,10 +112,14 @@ export class BookUploaderComponent {
onClear(clearCallback: () => void): void {
clearCallback();
this.files = [];
this.uploadCompleted.set(false);
}
onFilesSelect(event: FileSelectEvent): void {
this.uploadCompleted.set(false);
if (this.value === 'library' && (!this.selectedLibrary || !this.selectedPath)) {
this.messageService.add({
severity: 'error',
summary: this.t.translate('shared.bookUploader.toast.noDestinationSummary'),
@@ -185,8 +189,8 @@ export class BookUploaderComponent {
const filesToUpload = this.files.filter(f => f.status === 'Pending');
if (filesToUpload.length === 0) return;
this.isUploading = true;
this.uploadCompleted = false;
this.isUploading.set(true);
this.uploadCompleted.set(false);
const destination = this.value;
const libraryId = this.selectedLibrary?.id?.toString();
const pathId = this.selectedPath?.id?.toString();
@@ -197,9 +201,9 @@ export class BookUploaderComponent {
private uploadBatch(files: UploadingFile[], startIndex: number, batchSize: number, destination: string, libraryId?: string, pathId?: string): void {
const batch = files.slice(startIndex, startIndex + batchSize);
if (batch.length === 0) {
this.isUploading = false;
this.uploadCompleted = true;
this.cdr.detectChanges();
this.isUploading.set(false);
this.uploadCompleted.set(true);
if (destination === 'bookdrop') {
this.ref.close('uploaded_to_bookdrop');
}
@@ -266,9 +270,9 @@ export class BookUploaderComponent {
isChooseDisabled(): boolean {
if (this.value === 'bookdrop') {
return this.isUploading;
return this.isUploading();
}
return !this.selectedLibrary || !this.selectedPath || this.isUploading;
return !this.selectedLibrary || !this.selectedPath || this.isUploading();
}
isUploadDisabled(): boolean {
@@ -324,7 +328,7 @@ export class BookUploaderComponent {
}
hasUploadCompleted(): boolean {
return this.uploadCompleted;
return this.uploadCompleted();
}
closeDialog(): void {
@@ -95,7 +95,7 @@
</div>
<div class="directories-container">
@if (isLoading) {
@if (isLoading()) {
<div class="loading-state">
<p-progressSpinner
class="custom-spinner"
@@ -133,7 +133,7 @@
</span>
</div>
<div class="directories-grid" [class.fade-in]="!isLoading && filteredPaths.length > 0">
<div class="directories-grid" [class.fade-in]="!isLoading() && filteredPaths.length > 0">
@for (folder of filteredPaths; track folder) {
<div
class="directory-item"
@@ -46,13 +46,13 @@ describe('DirectoryPickerComponent', () => {
component.ngOnInit();
expect(utilityService.getFolders).toHaveBeenCalledWith('/');
expect(component.isLoading).toBe(true);
expect(component.isLoading()).toBe(true);
vi.advanceTimersByTime(100);
expect(component.paths).toEqual(['/books', '/comics']);
expect(component.filteredPaths).toEqual(['/books', '/comics']);
expect(component.isLoading).toBe(false);
expect(component.isLoading()).toBe(false);
expect(component.breadcrumbItems).toEqual([]);
});
@@ -127,7 +127,7 @@ describe('DirectoryPickerComponent', () => {
component.getFolders('/broken');
expect(component.isLoading).toBe(false);
expect(component.isLoading()).toBe(false);
expect(console.error).toHaveBeenCalled();
});
@@ -1,4 +1,4 @@
import {Component, inject, OnInit} from '@angular/core';
import {Component, inject, OnInit, signal} from '@angular/core';
import {DynamicDialogRef} from 'primeng/dynamicdialog';
import {UtilityService} from './utility.service';
import {TableModule} from 'primeng/table';
@@ -42,7 +42,7 @@ export class DirectoryPickerComponent implements OnInit {
selectedFolders: string[] = [];
selectedFoldersMap: Record<string, boolean> = {};
searchQuery: string = '';
isLoading: boolean = false;
isLoading = signal(false);
breadcrumbItems: MenuItem[] = [];
home: MenuItem = {icon: 'pi pi-home', command: () => this.navigateToRoot()};
@@ -55,14 +55,14 @@ export class DirectoryPickerComponent implements OnInit {
}
getFolders(path: string): void {
this.isLoading = true;
this.isLoading.set(true);
this.filteredPaths = [];
this.utilityService.getFolders(path).subscribe({
next: (folders: string[]) => {
setTimeout(() => {
this.paths = folders;
this.filteredPaths = folders;
this.isLoading = false;
this.isLoading.set(false);
this.updateBreadcrumb(path);
folders.forEach(folder => {
this.selectedFoldersMap[folder] = this.selectedFolders.includes(folder);
@@ -71,7 +71,7 @@ export class DirectoryPickerComponent implements OnInit {
},
error: (error) => {
console.error('Error fetching folders:', error);
this.isLoading = false;
this.isLoading.set(false);
}
});
}
@@ -153,9 +153,9 @@
<p-button
fluid
type="submit"
[label]="loading ? t('creatingAccountBtn') : t('createAccountBtn')"
[icon]="loading ? 'pi pi-spin pi-spinner' : 'pi pi-user-plus'"
[disabled]="loading || setupForm.invalid"
[label]="loading() ? t('creatingAccountBtn') : t('createAccountBtn')"
[icon]="loading() ? 'pi pi-spin pi-spinner' : 'pi pi-user-plus'"
[disabled]="loading() || setupForm.invalid"
class="setup-button"
/>
@@ -63,7 +63,7 @@ describe('SetupComponent', () => {
component.onSubmit();
expect(setupService.createAdmin).not.toHaveBeenCalled();
expect(component.loading).toBe(false);
expect(component.loading()).toBe(false);
});
it('submits the admin payload without confirmPassword', () => {
@@ -71,7 +71,7 @@ describe('SetupComponent', () => {
component.onSubmit();
expect(component.loading).toBe(true);
expect(component.loading()).toBe(true);
expect(component.error).toBe(null);
expect(setupService.createAdmin).toHaveBeenCalledWith({
name: 'Admin User',
@@ -87,7 +87,7 @@ describe('SetupComponent', () => {
component.onSubmit();
expect(component.success).toBe(true);
expect(component.loading).toBe(true);
expect(component.loading()).toBe(true);
expect(router.navigate).not.toHaveBeenCalled();
vi.advanceTimersByTime(1500);
@@ -104,7 +104,7 @@ describe('SetupComponent', () => {
component.onSubmit();
expect(component.loading).toBe(false);
expect(component.loading()).toBe(false);
expect(component.error).toBe('Setup failed');
expect(component.success).toBe(false);
});
@@ -1,4 +1,4 @@
import {Component, inject} from '@angular/core';
import {Component, inject, signal} from '@angular/core';
import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
import {Router} from '@angular/router';
import {SetupService} from './setup.service';
@@ -26,7 +26,7 @@ export class SetupComponent {
private setupService = inject(SetupService);
private router = inject(Router);
setupForm: FormGroup;
loading = false;
loading = signal(false);
error: string | null = null;
success = false;
private readonly t = inject(TranslocoService);
@@ -44,7 +44,7 @@ export class SetupComponent {
onSubmit(): void {
if (this.setupForm.invalid) return;
this.loading = true;
this.loading.set(true);
this.error = null;
// Remove confirm password from the payload, as it does not need to be sent to backend
const { confirmPassword, ...payload } = this.setupForm.value;
@@ -55,7 +55,7 @@ export class SetupComponent {
setTimeout(() => this.router.navigate(['/login']), 1500);
},
error: (err) => {
this.loading = false;
this.loading.set(false);
this.error =
err?.error?.message || this.t.translate('shared.setup.toast.createFailedDefault');
},