Kobo Phase 1: Enable Book Transfer/Sync
This commit is contained in:
committed by
Aditya Chandel
parent
a092d629c9
commit
6e6861b329
+128
@@ -0,0 +1,128 @@
|
||||
<p-toast position="top-right"></p-toast>
|
||||
|
||||
<p class="text-lg flex items-center gap-2 px-4 pt-4 pb-2">
|
||||
KOReader Sync Settings:
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="Click to view KOReader setup documentation"
|
||||
tooltipPosition="right"
|
||||
(click)="openKoReaderDocumentation()"
|
||||
style="cursor: pointer;">
|
||||
</i>
|
||||
</p>
|
||||
|
||||
@if (hasPermission) {
|
||||
<form #koreaderForm="ngForm" class="px-4 py-4">
|
||||
|
||||
<div class="p-field flex items-center px-4 py-2">
|
||||
<p-toggle-switch
|
||||
name="syncEnabled"
|
||||
[(ngModel)]="koReaderSyncEnabled"
|
||||
(ngModelChange)="onToggleEnabled($event)"
|
||||
inputId="syncEnabled"
|
||||
[disabled]="!credentialsSaved">
|
||||
</p-toggle-switch>
|
||||
<label for="syncEnabled" class="ml-2">
|
||||
Enable KOReader Sync
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col px-4 py-4 space-y-4">
|
||||
<!-- API Path -->
|
||||
<div class="flex flex-col gap-1 max-w-[30rem]">
|
||||
<label for="koreaderEndpoint">KOReader API Path</label>
|
||||
<div class="flex items-center gap-2">
|
||||
<input
|
||||
fluid
|
||||
pInputText
|
||||
id="koreaderEndpoint"
|
||||
[value]="koreaderEndpoint"
|
||||
readonly/>
|
||||
<p-button
|
||||
icon="pi pi-copy"
|
||||
outlined="true"
|
||||
severity="info"
|
||||
(onClick)="copyText(koreaderEndpoint)">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Username -->
|
||||
<div class="flex flex-col gap-1 max-w-[30rem]">
|
||||
<label for="username">KOReader Username</label>
|
||||
<div class="flex items-center gap-2">
|
||||
<input
|
||||
fluid
|
||||
pInputText
|
||||
id="username"
|
||||
name="username"
|
||||
required
|
||||
[(ngModel)]="koReaderUsername"
|
||||
#usernameModel="ngModel"
|
||||
[class.p-invalid]="usernameModel.invalid && usernameModel.touched"
|
||||
[disabled]="editMode"/>
|
||||
<p-button
|
||||
icon="pi pi-copy"
|
||||
outlined="true"
|
||||
severity="info"
|
||||
(onClick)="copyText(koReaderUsername)">
|
||||
</p-button>
|
||||
</div>
|
||||
<small *ngIf="editMode && usernameModel.invalid && usernameModel.touched" class="p-error">
|
||||
Username is required.
|
||||
</small>
|
||||
</div>
|
||||
|
||||
<!-- Password -->
|
||||
<div class="flex flex-col gap-1 max-w-[33rem]">
|
||||
<label for="password">KOReader Password</label>
|
||||
<div class="flex items-center gap-2">
|
||||
<input
|
||||
fluid
|
||||
pInputText
|
||||
id="password"
|
||||
name="password"
|
||||
required
|
||||
minlength="6"
|
||||
[type]="showPassword ? 'text' : 'password'"
|
||||
[(ngModel)]="koReaderPassword"
|
||||
#passwordModel="ngModel"
|
||||
[class.p-invalid]="passwordModel.invalid && passwordModel.touched"
|
||||
[disabled]="editMode"/>
|
||||
<p-button
|
||||
icon="pi pi-copy"
|
||||
outlined="true"
|
||||
severity="info"
|
||||
(onClick)="copyText(koReaderPassword)">
|
||||
</p-button>
|
||||
<p-button
|
||||
[icon]="showPassword ? 'pi pi-eye-slash' : 'pi pi-eye'"
|
||||
outlined="true"
|
||||
severity="info"
|
||||
(onClick)="toggleShowPassword()">
|
||||
</p-button>
|
||||
</div>
|
||||
<small *ngIf="editMode && passwordModel.invalid && passwordModel.touched" class="p-error">
|
||||
Password must be at least 6 characters.
|
||||
</small>
|
||||
</div>
|
||||
|
||||
<p-button
|
||||
[icon]="!editMode ? 'pi pi-save' : 'pi pi-pencil'"
|
||||
outlined="true"
|
||||
[severity]="!editMode ? 'success' : 'warn'"
|
||||
[label]="!editMode ? 'Save' : 'Edit'"
|
||||
(onClick)="onEditSave()"
|
||||
[disabled]="!editMode && !canSave">
|
||||
</p-button>
|
||||
</div>
|
||||
</form>
|
||||
} @else {
|
||||
<div class="px-4 py-4 m-4 rounded-lg bg-red-700/30 border border-red-600 text-red-200 flex items-center gap-2 max-w-lg">
|
||||
<i class="pi pi-lock text-red-400 text-xl"></i>
|
||||
<span>
|
||||
Access to KOReader sync is restricted.
|
||||
<br>
|
||||
Please contact your administrator to request permission.
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {ToggleSwitch} from 'primeng/toggleswitch';
|
||||
import {Button} from 'primeng/button';
|
||||
import {ToastModule} from 'primeng/toast';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {KoreaderService} from '../koreader.service';
|
||||
import {UserService} from '../../user-management/user.service';
|
||||
import {filter, takeUntil} from 'rxjs/operators';
|
||||
import {Subject} from 'rxjs';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
selector: 'app-koreader-settings-component',
|
||||
imports: [
|
||||
CommonModule,
|
||||
FormsModule,
|
||||
InputText,
|
||||
ToggleSwitch,
|
||||
Button,
|
||||
ToastModule,
|
||||
Tooltip
|
||||
],
|
||||
providers: [MessageService],
|
||||
templateUrl: './koreader-settings-component.html',
|
||||
styleUrls: ['./koreader-settings-component.scss']
|
||||
})
|
||||
export class KoreaderSettingsComponent implements OnInit, OnDestroy {
|
||||
editMode = true;
|
||||
showPassword = false;
|
||||
koReaderSyncEnabled = false;
|
||||
koReaderUsername = '';
|
||||
koReaderPassword = '';
|
||||
credentialsSaved = false;
|
||||
readonly koreaderEndpoint = `${window.location.origin}/api/koreader`;
|
||||
|
||||
private readonly messageService = inject(MessageService);
|
||||
private readonly koreaderService = inject(KoreaderService);
|
||||
private readonly userService = inject(UserService);
|
||||
|
||||
private readonly destroy$ = new Subject<void>();
|
||||
hasPermission = false;
|
||||
|
||||
ngOnInit() {
|
||||
this.userService.userState$.pipe(
|
||||
filter(userState => !!userState?.user && userState.loaded),
|
||||
takeUntil(this.destroy$)
|
||||
).subscribe(userState => {
|
||||
this.hasPermission = (userState.user?.permissions.canSyncKoReader || userState.user?.permissions.admin) ?? false;
|
||||
if (this.hasPermission) {
|
||||
this.loadKoreaderSettings();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private loadKoreaderSettings() {
|
||||
this.koreaderService.getUser().subscribe({
|
||||
next: koreaderUser => {
|
||||
this.koReaderUsername = koreaderUser.username;
|
||||
this.koReaderPassword = koreaderUser.password;
|
||||
this.koReaderSyncEnabled = koreaderUser.syncEnabled;
|
||||
this.credentialsSaved = true;
|
||||
},
|
||||
error: err => {
|
||||
if (err.status !== 404) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Load Error',
|
||||
detail: 'Unable to retrieve KOReader account. Please try again.'
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
get canSave(): boolean {
|
||||
const u = this.koReaderUsername?.trim() ?? '';
|
||||
const p = this.koReaderPassword ?? '';
|
||||
return u.length > 0 && p.length >= 6;
|
||||
}
|
||||
|
||||
onEditSave() {
|
||||
if (!this.editMode) {
|
||||
this.saveCredentials();
|
||||
}
|
||||
this.editMode = !this.editMode;
|
||||
}
|
||||
|
||||
onToggleEnabled(enabled: boolean) {
|
||||
this.koreaderService.toggleSync(enabled).subscribe({
|
||||
next: () => {
|
||||
this.koReaderSyncEnabled = enabled;
|
||||
this.messageService.add({severity: 'success', summary: 'Sync Updated', detail: `KOReader sync has been ${enabled ? 'enabled' : 'disabled'}.`});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({severity: 'error', summary: 'Update Failed', detail: 'Unable to update KOReader sync setting. Please try again.'});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toggleShowPassword() {
|
||||
this.showPassword = !this.showPassword;
|
||||
}
|
||||
|
||||
saveCredentials() {
|
||||
this.koreaderService.createUser(this.koReaderUsername, this.koReaderPassword)
|
||||
.subscribe({
|
||||
next: () => {
|
||||
this.credentialsSaved = true;
|
||||
this.messageService.add({severity: 'success', summary: 'Saved', detail: 'KOReader account saved successfully.'});
|
||||
},
|
||||
error: () =>
|
||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to save KOReader credentials. Please try again.'})
|
||||
});
|
||||
}
|
||||
|
||||
copyText(text: string) {
|
||||
if (!text) {
|
||||
return;
|
||||
}
|
||||
navigator.clipboard.writeText(text).catch(err => {
|
||||
console.error('Copy failed', err);
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
}
|
||||
|
||||
openKoReaderDocumentation() {
|
||||
window.open('https://booklore-app.github.io/booklore-docs/docs/devices/koreader', '_blank');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user