fix(ui): convert device settings state to signals (#1635)

This commit is contained in:
Alex
2026-06-05 07:26:51 +01:00
committed by GitHub
parent 7129d3cf82
commit f6661fec0b
4 changed files with 106 additions and 85 deletions
@@ -13,7 +13,7 @@
</p> </p>
</div> </div>
@if (hasPermission) { @if (hasPermission()) {
<div class="settings-content"> <div class="settings-content">
<div class="preferences-section"> <div class="preferences-section">
<div class="section-header"> <div class="section-header">
@@ -33,10 +33,10 @@
<span class="setting-label"> <span class="setting-label">
{{ t('hardcover.syncLabel') }}</span> {{ t('hardcover.syncLabel') }}</span>
<p-toggle-switch <p-toggle-switch
id="hardcoverSyncEnabled" inputId="hardcoverSyncEnabled"
name="hardcoverSyncEnabled" name="hardcoverSyncEnabled"
[(ngModel)]="hardcoverSyncEnabled" [ngModel]="hardcoverSyncEnabled()"
(ngModelChange)="onHardcoverSyncToggle()"> (ngModelChange)="onHardcoverSyncToggle($event)">
</p-toggle-switch> </p-toggle-switch>
</div> </div>
<p class="setting-description"> <p class="setting-description">
@@ -45,7 +45,7 @@
</div> </div>
</div> </div>
@if (hardcoverSyncEnabled) { @if (hardcoverSyncEnabled()) {
<div class="setting-item"> <div class="setting-item">
<div class="setting-info"> <div class="setting-info">
<div class="setting-label-row"> <div class="setting-label-row">
@@ -54,8 +54,9 @@
<input <input
pInputText pInputText
id="hardcoverApiKey" id="hardcoverApiKey"
[(ngModel)]="hardcoverApiKey" [ngModel]="hardcoverApiKey()"
[type]="showHardcoverApiKey ? 'text' : 'password'" (ngModelChange)="hardcoverApiKey.set($event)"
[type]="showHardcoverApiKey() ? 'text' : 'password'"
name="hardcoverApiKey" name="hardcoverApiKey"
class="token-input" class="token-input"
[placeholder]="t('hardcover.apiKeyPlaceholder')" [placeholder]="t('hardcover.apiKeyPlaceholder')"
@@ -66,11 +67,11 @@
outlined="true" outlined="true"
severity="info" severity="info"
size="small" size="small"
(onClick)="copyText(hardcoverApiKey, 'API Key')" (onClick)="copyText(hardcoverApiKey(), t('hardcover.apiKey'))"
[disabled]="!hardcoverApiKey"> [disabled]="!hardcoverApiKey()">
</p-button> </p-button>
<p-button <p-button
[icon]="showHardcoverApiKey ? 'pi pi-eye-slash' : 'pi pi-eye'" [icon]="showHardcoverApiKey() ? 'pi pi-eye-slash' : 'pi pi-eye'"
outlined="true" outlined="true"
severity="info" severity="info"
size="small" size="small"
@@ -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 {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {InputText} from 'primeng/inputtext'; import {InputText} from 'primeng/inputtext';
@@ -34,21 +34,20 @@ export class HardcoverSettingsComponent {
private readonly t = inject(TranslocoService); private readonly t = inject(TranslocoService);
private readonly destroyRef = inject(DestroyRef); private readonly destroyRef = inject(DestroyRef);
hasPermission = false; readonly hasPermission = computed(() => {
hardcoverSyncEnabled = false; const user = this.userService.currentUser();
hardcoverApiKey = ''; return !!(user?.permissions.canSyncKoReader || user?.permissions.canSyncKobo || user?.permissions.admin);
showHardcoverApiKey = false; });
hardcoverSyncEnabled = signal(false);
hardcoverApiKey = signal('');
showHardcoverApiKey = signal(false);
private prevHasPermission = false; private prevHasPermission = false;
private savedHardcoverSyncEnabled = false;
private savedHardcoverApiKey = '';
constructor() { constructor() {
effect(() => { effect(() => {
const user = this.userService.currentUser(); const currHasPermission = this.hasPermission();
if (!user) return;
const currHasPermission = (user.permissions.canSyncKoReader
|| user.permissions.canSyncKobo
|| user.permissions.admin) ?? false;
this.hasPermission = currHasPermission;
if (currHasPermission && !this.prevHasPermission) { if (currHasPermission && !this.prevHasPermission) {
this.loadHardcoverSettings(); this.loadHardcoverSettings();
} }
@@ -61,8 +60,9 @@ export class HardcoverSettingsComponent {
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
).subscribe({ ).subscribe({
next: settings => { next: settings => {
this.hardcoverSyncEnabled = settings.hardcoverSyncEnabled ?? false; this.hardcoverSyncEnabled.set(settings.hardcoverSyncEnabled ?? false);
this.hardcoverApiKey = settings.hardcoverApiKey ?? ''; this.hardcoverApiKey.set(settings.hardcoverApiKey ?? '');
this.rememberSavedSettings();
}, },
error: () => { error: () => {
this.messageService.add({ this.messageService.add({
@@ -75,11 +75,12 @@ export class HardcoverSettingsComponent {
} }
toggleShowHardcoverApiKey() { toggleShowHardcoverApiKey() {
this.showHardcoverApiKey = !this.showHardcoverApiKey; this.showHardcoverApiKey.update(showHardcoverApiKey => !showHardcoverApiKey);
} }
onHardcoverSyncToggle() { onHardcoverSyncToggle(enabled: boolean) {
const message = this.hardcoverSyncEnabled this.hardcoverSyncEnabled.set(enabled);
const message = enabled
? this.t.translate('settingsDevice.hardcover.syncEnabledMsg') ? this.t.translate('settingsDevice.hardcover.syncEnabledMsg')
: this.t.translate('settingsDevice.hardcover.syncDisabledMsg'); : this.t.translate('settingsDevice.hardcover.syncDisabledMsg');
this.updateHardcoverSettings(message); this.updateHardcoverSettings(message);
@@ -91,17 +92,19 @@ export class HardcoverSettingsComponent {
private updateHardcoverSettings(successMessage: string) { private updateHardcoverSettings(successMessage: string) {
this.hardcoverSyncSettingsService.updateSettings({ this.hardcoverSyncSettingsService.updateSettings({
hardcoverSyncEnabled: this.hardcoverSyncEnabled, hardcoverSyncEnabled: this.hardcoverSyncEnabled(),
hardcoverApiKey: this.hardcoverApiKey hardcoverApiKey: this.hardcoverApiKey()
}).pipe( }).pipe(
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
).subscribe({ ).subscribe({
next: settings => { next: settings => {
this.hardcoverSyncEnabled = settings.hardcoverSyncEnabled ?? false; this.hardcoverSyncEnabled.set(settings.hardcoverSyncEnabled ?? false);
this.hardcoverApiKey = settings.hardcoverApiKey ?? ''; this.hardcoverApiKey.set(settings.hardcoverApiKey ?? '');
this.rememberSavedSettings();
this.messageService.add({severity: 'success', summary: this.t.translate('settingsDevice.hardcover.settingsUpdated'), detail: successMessage}); this.messageService.add({severity: 'success', summary: this.t.translate('settingsDevice.hardcover.settingsUpdated'), detail: successMessage});
}, },
error: () => { error: () => {
this.restoreSavedSettings();
this.messageService.add({ this.messageService.add({
severity: 'error', severity: 'error',
summary: this.t.translate('settingsDevice.hardcover.updateFailed'), 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') { copyText(text: string, label: string = 'Text') {
if (!text) { if (!text) {
return; return;
@@ -13,7 +13,7 @@
</p> </p>
</div> </div>
@if (hasPermission) { @if (hasPermission()) {
<div class="settings-content"> <div class="settings-content">
<div class="preferences-section"> <div class="preferences-section">
<div class="section-header"> <div class="section-header">
@@ -34,10 +34,10 @@
<span class="setting-label">{{ t('koreader.enableSync') }}</span> <span class="setting-label">{{ t('koreader.enableSync') }}</span>
<p-toggle-switch <p-toggle-switch
name="syncEnabled" name="syncEnabled"
[(ngModel)]="koReaderSyncEnabled" [ngModel]="koReaderSyncEnabled()"
(ngModelChange)="onToggleEnabled($event)" (ngModelChange)="onToggleEnabled($event)"
inputId="syncEnabled" inputId="syncEnabled"
[disabled]="!credentialsSaved"> [disabled]="!credentialsSaved()">
</p-toggle-switch> </p-toggle-switch>
</div> </div>
<p class="setting-description"> <p class="setting-description">
@@ -52,10 +52,10 @@
<span class="setting-label">{{ t('koreader.syncWithGrimmory') }}</span> <span class="setting-label">{{ t('koreader.syncWithGrimmory') }}</span>
<p-toggle-switch <p-toggle-switch
name="syncWithWebReader" name="syncWithWebReader"
[(ngModel)]="syncWithWebReader" [ngModel]="syncWithWebReader()"
(ngModelChange)="onToggleSyncWithWebReader($event)" (ngModelChange)="onToggleSyncWithWebReader($event)"
inputId="syncWithWebReader" inputId="syncWithWebReader"
[disabled]="!credentialsSaved || !koReaderSyncEnabled"> [disabled]="!credentialsSaved() || !koReaderSyncEnabled()">
</p-toggle-switch> </p-toggle-switch>
</div> </div>
<p class="setting-description" [innerHTML]="t('koreader.syncWithGrimmoryDesc')"> <p class="setting-description" [innerHTML]="t('koreader.syncWithGrimmoryDesc')">
@@ -100,25 +100,26 @@
id="username" id="username"
name="username" name="username"
required required
[(ngModel)]="koReaderUsername" [ngModel]="koReaderUsername()"
(ngModelChange)="koReaderUsername.set($event)"
#usernameModel="ngModel" #usernameModel="ngModel"
[class.p-invalid]="usernameModel.invalid && usernameModel.touched" [class.p-invalid]="usernameModel.invalid && usernameModel.touched"
[disabled]="editMode" [disabled]="editMode()"
class="token-input"/> class="token-input"/>
<p-button <p-button
icon="pi pi-copy" icon="pi pi-copy"
outlined="true" outlined="true"
severity="info" severity="info"
size="small" size="small"
(onClick)="copyText(koReaderUsername, 'Username')" (onClick)="copyText(koReaderUsername(), t('koreader.username'))"
[disabled]="!koReaderUsername"> [disabled]="!koReaderUsername()">
</p-button> </p-button>
</div> </div>
</div> </div>
<p class="setting-description"> <p class="setting-description">
{{ t('koreader.usernameDesc') }} {{ t('koreader.usernameDesc') }}
</p> </p>
@if (editMode && usernameModel.invalid && usernameModel.touched) { @if (!editMode() && usernameModel.invalid && usernameModel.touched) {
<small class="error-message"> <small class="error-message">
{{ t('koreader.usernameRequired') }} {{ t('koreader.usernameRequired') }}
</small> </small>
@@ -137,34 +138,35 @@
name="password" name="password"
required required
minlength="6" minlength="6"
[type]="showPassword ? 'text' : 'password'" [type]="showPassword() ? 'text' : 'password'"
[(ngModel)]="koReaderPassword" [ngModel]="koReaderPassword()"
(ngModelChange)="koReaderPassword.set($event)"
#passwordModel="ngModel" #passwordModel="ngModel"
[class.p-invalid]="passwordModel.invalid && passwordModel.touched" [class.p-invalid]="passwordModel.invalid && passwordModel.touched"
[disabled]="editMode" [disabled]="editMode()"
class="token-input-password"/> class="token-input-password"/>
<p-button <p-button
icon="pi pi-copy" icon="pi pi-copy"
outlined="true" outlined="true"
severity="info" severity="info"
size="small" size="small"
(onClick)="copyText(koReaderPassword, 'Password')" (onClick)="copyText(koReaderPassword(), t('koreader.password'))"
[disabled]="!koReaderPassword"> [disabled]="!koReaderPassword()">
</p-button> </p-button>
<p-button <p-button
[icon]="showPassword ? 'pi pi-eye-slash' : 'pi pi-eye'" [icon]="showPassword() ? 'pi pi-eye-slash' : 'pi pi-eye'"
outlined="true" outlined="true"
severity="info" severity="info"
size="small" size="small"
(onClick)="toggleShowPassword()" (onClick)="toggleShowPassword()"
[disabled]="!koReaderPassword"> [disabled]="!koReaderPassword()">
</p-button> </p-button>
</div> </div>
</div> </div>
<p class="setting-description"> <p class="setting-description">
{{ t('koreader.passwordDesc') }} {{ t('koreader.passwordDesc') }}
</p> </p>
@if (editMode && passwordModel.invalid && passwordModel.touched) { @if (!editMode() && passwordModel.invalid && passwordModel.touched) {
<small class="error-message"> <small class="error-message">
{{ t('koreader.passwordMinLength') }} {{ t('koreader.passwordMinLength') }}
</small> </small>
@@ -177,13 +179,13 @@
<div class="setting-label-row"> <div class="setting-label-row">
<span class="setting-label">{{ t('koreader.settingsManagement') }}</span> <span class="setting-label">{{ t('koreader.settingsManagement') }}</span>
<p-button <p-button
[icon]="!editMode ? 'pi pi-save' : 'pi pi-pencil'" [icon]="!editMode() ? 'pi pi-save' : 'pi pi-pencil'"
outlined="true" outlined="true"
[severity]="!editMode ? 'success' : 'warn'" [severity]="!editMode() ? 'success' : 'warn'"
[label]="!editMode ? ('common.save' | transloco) : ('common.edit' | transloco)" [label]="!editMode() ? ('common.save' | transloco) : ('common.edit' | transloco)"
size="small" size="small"
(onClick)="onEditSave()" (onClick)="onEditSave()"
[disabled]="!editMode && !canSave"> [disabled]="!editMode() && !canSave()">
</p-button> </p-button>
</div> </div>
<p class="setting-description"> <p class="setting-description">
@@ -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 {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
@@ -30,13 +30,13 @@ import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/tran
styleUrls: ['./koreader-settings-component.scss'] styleUrls: ['./koreader-settings-component.scss']
}) })
export class KoreaderSettingsComponent { export class KoreaderSettingsComponent {
editMode = true; editMode = signal(true);
showPassword = false; showPassword = signal(false);
koReaderSyncEnabled = false; koReaderSyncEnabled = signal(false);
syncWithWebReader = false; syncWithWebReader = signal(false);
koReaderUsername = ''; koReaderUsername = signal('');
koReaderPassword = ''; koReaderPassword = signal('');
credentialsSaved = false; credentialsSaved = signal(false);
readonly koreaderEndpoint = `${window.location.origin}/api/koreader`; readonly koreaderEndpoint = `${window.location.origin}/api/koreader`;
private readonly messageService = inject(MessageService); private readonly messageService = inject(MessageService);
@@ -45,16 +45,21 @@ export class KoreaderSettingsComponent {
private readonly t = inject(TranslocoService); private readonly t = inject(TranslocoService);
private readonly destroyRef = inject(DestroyRef); 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; private prevHasPermission = false;
readonly canSave = computed(() => {
const username = this.koReaderUsername().trim();
const password = this.koReaderPassword();
return username.length > 0 && password.length >= 6;
});
constructor() { constructor() {
effect(() => { effect(() => {
const user = this.userService.currentUser(); const currHasPermission = this.hasPermission();
if (!user) return;
const currHasPermission = (user.permissions.canSyncKoReader || user.permissions.admin) ?? false;
this.hasPermission = currHasPermission;
if (currHasPermission && !this.prevHasPermission) { if (currHasPermission && !this.prevHasPermission) {
this.loadKoreaderSettings(); this.loadKoreaderSettings();
} }
@@ -67,11 +72,11 @@ export class KoreaderSettingsComponent {
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
).subscribe({ ).subscribe({
next: koreaderUser => { next: koreaderUser => {
this.koReaderUsername = koreaderUser.username; this.koReaderUsername.set(koreaderUser.username);
this.koReaderPassword = koreaderUser.password; this.koReaderPassword.set(koreaderUser.password);
this.koReaderSyncEnabled = koreaderUser.syncEnabled; this.koReaderSyncEnabled.set(koreaderUser.syncEnabled);
this.syncWithWebReader = koreaderUser.syncWithWebReader ?? false; this.syncWithWebReader.set(koreaderUser.syncWithWebReader ?? false);
this.credentialsSaved = true; this.credentialsSaved.set(true);
}, },
error: err => { error: err => {
if (err.status !== 404) { 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() { onEditSave() {
if (!this.editMode) { if (!this.editMode()) {
this.saveCredentials(); this.saveCredentials();
} }
this.editMode = !this.editMode; this.editMode.update(editMode => !editMode);
} }
onToggleEnabled(enabled: boolean) { onToggleEnabled(enabled: boolean) {
const previousEnabled = this.koReaderSyncEnabled();
this.koReaderSyncEnabled.set(enabled);
this.koreaderService.toggleSync(enabled).pipe( this.koreaderService.toggleSync(enabled).pipe(
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
).subscribe({ ).subscribe({
next: () => { 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')}); 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: () => { error: () => {
this.koReaderSyncEnabled.set(previousEnabled);
this.messageService.add({severity: 'error', summary: this.t.translate('settingsDevice.koreader.syncUpdateFailed'), detail: this.t.translate('settingsDevice.koreader.syncUpdateError')}); this.messageService.add({severity: 'error', summary: this.t.translate('settingsDevice.koreader.syncUpdateFailed'), detail: this.t.translate('settingsDevice.koreader.syncUpdateError')});
} }
}); });
} }
onToggleSyncWithWebReader(enabled: boolean) { onToggleSyncWithWebReader(enabled: boolean) {
const previousSyncWithWebReader = this.syncWithWebReader();
this.syncWithWebReader.set(enabled);
this.koreaderService.toggleSyncProgressWithWebReader(enabled).pipe( this.koreaderService.toggleSyncProgressWithWebReader(enabled).pipe(
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
).subscribe({ ).subscribe({
next: () => { next: () => {
this.syncWithWebReader = enabled; this.syncWithWebReader.set(enabled);
this.messageService.add({ this.messageService.add({
severity: 'success', severity: 'success',
summary: this.t.translate('settingsDevice.koreader.syncUpdated'), summary: this.t.translate('settingsDevice.koreader.syncUpdated'),
@@ -126,6 +130,7 @@ export class KoreaderSettingsComponent {
}); });
}, },
error: () => { error: () => {
this.syncWithWebReader.set(previousSyncWithWebReader);
this.messageService.add({ this.messageService.add({
severity: 'error', severity: 'error',
summary: this.t.translate('settingsDevice.koreader.syncUpdateFailed'), summary: this.t.translate('settingsDevice.koreader.syncUpdateFailed'),
@@ -136,16 +141,16 @@ export class KoreaderSettingsComponent {
} }
toggleShowPassword() { toggleShowPassword() {
this.showPassword = !this.showPassword; this.showPassword.update(showPassword => !showPassword);
} }
saveCredentials() { saveCredentials() {
this.koreaderService.createUser(this.koReaderUsername, this.koReaderPassword) this.koreaderService.createUser(this.koReaderUsername(), this.koReaderPassword())
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({ .subscribe({
next: () => { 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')}); this.messageService.add({severity: 'success', summary: this.t.translate('settingsDevice.koreader.saved'), detail: this.t.translate('settingsDevice.koreader.credentialsSaved')});
}, },
error: () => error: () =>