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()) {
@@ -33,10 +33,10 @@ {{ t('hardcover.syncLabel') }} + [ngModel]="hardcoverSyncEnabled()" + (ngModelChange)="onHardcoverSyncToggle($event)">

@@ -45,7 +45,7 @@

- @if (hardcoverSyncEnabled) { + @if (hardcoverSyncEnabled()) {
@@ -54,8 +54,9 @@ + (onClick)="copyText(hardcoverApiKey(), t('hardcover.apiKey'))" + [disabled]="!hardcoverApiKey()"> { + const user = this.userService.currentUser(); + return !!(user?.permissions.canSyncKoReader || user?.permissions.canSyncKobo || user?.permissions.admin); + }); + hardcoverSyncEnabled = signal(false); + hardcoverApiKey = signal(''); + showHardcoverApiKey = signal(false); private prevHasPermission = false; + private savedHardcoverSyncEnabled = false; + private savedHardcoverApiKey = ''; constructor() { effect(() => { - const user = this.userService.currentUser(); - if (!user) return; - - const currHasPermission = (user.permissions.canSyncKoReader - || user.permissions.canSyncKobo - || user.permissions.admin) ?? false; - this.hasPermission = currHasPermission; + const currHasPermission = this.hasPermission(); if (currHasPermission && !this.prevHasPermission) { this.loadHardcoverSettings(); } @@ -61,8 +60,9 @@ export class HardcoverSettingsComponent { takeUntilDestroyed(this.destroyRef) ).subscribe({ next: settings => { - this.hardcoverSyncEnabled = settings.hardcoverSyncEnabled ?? false; - this.hardcoverApiKey = settings.hardcoverApiKey ?? ''; + this.hardcoverSyncEnabled.set(settings.hardcoverSyncEnabled ?? false); + this.hardcoverApiKey.set(settings.hardcoverApiKey ?? ''); + this.rememberSavedSettings(); }, error: () => { this.messageService.add({ @@ -75,11 +75,12 @@ export class HardcoverSettingsComponent { } toggleShowHardcoverApiKey() { - this.showHardcoverApiKey = !this.showHardcoverApiKey; + this.showHardcoverApiKey.update(showHardcoverApiKey => !showHardcoverApiKey); } - onHardcoverSyncToggle() { - const message = this.hardcoverSyncEnabled + onHardcoverSyncToggle(enabled: boolean) { + this.hardcoverSyncEnabled.set(enabled); + const message = enabled ? this.t.translate('settingsDevice.hardcover.syncEnabledMsg') : this.t.translate('settingsDevice.hardcover.syncDisabledMsg'); this.updateHardcoverSettings(message); @@ -91,17 +92,19 @@ export class HardcoverSettingsComponent { private updateHardcoverSettings(successMessage: string) { this.hardcoverSyncSettingsService.updateSettings({ - hardcoverSyncEnabled: this.hardcoverSyncEnabled, - hardcoverApiKey: this.hardcoverApiKey + hardcoverSyncEnabled: this.hardcoverSyncEnabled(), + hardcoverApiKey: this.hardcoverApiKey() }).pipe( takeUntilDestroyed(this.destroyRef) ).subscribe({ next: settings => { - this.hardcoverSyncEnabled = settings.hardcoverSyncEnabled ?? false; - this.hardcoverApiKey = settings.hardcoverApiKey ?? ''; + this.hardcoverSyncEnabled.set(settings.hardcoverSyncEnabled ?? false); + this.hardcoverApiKey.set(settings.hardcoverApiKey ?? ''); + this.rememberSavedSettings(); this.messageService.add({severity: 'success', summary: this.t.translate('settingsDevice.hardcover.settingsUpdated'), detail: successMessage}); }, error: () => { + this.restoreSavedSettings(); this.messageService.add({ severity: 'error', summary: this.t.translate('settingsDevice.hardcover.updateFailed'), @@ -111,6 +114,16 @@ export class HardcoverSettingsComponent { }); } + private rememberSavedSettings() { + this.savedHardcoverSyncEnabled = this.hardcoverSyncEnabled(); + this.savedHardcoverApiKey = this.hardcoverApiKey(); + } + + private restoreSavedSettings() { + this.hardcoverSyncEnabled.set(this.savedHardcoverSyncEnabled); + this.hardcoverApiKey.set(this.savedHardcoverApiKey); + } + copyText(text: string, label: string = 'Text') { if (!text) { return; diff --git a/frontend/src/app/features/settings/device-settings/component/koreader-settings/koreader-settings-component.html b/frontend/src/app/features/settings/device-settings/component/koreader-settings/koreader-settings-component.html index c2b24b039..7ccb25657 100644 --- a/frontend/src/app/features/settings/device-settings/component/koreader-settings/koreader-settings-component.html +++ b/frontend/src/app/features/settings/device-settings/component/koreader-settings/koreader-settings-component.html @@ -13,7 +13,7 @@

- @if (hasPermission) { + @if (hasPermission()) {
@@ -34,10 +34,10 @@ {{ t('koreader.enableSync') }} + [disabled]="!credentialsSaved()">

@@ -52,10 +52,10 @@ {{ t('koreader.syncWithGrimmory') }} + [disabled]="!credentialsSaved() || !koReaderSyncEnabled()">

@@ -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"/> + (onClick)="copyText(koReaderUsername(), t('koreader.username'))" + [disabled]="!koReaderUsername()">

{{ 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"/> + (onClick)="copyText(koReaderPassword(), t('koreader.password'))" + [disabled]="!koReaderPassword()"> + [disabled]="!koReaderPassword()">

{{ t('koreader.passwordDesc') }}

- @if (editMode && passwordModel.invalid && passwordModel.touched) { + @if (!editMode() && passwordModel.invalid && passwordModel.touched) { {{ t('koreader.passwordMinLength') }} @@ -177,13 +179,13 @@
{{ t('koreader.settingsManagement') }} + [disabled]="!editMode() && !canSave()">

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: () =>