From f6661fec0b5660bb4d792142cedf7ed6a4ff7da6 Mon Sep 17 00:00:00 2001 From: Alex <25013571+alexhb1@users.noreply.github.com> Date: Fri, 5 Jun 2026 07:26:51 +0100 Subject: [PATCH] fix(ui): convert device settings state to signals (#1635) --- .../hardcover-settings-component.html | 21 +++--- .../hardcover-settings-component.ts | 55 +++++++++------ .../koreader-settings-component.html | 46 +++++++------ .../koreader-settings-component.ts | 69 ++++++++++--------- 4 files changed, 106 insertions(+), 85 deletions(-) diff --git a/frontend/src/app/features/settings/device-settings/component/hardcover-settings/hardcover-settings-component.html b/frontend/src/app/features/settings/device-settings/component/hardcover-settings/hardcover-settings-component.html index 8c5e1190b..394c5054a 100644 --- a/frontend/src/app/features/settings/device-settings/component/hardcover-settings/hardcover-settings-component.html +++ b/frontend/src/app/features/settings/device-settings/component/hardcover-settings/hardcover-settings-component.html @@ -13,7 +13,7 @@
- @if (hasPermission) { + @if (hasPermission()) {@@ -45,7 +45,7 @@
@@ -52,10 +52,10 @@
{{ t('koreader.syncWithGrimmory') }}
@@ -100,25 +100,26 @@
id="username"
name="username"
required
- [(ngModel)]="koReaderUsername"
+ [ngModel]="koReaderUsername()"
+ (ngModelChange)="koReaderUsername.set($event)"
#usernameModel="ngModel"
[class.p-invalid]="usernameModel.invalid && usernameModel.touched"
- [disabled]="editMode"
+ [disabled]="editMode()"
class="token-input"/>
{{ t('koreader.usernameDesc') }}
- @if (editMode && usernameModel.invalid && usernameModel.touched) { + @if (!editMode() && usernameModel.invalid && usernameModel.touched) { {{ t('koreader.usernameRequired') }} @@ -137,34 +138,35 @@ name="password" required minlength="6" - [type]="showPassword ? 'text' : 'password'" - [(ngModel)]="koReaderPassword" + [type]="showPassword() ? 'text' : 'password'" + [ngModel]="koReaderPassword()" + (ngModelChange)="koReaderPassword.set($event)" #passwordModel="ngModel" [class.p-invalid]="passwordModel.invalid && passwordModel.touched" - [disabled]="editMode" + [disabled]="editMode()" class="token-input-password"/>{{ t('koreader.passwordDesc') }}
- @if (editMode && passwordModel.invalid && passwordModel.touched) { + @if (!editMode() && passwordModel.invalid && passwordModel.touched) { {{ t('koreader.passwordMinLength') }} @@ -177,13 +179,13 @@diff --git a/frontend/src/app/features/settings/device-settings/component/koreader-settings/koreader-settings-component.ts b/frontend/src/app/features/settings/device-settings/component/koreader-settings/koreader-settings-component.ts index 47991d9ec..b882d1dfe 100644 --- a/frontend/src/app/features/settings/device-settings/component/koreader-settings/koreader-settings-component.ts +++ b/frontend/src/app/features/settings/device-settings/component/koreader-settings/koreader-settings-component.ts @@ -1,4 +1,4 @@ -import {Component, DestroyRef, effect, inject} from '@angular/core'; +import {Component, DestroyRef, computed, effect, inject, signal} from '@angular/core'; import {takeUntilDestroyed} from '@angular/core/rxjs-interop'; import {FormsModule} from '@angular/forms'; @@ -30,13 +30,13 @@ import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/tran styleUrls: ['./koreader-settings-component.scss'] }) export class KoreaderSettingsComponent { - editMode = true; - showPassword = false; - koReaderSyncEnabled = false; - syncWithWebReader = false; - koReaderUsername = ''; - koReaderPassword = ''; - credentialsSaved = false; + editMode = signal(true); + showPassword = signal(false); + koReaderSyncEnabled = signal(false); + syncWithWebReader = signal(false); + koReaderUsername = signal(''); + koReaderPassword = signal(''); + credentialsSaved = signal(false); readonly koreaderEndpoint = `${window.location.origin}/api/koreader`; private readonly messageService = inject(MessageService); @@ -45,16 +45,21 @@ export class KoreaderSettingsComponent { private readonly t = inject(TranslocoService); private readonly destroyRef = inject(DestroyRef); - hasPermission = false; + readonly hasPermission = computed(() => { + const user = this.userService.currentUser(); + return !!(user?.permissions.canSyncKoReader || user?.permissions.admin); + }); private prevHasPermission = false; + readonly canSave = computed(() => { + const username = this.koReaderUsername().trim(); + const password = this.koReaderPassword(); + return username.length > 0 && password.length >= 6; + }); + constructor() { effect(() => { - const user = this.userService.currentUser(); - if (!user) return; - - const currHasPermission = (user.permissions.canSyncKoReader || user.permissions.admin) ?? false; - this.hasPermission = currHasPermission; + const currHasPermission = this.hasPermission(); if (currHasPermission && !this.prevHasPermission) { this.loadKoreaderSettings(); } @@ -67,11 +72,11 @@ export class KoreaderSettingsComponent { takeUntilDestroyed(this.destroyRef) ).subscribe({ next: koreaderUser => { - this.koReaderUsername = koreaderUser.username; - this.koReaderPassword = koreaderUser.password; - this.koReaderSyncEnabled = koreaderUser.syncEnabled; - this.syncWithWebReader = koreaderUser.syncWithWebReader ?? false; - this.credentialsSaved = true; + this.koReaderUsername.set(koreaderUser.username); + this.koReaderPassword.set(koreaderUser.password); + this.koReaderSyncEnabled.set(koreaderUser.syncEnabled); + this.syncWithWebReader.set(koreaderUser.syncWithWebReader ?? false); + this.credentialsSaved.set(true); }, error: err => { if (err.status !== 404) { @@ -86,39 +91,38 @@ export class KoreaderSettingsComponent { } - get canSave(): boolean { - const u = this.koReaderUsername?.trim() ?? ''; - const p = this.koReaderPassword ?? ''; - return u.length > 0 && p.length >= 6; - } - onEditSave() { - if (!this.editMode) { + if (!this.editMode()) { this.saveCredentials(); } - this.editMode = !this.editMode; + this.editMode.update(editMode => !editMode); } onToggleEnabled(enabled: boolean) { + const previousEnabled = this.koReaderSyncEnabled(); + this.koReaderSyncEnabled.set(enabled); this.koreaderService.toggleSync(enabled).pipe( takeUntilDestroyed(this.destroyRef) ).subscribe({ next: () => { - this.koReaderSyncEnabled = enabled; + this.koReaderSyncEnabled.set(enabled); this.messageService.add({severity: 'success', summary: this.t.translate('settingsDevice.koreader.syncUpdated'), detail: enabled ? this.t.translate('settingsDevice.koreader.syncEnabled') : this.t.translate('settingsDevice.koreader.syncDisabled')}); }, error: () => { + this.koReaderSyncEnabled.set(previousEnabled); this.messageService.add({severity: 'error', summary: this.t.translate('settingsDevice.koreader.syncUpdateFailed'), detail: this.t.translate('settingsDevice.koreader.syncUpdateError')}); } }); } onToggleSyncWithWebReader(enabled: boolean) { + const previousSyncWithWebReader = this.syncWithWebReader(); + this.syncWithWebReader.set(enabled); this.koreaderService.toggleSyncProgressWithWebReader(enabled).pipe( takeUntilDestroyed(this.destroyRef) ).subscribe({ next: () => { - this.syncWithWebReader = enabled; + this.syncWithWebReader.set(enabled); this.messageService.add({ severity: 'success', summary: this.t.translate('settingsDevice.koreader.syncUpdated'), @@ -126,6 +130,7 @@ export class KoreaderSettingsComponent { }); }, error: () => { + this.syncWithWebReader.set(previousSyncWithWebReader); this.messageService.add({ severity: 'error', summary: this.t.translate('settingsDevice.koreader.syncUpdateFailed'), @@ -136,16 +141,16 @@ export class KoreaderSettingsComponent { } toggleShowPassword() { - this.showPassword = !this.showPassword; + this.showPassword.update(showPassword => !showPassword); } saveCredentials() { - this.koreaderService.createUser(this.koReaderUsername, this.koReaderPassword) + this.koreaderService.createUser(this.koReaderUsername(), this.koReaderPassword()) .pipe(takeUntilDestroyed(this.destroyRef)) .subscribe({ next: () => { - this.credentialsSaved = true; + this.credentialsSaved.set(true); this.messageService.add({severity: 'success', summary: this.t.translate('settingsDevice.koreader.saved'), detail: this.t.translate('settingsDevice.koreader.credentialsSaved')}); }, error: () =>