fix(ui): use signals for OPDS users to improve reactivity (#1544)

This commit is contained in:
James Ward
2026-05-29 10:00:42 -04:00
committed by GitHub
parent 49a7593e42
commit fe513288ad
3 changed files with 31 additions and 29 deletions
@@ -44,7 +44,7 @@
</div> </div>
<p-toggleswitch <p-toggleswitch
[(ngModel)]="opdsEnabled" [(ngModel)]="opdsEnabled"
[disabled]="loading" [disabled]="loading()"
(onChange)="toggleOpdsServer()"> (onChange)="toggleOpdsServer()">
</p-toggleswitch> </p-toggleswitch>
</div> </div>
@@ -68,7 +68,7 @@
</div> </div>
<p-toggleswitch <p-toggleswitch
[(ngModel)]="komgaApiEnabled" [(ngModel)]="komgaApiEnabled"
[disabled]="loading" [disabled]="loading()"
(onChange)="toggleKomgaApi()"> (onChange)="toggleKomgaApi()">
</p-toggleswitch> </p-toggleswitch>
</div> </div>
@@ -93,7 +93,7 @@
</div> </div>
<p-toggleswitch <p-toggleswitch
[(ngModel)]="komgaGroupUnknown" [(ngModel)]="komgaGroupUnknown"
[disabled]="loading" [disabled]="loading()"
(onChange)="toggleKomgaGroupUnknown()"> (onChange)="toggleKomgaGroupUnknown()">
</p-toggleswitch> </p-toggleswitch>
</div> </div>
@@ -207,7 +207,7 @@
</div> </div>
<div class="section-body"> <div class="section-body">
@if (users.length === 0) { @if (users().length === 0) {
<div class="empty-state"> <div class="empty-state">
<i class="pi pi-users"></i> <i class="pi pi-users"></i>
<p class="empty-title">{{ t('users.emptyTitle') }}</p> <p class="empty-title">{{ t('users.emptyTitle') }}</p>
@@ -215,7 +215,7 @@
</div> </div>
} @else { } @else {
<div class="users-list"> <div class="users-list">
@for (user of users; track user.id) { @for (user of users(); track user.id) {
<div class="user-item"> <div class="user-item">
<div class="user-info"> <div class="user-info">
<div class="user-avatar"> <div class="user-avatar">
@@ -71,13 +71,13 @@ describe('OpdsSettings', () => {
component.ngOnInit(); component.ngOnInit();
TestBed.flushEffects(); TestBed.flushEffects();
expect(component.loading).toBe(true); expect(component.loading()).toBe(true);
appSettingsState.set(buildAppSettings()); appSettingsState.set(buildAppSettings());
TestBed.flushEffects(); TestBed.flushEffects();
expect(getUser).not.toHaveBeenCalled(); expect(getUser).not.toHaveBeenCalled();
expect(component.loading).toBe(false); expect(component.loading()).toBe(false);
fixture.destroy(); fixture.destroy();
}); });
@@ -100,8 +100,8 @@ describe('OpdsSettings', () => {
TestBed.flushEffects(); TestBed.flushEffects();
expect(getUser).toHaveBeenCalledOnce(); expect(getUser).toHaveBeenCalledOnce();
expect(component.loading).toBe(false); expect(component.loading()).toBe(false);
expect(component.users).toHaveLength(1); expect(component.users()).toHaveLength(1);
fixture.destroy(); fixture.destroy();
}); });
@@ -1,4 +1,4 @@
import {Component, DestroyRef, effect, inject, OnInit} from '@angular/core'; import {Component, DestroyRef, WritableSignal, effect, inject, signal, OnInit} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop'; import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {Button} from 'primeng/button'; import {Button} from 'primeng/button';
@@ -56,8 +56,8 @@ export class OpdsSettings implements OnInit {
private appSettingsService = inject(AppSettingsService); private appSettingsService = inject(AppSettingsService);
private t = inject(TranslocoService); private t = inject(TranslocoService);
users: OpdsUserV2[] = []; users: WritableSignal<OpdsUserV2[]> = signal([]);
loading = false; loading = signal(false);
showCreateUserDialog = false; showCreateUserDialog = false;
newUser: OpdsUserV2CreateRequest = {username: '', password: '', sortOrder: 'RECENT'}; newUser: OpdsUserV2CreateRequest = {username: '', password: '', sortOrder: 'RECENT'};
passwordVisibility: boolean[] = []; passwordVisibility: boolean[] = [];
@@ -87,8 +87,8 @@ export class OpdsSettings implements OnInit {
const user = this.userService.currentUser(); const user = this.userService.currentUser();
this.hasPermission = !!(user?.permissions.canAccessOpds || user?.permissions.admin); this.hasPermission = !!(user?.permissions.canAccessOpds || user?.permissions.admin);
if (!this.hasPermission) { if (!this.hasPermission) {
this.loading = false; this.loading.set(false);
this.users = []; this.users.set([]);
this.hasLoadedUsers = false; this.hasLoadedUsers = false;
} }
}); });
@@ -103,7 +103,7 @@ export class OpdsSettings implements OnInit {
} }
if (!settings) { if (!settings) {
this.loading = true; this.loading.set(true);
return; return;
} }
@@ -111,7 +111,7 @@ export class OpdsSettings implements OnInit {
}); });
ngOnInit(): void { ngOnInit(): void {
this.loading = true; this.loading.set(true);
} }
private applyAppSettings(settings: AppSettings): void { private applyAppSettings(settings: AppSettings): void {
@@ -124,14 +124,14 @@ export class OpdsSettings implements OnInit {
this.loadUsers(); this.loadUsers();
} }
} else { } else {
this.users = []; this.users.set([]);
this.loading = false; this.loading.set(false);
this.hasLoadedUsers = false; this.hasLoadedUsers = false;
} }
} }
private loadUsers(): void { private loadUsers(): void {
this.loading = true; this.loading.set(true);
this.hasLoadedUsers = true; this.hasLoadedUsers = true;
this.opdsService.getUser().pipe( this.opdsService.getUser().pipe(
takeUntilDestroyed(this.destroyRef), takeUntilDestroyed(this.destroyRef),
@@ -141,9 +141,9 @@ export class OpdsSettings implements OnInit {
return of([]); return of([]);
}) })
).subscribe(users => { ).subscribe(users => {
this.users = users; this.users.set([...users]);
this.passwordVisibility = new Array(users.length).fill(false); this.passwordVisibility = new Array(users.length).fill(false);
this.loading = false; this.loading.set(false);
}); });
} }
@@ -154,7 +154,7 @@ export class OpdsSettings implements OnInit {
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
).subscribe({ ).subscribe({
next: user => { next: user => {
this.users.push(user); this.users.update(users => [...users, user]);
this.resetCreateUserDialog(); this.resetCreateUserDialog();
this.showMessage('success', this.t.translate('common.success'), this.t.translate('settingsOpds.createSuccess')); this.showMessage('success', this.t.translate('common.success'), this.t.translate('settingsOpds.createSuccess'));
}, },
@@ -187,7 +187,7 @@ export class OpdsSettings implements OnInit {
return of(null); return of(null);
}) })
).subscribe(() => { ).subscribe(() => {
this.users = this.users.filter(u => u.id !== user.id); this.users.update(users => users.filter(u => u.id !== user.id));
this.showMessage('success', this.t.translate('common.success'), this.t.translate('settingsOpds.deleteSuccess')); this.showMessage('success', this.t.translate('common.success'), this.t.translate('settingsOpds.deleteSuccess'));
}); });
} }
@@ -207,7 +207,7 @@ export class OpdsSettings implements OnInit {
if (this.opdsEnabled || this.komgaApiEnabled) { if (this.opdsEnabled || this.komgaApiEnabled) {
this.loadUsers(); this.loadUsers();
} else { } else {
this.users = []; this.users.set([]);
} }
} }
@@ -216,7 +216,7 @@ export class OpdsSettings implements OnInit {
if (this.opdsEnabled || this.komgaApiEnabled) { if (this.opdsEnabled || this.komgaApiEnabled) {
this.loadUsers(); this.loadUsers();
} else { } else {
this.users = []; this.users.set([]);
} }
} }
@@ -311,10 +311,12 @@ export class OpdsSettings implements OnInit {
}) })
).subscribe(updatedUser => { ).subscribe(updatedUser => {
if (updatedUser) { if (updatedUser) {
const index = this.users.findIndex(u => u.id === user.id); this.users.update(
if (index !== -1) { users => users.map(
this.users[index] = updatedUser; u => (u.id === user.id ? updatedUser : u)
} )
);
this.showMessage('success', this.t.translate('common.success'), this.t.translate('settingsOpds.sortUpdateSuccess')); this.showMessage('success', this.t.translate('common.success'), this.t.translate('settingsOpds.sortUpdateSuccess'));
} }
this.cancelEdit(); this.cancelEdit();