Feature/koreader hardcover sync (#2204)
* feat(settings): add hardcover sync settings and integrate into device settings UI * feat(hardcover): implement Hardcover sync settings API and UI integration * test(hardcover): add unit tests for HardcoverSyncSettingsService and HardcoverSettingsComponent * fix(hardcover-settings): correct API key copy function to handle null values properly --------- Co-authored-by: akiraslingshot <akiraslingshot@gmail.com> Co-authored-by: ACX <8075870+acx10@users.noreply.github.com>
This commit is contained in:
co-authored by
akiraslingshot
ACX
parent
20ed3e5b25
commit
77bb2904e3
+105
@@ -0,0 +1,105 @@
|
||||
<p-toast position="top-right"></p-toast>
|
||||
|
||||
<div class="main-container enclosing-container">
|
||||
<div class="settings-header">
|
||||
<h2 class="settings-title">
|
||||
<i class="pi pi-book"></i>
|
||||
Hardcover Integration
|
||||
<app-external-doc-link docType="hardcover"></app-external-doc-link>
|
||||
</h2>
|
||||
<p class="settings-description">
|
||||
Sync your reading progress from Booklore to your Hardcover account.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@if (hasPermission) {
|
||||
<div class="settings-content">
|
||||
<div class="preferences-section">
|
||||
<div class="section-header">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-sync"></i>
|
||||
Sync Configuration
|
||||
</h3>
|
||||
<p class="section-description">
|
||||
Enable and configure synchronization between Booklore and Hardcover.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="settings-card">
|
||||
<div class="setting-item">
|
||||
<div class="setting-info">
|
||||
<div class="setting-label-row">
|
||||
<label class="setting-label">Sync Reading Progress to Hardcover</label>
|
||||
<p-toggle-switch
|
||||
id="hardcoverSyncEnabled"
|
||||
name="hardcoverSyncEnabled"
|
||||
[(ngModel)]="hardcoverSyncEnabled"
|
||||
(ngModelChange)="onHardcoverSyncToggle()">
|
||||
</p-toggle-switch>
|
||||
</div>
|
||||
<p class="setting-description">
|
||||
When enabled, your reading progress will be automatically synced to your Hardcover account.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (hardcoverSyncEnabled) {
|
||||
<div class="setting-item">
|
||||
<div class="setting-info">
|
||||
<div class="setting-label-row">
|
||||
<label class="setting-label">Hardcover API Key</label>
|
||||
<div class="token-input-group">
|
||||
<input
|
||||
pInputText
|
||||
id="hardcoverApiKey"
|
||||
[(ngModel)]="hardcoverApiKey"
|
||||
[type]="showHardcoverApiKey ? 'text' : 'password'"
|
||||
name="hardcoverApiKey"
|
||||
class="token-input"
|
||||
placeholder="Enter your Hardcover API key"
|
||||
(blur)="onHardcoverApiKeyChange()"
|
||||
/>
|
||||
<p-button
|
||||
icon="pi pi-copy"
|
||||
outlined="true"
|
||||
severity="info"
|
||||
size="small"
|
||||
(onClick)="copyText(hardcoverApiKey, 'API Key')"
|
||||
[disabled]="!hardcoverApiKey">
|
||||
</p-button>
|
||||
<p-button
|
||||
[icon]="showHardcoverApiKey ? 'pi pi-eye-slash' : 'pi pi-eye'"
|
||||
outlined="true"
|
||||
severity="info"
|
||||
size="small"
|
||||
(onClick)="toggleShowHardcoverApiKey()">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
<p class="setting-description">
|
||||
Your personal Hardcover API key. Get it from
|
||||
<a href="https://hardcover.app/account/api" target="_blank" rel="noopener">hardcover.app/account/api</a>.
|
||||
This key is used only for syncing your reading progress and is not shared with other users.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
} @else {
|
||||
<div class="settings-content">
|
||||
<div class="access-denied-card">
|
||||
<i class="pi pi-lock"></i>
|
||||
<div class="access-denied-content">
|
||||
<h3>Access Restricted</h3>
|
||||
<p>
|
||||
Access to Hardcover sync is restricted.
|
||||
<br>
|
||||
Please contact your administrator to request permission.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
.main-container {
|
||||
width: 100%;
|
||||
padding: 1rem;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.enclosing-container {
|
||||
border-color: var(--p-content-border-color);
|
||||
background: var(--p-content-background);
|
||||
}
|
||||
|
||||
.settings-header {
|
||||
margin-top: 1rem;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.settings-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
font-size: 1.25rem;
|
||||
font-weight: 700;
|
||||
color: var(--p-text-color);
|
||||
margin: 0 0 0.75rem 0;
|
||||
|
||||
.pi {
|
||||
color: var(--p-primary-color);
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
}
|
||||
|
||||
.settings-description {
|
||||
color: var(--p-text-muted-color);
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.5;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.settings-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2rem;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
padding-left: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.preferences-section {
|
||||
@media (max-width: 768px) {
|
||||
padding: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.section-header {
|
||||
margin-bottom: 1rem;
|
||||
margin-left: 1rem;
|
||||
margin-right: 1rem;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
color: var(--p-text-color);
|
||||
margin: 0 0 0.5rem 0;
|
||||
|
||||
.pi {
|
||||
color: var(--p-primary-color);
|
||||
}
|
||||
}
|
||||
|
||||
.section-description {
|
||||
color: var(--p-text-muted-color);
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.5;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.settings-card {
|
||||
border: 1px solid var(--p-content-border-color);
|
||||
border-radius: 8px;
|
||||
background: var(--p-content-background);
|
||||
padding: 1rem 1.5rem 1.5rem 1.5rem;
|
||||
margin-left: 1rem;
|
||||
margin-right: 1rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2rem;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
padding: 1rem;
|
||||
margin-left: 0;
|
||||
margin-right: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.setting-item {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 2rem;
|
||||
padding: 0.5rem 0;
|
||||
|
||||
&:last-child {
|
||||
border-bottom: none;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
padding: 0.5rem 0;
|
||||
}
|
||||
}
|
||||
|
||||
.setting-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
|
||||
.setting-label {
|
||||
display: block;
|
||||
font-weight: 600;
|
||||
color: var(--p-text-color);
|
||||
font-size: 1rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.setting-label-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-bottom: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
|
||||
.setting-label {
|
||||
margin-bottom: 0;
|
||||
flex-shrink: 0;
|
||||
min-width: 150px;
|
||||
}
|
||||
|
||||
.token-input-group {
|
||||
flex: 1;
|
||||
min-width: 300px;
|
||||
max-width: 500px;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
min-width: 250px;
|
||||
max-width: 100%;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.setting-description {
|
||||
color: var(--p-text-muted-color);
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.5;
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.token-input-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
width: 100%;
|
||||
|
||||
.token-input {
|
||||
flex: 1;
|
||||
min-width: 200px;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
min-width: 150px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.access-denied-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
border: 1px solid var(--p-content-border-color);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
background: var(--p-content-background);
|
||||
margin: 0 1rem;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.pi {
|
||||
font-size: 1.5rem;
|
||||
color: var(--p-text-muted-color);
|
||||
}
|
||||
}
|
||||
|
||||
.access-denied-content {
|
||||
h3 {
|
||||
margin: 0 0 0.25rem 0;
|
||||
font-size: 1rem;
|
||||
color: var(--p-text-color);
|
||||
}
|
||||
|
||||
p {
|
||||
margin: 0;
|
||||
color: var(--p-text-muted-color);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||
import {BehaviorSubject, of, throwError} from 'rxjs';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {NO_ERRORS_SCHEMA} from '@angular/core';
|
||||
import {HardcoverSettingsComponent} from './hardcover-settings-component';
|
||||
import {HardcoverSyncSettingsService} from './hardcover-sync-settings.service';
|
||||
import {UserService, UserState} from '../../../user-management/user.service';
|
||||
|
||||
describe('HardcoverSettingsComponent', () => {
|
||||
let fixture: ComponentFixture<HardcoverSettingsComponent>;
|
||||
let component: HardcoverSettingsComponent;
|
||||
let settingsServiceMock: any;
|
||||
let messageServiceMock: any;
|
||||
let userState$: BehaviorSubject<UserState>;
|
||||
|
||||
const makeState = (permissions: Partial<UserState['user']> = {}): UserState => ({
|
||||
loaded: true,
|
||||
error: null,
|
||||
user: {
|
||||
id: 1,
|
||||
username: 'u',
|
||||
name: 'User',
|
||||
email: 'u@example.com',
|
||||
assignedLibraries: [],
|
||||
provisioningMethod: 'LOCAL',
|
||||
permissions: {
|
||||
admin: false,
|
||||
canUpload: false,
|
||||
canDownload: false,
|
||||
canEmailBook: false,
|
||||
canDeleteBook: false,
|
||||
canEditMetadata: false,
|
||||
canManageLibrary: false,
|
||||
canManageMetadataConfig: false,
|
||||
canSyncKoReader: false,
|
||||
canSyncKobo: false,
|
||||
canAccessOpds: false,
|
||||
canAccessBookdrop: false,
|
||||
canAccessLibraryStats: false,
|
||||
canAccessUserStats: false,
|
||||
canAccessTaskManager: false,
|
||||
canManageEmailConfig: false,
|
||||
canManageGlobalPreferences: false,
|
||||
canManageIcons: false,
|
||||
demoUser: false,
|
||||
canBulkAutoFetchMetadata: false,
|
||||
canBulkCustomFetchMetadata: false,
|
||||
canBulkEditMetadata: false,
|
||||
canBulkRegenerateCover: false,
|
||||
canMoveOrganizeFiles: false,
|
||||
canBulkLockUnlockMetadata: false
|
||||
},
|
||||
userSettings: {} as any,
|
||||
...permissions
|
||||
}
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
settingsServiceMock = {
|
||||
getSettings: vi.fn(),
|
||||
updateSettings: vi.fn()
|
||||
};
|
||||
messageServiceMock = {
|
||||
add: vi.fn()
|
||||
};
|
||||
userState$ = new BehaviorSubject<UserState>(makeState());
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [HardcoverSettingsComponent],
|
||||
providers: [
|
||||
{provide: HardcoverSyncSettingsService, useValue: settingsServiceMock},
|
||||
{provide: MessageService, useValue: messageServiceMock},
|
||||
{provide: UserService, useValue: {userState$: userState$}}
|
||||
],
|
||||
schemas: [NO_ERRORS_SCHEMA]
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(HardcoverSettingsComponent);
|
||||
component = fixture.componentInstance;
|
||||
});
|
||||
|
||||
it('should load settings when user has permission', () => {
|
||||
settingsServiceMock.getSettings.mockReturnValue(of({hardcoverSyncEnabled: true, hardcoverApiKey: 'key'}));
|
||||
userState$.next(makeState({
|
||||
permissions: {
|
||||
...makeState().user!.permissions,
|
||||
canSyncKobo: true
|
||||
}
|
||||
}) as UserState);
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(settingsServiceMock.getSettings).toHaveBeenCalled();
|
||||
expect(component.hardcoverSyncEnabled).toBe(true);
|
||||
expect(component.hardcoverApiKey).toBe('key');
|
||||
});
|
||||
|
||||
it('should not load settings without permission', () => {
|
||||
settingsServiceMock.getSettings.mockReturnValue(of({}));
|
||||
userState$.next(makeState());
|
||||
fixture.detectChanges();
|
||||
expect(settingsServiceMock.getSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should update settings on toggle', () => {
|
||||
settingsServiceMock.updateSettings.mockReturnValue(of({hardcoverSyncEnabled: false, hardcoverApiKey: 'k'}));
|
||||
component.hardcoverSyncEnabled = false;
|
||||
component.hardcoverApiKey = 'k';
|
||||
|
||||
component.onHardcoverSyncToggle();
|
||||
|
||||
expect(settingsServiceMock.updateSettings).toHaveBeenCalledWith({
|
||||
hardcoverSyncEnabled: false,
|
||||
hardcoverApiKey: 'k'
|
||||
});
|
||||
expect(messageServiceMock.add).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle update error', () => {
|
||||
settingsServiceMock.updateSettings.mockReturnValue(throwError(() => new Error('fail')));
|
||||
component.hardcoverSyncEnabled = true;
|
||||
component.hardcoverApiKey = 'k';
|
||||
|
||||
component.onHardcoverApiKeyChange();
|
||||
|
||||
expect(settingsServiceMock.updateSettings).toHaveBeenCalled();
|
||||
expect(messageServiceMock.add).toHaveBeenCalledWith(expect.objectContaining({
|
||||
severity: 'error'
|
||||
}));
|
||||
});
|
||||
|
||||
it('should handle load error', () => {
|
||||
settingsServiceMock.getSettings.mockReturnValue(throwError(() => new Error('fail')));
|
||||
userState$.next(makeState({
|
||||
permissions: {
|
||||
...makeState().user!.permissions,
|
||||
admin: true
|
||||
}
|
||||
}) as UserState);
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(settingsServiceMock.getSettings).toHaveBeenCalled();
|
||||
expect(messageServiceMock.add).toHaveBeenCalledWith(expect.objectContaining({
|
||||
severity: 'error'
|
||||
}));
|
||||
});
|
||||
});
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
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 {Subject} from 'rxjs';
|
||||
import {filter, takeUntil} from 'rxjs/operators';
|
||||
import {ExternalDocLinkComponent} from '../../../../../shared/components/external-doc-link/external-doc-link.component';
|
||||
import {UserService} from '../../../user-management/user.service';
|
||||
import {HardcoverSyncSettingsService} from './hardcover-sync-settings.service';
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
selector: 'app-hardcover-settings-component',
|
||||
imports: [
|
||||
FormsModule,
|
||||
InputText,
|
||||
ToggleSwitch,
|
||||
Button,
|
||||
ToastModule,
|
||||
ExternalDocLinkComponent
|
||||
],
|
||||
providers: [MessageService],
|
||||
templateUrl: './hardcover-settings-component.html',
|
||||
styleUrls: ['./hardcover-settings-component.scss']
|
||||
})
|
||||
export class HardcoverSettingsComponent implements OnInit, OnDestroy {
|
||||
private readonly messageService = inject(MessageService);
|
||||
private readonly hardcoverSyncSettingsService = inject(HardcoverSyncSettingsService);
|
||||
private readonly userService = inject(UserService);
|
||||
private readonly destroy$ = new Subject<void>();
|
||||
|
||||
hasPermission = false;
|
||||
hardcoverSyncEnabled = false;
|
||||
hardcoverApiKey = '';
|
||||
showHardcoverApiKey = false;
|
||||
|
||||
ngOnInit() {
|
||||
let prevHasPermission = false;
|
||||
this.userService.userState$.pipe(
|
||||
filter(userState => !!userState?.user && userState.loaded),
|
||||
takeUntil(this.destroy$)
|
||||
).subscribe(userState => {
|
||||
const currHasPermission = (userState.user?.permissions.canSyncKoReader
|
||||
|| userState.user?.permissions.canSyncKobo
|
||||
|| userState.user?.permissions.admin) ?? false;
|
||||
this.hasPermission = currHasPermission;
|
||||
if (currHasPermission && !prevHasPermission) {
|
||||
this.loadHardcoverSettings();
|
||||
}
|
||||
prevHasPermission = currHasPermission;
|
||||
});
|
||||
}
|
||||
|
||||
private loadHardcoverSettings() {
|
||||
this.hardcoverSyncSettingsService.getSettings().subscribe({
|
||||
next: settings => {
|
||||
this.hardcoverSyncEnabled = settings.hardcoverSyncEnabled ?? false;
|
||||
this.hardcoverApiKey = settings.hardcoverApiKey ?? '';
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Load Error',
|
||||
detail: 'Unable to retrieve Hardcover sync settings. Please try again.'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toggleShowHardcoverApiKey() {
|
||||
this.showHardcoverApiKey = !this.showHardcoverApiKey;
|
||||
}
|
||||
|
||||
onHardcoverSyncToggle() {
|
||||
const message = this.hardcoverSyncEnabled
|
||||
? 'Hardcover sync enabled'
|
||||
: 'Hardcover sync disabled';
|
||||
this.updateHardcoverSettings(message);
|
||||
}
|
||||
|
||||
onHardcoverApiKeyChange() {
|
||||
this.updateHardcoverSettings('Hardcover API key updated');
|
||||
}
|
||||
|
||||
private updateHardcoverSettings(successMessage: string) {
|
||||
this.hardcoverSyncSettingsService.updateSettings({
|
||||
hardcoverSyncEnabled: this.hardcoverSyncEnabled,
|
||||
hardcoverApiKey: this.hardcoverApiKey
|
||||
}).subscribe({
|
||||
next: settings => {
|
||||
this.hardcoverSyncEnabled = settings.hardcoverSyncEnabled ?? false;
|
||||
this.hardcoverApiKey = settings.hardcoverApiKey ?? '';
|
||||
this.messageService.add({severity: 'success', summary: 'Settings Updated', detail: successMessage});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Update Failed',
|
||||
detail: 'Unable to update Hardcover sync settings. Please try again.'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
copyText(text: string, label: string = 'Text') {
|
||||
if (!text) {
|
||||
return;
|
||||
}
|
||||
navigator.clipboard.writeText(text).then(() => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Copied',
|
||||
detail: `${label} copied to clipboard`
|
||||
});
|
||||
}).catch(err => {
|
||||
console.error('Copy failed', err);
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Copy Failed',
|
||||
detail: `Unable to copy ${label.toLowerCase()} to clipboard`
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {EnvironmentInjector, runInInjectionContext} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {of, throwError} from 'rxjs';
|
||||
import {HardcoverSyncSettingsService, HardcoverSyncSettings} from './hardcover-sync-settings.service';
|
||||
|
||||
describe('HardcoverSyncSettingsService', () => {
|
||||
let service: HardcoverSyncSettingsService;
|
||||
let httpClientMock: any;
|
||||
|
||||
const mockSettings: HardcoverSyncSettings = {
|
||||
hardcoverApiKey: 'hardcover-key',
|
||||
hardcoverSyncEnabled: true
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
get: vi.fn(),
|
||||
put: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
HardcoverSyncSettingsService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(injector, () => TestBed.inject(HardcoverSyncSettingsService));
|
||||
});
|
||||
|
||||
it('should get Hardcover sync settings', () => {
|
||||
httpClientMock.get.mockReturnValue(of(mockSettings));
|
||||
service.getSettings().subscribe(settings => {
|
||||
expect(settings).toEqual(mockSettings);
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringContaining('/api/v1/hardcover-sync-settings'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should update Hardcover sync settings', () => {
|
||||
httpClientMock.put.mockReturnValue(of(mockSettings));
|
||||
service.updateSettings(mockSettings).subscribe(settings => {
|
||||
expect(settings).toEqual(mockSettings);
|
||||
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringContaining('/api/v1/hardcover-sync-settings'), mockSettings);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle getSettings error', () => {
|
||||
httpClientMock.get.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.getSettings().subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle updateSettings error', () => {
|
||||
httpClientMock.put.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.updateSettings(mockSettings).subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('HardcoverSyncSettingsService - API Contract Tests', () => {
|
||||
let service: HardcoverSyncSettingsService;
|
||||
let httpClientMock: any;
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
get: vi.fn(),
|
||||
put: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
HardcoverSyncSettingsService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(injector, () => TestBed.inject(HardcoverSyncSettingsService));
|
||||
});
|
||||
|
||||
it('should call correct endpoint for getSettings', () => {
|
||||
httpClientMock.get.mockReturnValue(of({}));
|
||||
service.getSettings().subscribe();
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v1\/hardcover-sync-settings$/));
|
||||
});
|
||||
|
||||
it('should call correct endpoint for updateSettings', () => {
|
||||
httpClientMock.put.mockReturnValue(of({}));
|
||||
const settings: HardcoverSyncSettings = {
|
||||
hardcoverApiKey: 'key',
|
||||
hardcoverSyncEnabled: false
|
||||
};
|
||||
service.updateSettings(settings).subscribe();
|
||||
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v1\/hardcover-sync-settings$/), settings);
|
||||
});
|
||||
});
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import {API_CONFIG} from '../../../../../core/config/api-config';
|
||||
|
||||
export interface HardcoverSyncSettings {
|
||||
hardcoverApiKey?: string;
|
||||
hardcoverSyncEnabled?: boolean;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class HardcoverSyncSettingsService {
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/hardcover-sync-settings`;
|
||||
|
||||
getSettings(): Observable<HardcoverSyncSettings> {
|
||||
return this.http.get<HardcoverSyncSettings>(this.baseUrl);
|
||||
}
|
||||
|
||||
updateSettings(settings: HardcoverSyncSettings): Observable<HardcoverSyncSettings> {
|
||||
return this.http.put<HardcoverSyncSettings>(this.baseUrl, settings);
|
||||
}
|
||||
}
|
||||
-76
@@ -174,82 +174,6 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (koboSyncSettings.syncEnabled) {
|
||||
<div class="preferences-section">
|
||||
<div class="section-header">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-book"></i>
|
||||
Hardcover Integration
|
||||
</h3>
|
||||
<p class="section-description">
|
||||
Sync your Kobo reading progress to your personal Hardcover account.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="settings-card">
|
||||
<div class="setting-item">
|
||||
<div class="setting-info">
|
||||
<div class="setting-label-row">
|
||||
<label class="setting-label">Sync Reading Progress to Hardcover</label>
|
||||
<p-toggle-switch
|
||||
id="hardcoverSyncEnabled"
|
||||
name="hardcoverSyncEnabled"
|
||||
[(ngModel)]="koboSyncSettings.hardcoverSyncEnabled"
|
||||
(ngModelChange)="onHardcoverSyncToggle()">
|
||||
</p-toggle-switch>
|
||||
</div>
|
||||
<p class="setting-description">
|
||||
When enabled, your reading progress from Kobo will be automatically synced to your Hardcover account.
|
||||
Each user can configure their own Hardcover account.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (koboSyncSettings.hardcoverSyncEnabled) {
|
||||
<div class="setting-item">
|
||||
<div class="setting-info">
|
||||
<div class="setting-label-row">
|
||||
<label class="setting-label">Hardcover API Key</label>
|
||||
<div class="token-input-group">
|
||||
<input
|
||||
pInputText
|
||||
id="hardcoverApiKey"
|
||||
[(ngModel)]="koboSyncSettings.hardcoverApiKey"
|
||||
[type]="showHardcoverApiKey ? 'text' : 'password'"
|
||||
name="hardcoverApiKey"
|
||||
class="token-input"
|
||||
placeholder="Enter your Hardcover API key"
|
||||
(blur)="onHardcoverApiKeyChange()"
|
||||
/>
|
||||
<p-button
|
||||
icon="pi pi-copy"
|
||||
outlined="true"
|
||||
severity="info"
|
||||
size="small"
|
||||
(onClick)="copyText(koboSyncSettings.hardcoverApiKey ?? '', 'API Key')"
|
||||
[disabled]="!koboSyncSettings.hardcoverApiKey">
|
||||
</p-button>
|
||||
<p-button
|
||||
[icon]="showHardcoverApiKey ? 'pi pi-eye-slash' : 'pi pi-eye'"
|
||||
outlined="true"
|
||||
severity="info"
|
||||
size="small"
|
||||
(onClick)="toggleShowHardcoverApiKey()">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
<p class="setting-description">
|
||||
Your personal Hardcover API key. Get it from
|
||||
<a href="https://hardcover.app/account/api" target="_blank" rel="noopener">hardcover.app/account/api</a>.
|
||||
This key is used only for syncing your reading progress and is not shared with other users.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (isAdmin) {
|
||||
<div class="preferences-section">
|
||||
<div class="section-header">
|
||||
|
||||
+1
-20
@@ -43,7 +43,6 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
|
||||
isAdmin = false;
|
||||
credentialsSaved = false;
|
||||
showToken = false;
|
||||
showHardcoverApiKey = false;
|
||||
|
||||
koboSettings: KoboSettings = {
|
||||
convertToKepub: false,
|
||||
@@ -59,9 +58,7 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
|
||||
syncEnabled: false,
|
||||
progressMarkAsReadingThreshold: 1,
|
||||
progressMarkAsFinishedThreshold: 99,
|
||||
autoAddToShelf: true,
|
||||
hardcoverApiKey: '',
|
||||
hardcoverSyncEnabled: false
|
||||
autoAddToShelf: true
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
@@ -122,8 +119,6 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
|
||||
this.koboSyncSettings.progressMarkAsReadingThreshold = settings.progressMarkAsReadingThreshold ?? 1;
|
||||
this.koboSyncSettings.progressMarkAsFinishedThreshold = settings.progressMarkAsFinishedThreshold ?? 99;
|
||||
this.koboSyncSettings.autoAddToShelf = settings.autoAddToShelf ?? false;
|
||||
this.koboSyncSettings.hardcoverApiKey = settings.hardcoverApiKey ?? '';
|
||||
this.koboSyncSettings.hardcoverSyncEnabled = settings.hardcoverSyncEnabled ?? false;
|
||||
this.credentialsSaved = !!settings.token;
|
||||
},
|
||||
error: () => {
|
||||
@@ -172,20 +167,6 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
|
||||
this.showToken = !this.showToken;
|
||||
}
|
||||
|
||||
toggleShowHardcoverApiKey() {
|
||||
this.showHardcoverApiKey = !this.showHardcoverApiKey;
|
||||
}
|
||||
|
||||
onHardcoverSyncToggle() {
|
||||
const message = this.koboSyncSettings.hardcoverSyncEnabled
|
||||
? 'Hardcover sync enabled'
|
||||
: 'Hardcover sync disabled';
|
||||
this.updateKoboSettings(message);
|
||||
}
|
||||
|
||||
onHardcoverApiKeyChange() {
|
||||
this.updateKoboSettings('Hardcover API key updated');
|
||||
}
|
||||
|
||||
confirmRegenerateToken() {
|
||||
this.confirmationService.confirm({
|
||||
|
||||
+1
@@ -176,6 +176,7 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
} @else {
|
||||
<div class="settings-content">
|
||||
|
||||
+3
@@ -78,6 +78,7 @@ export class KoreaderSettingsComponent implements OnInit, OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
get canSave(): boolean {
|
||||
const u = this.koReaderUsername?.trim() ?? '';
|
||||
const p = this.koReaderPassword ?? '';
|
||||
@@ -107,6 +108,7 @@ export class KoreaderSettingsComponent implements OnInit, OnDestroy {
|
||||
this.showPassword = !this.showPassword;
|
||||
}
|
||||
|
||||
|
||||
saveCredentials() {
|
||||
this.koreaderService.createUser(this.koReaderUsername, this.koReaderPassword)
|
||||
.subscribe({
|
||||
@@ -119,6 +121,7 @@ export class KoreaderSettingsComponent implements OnInit, OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
copyText(text: string, label: string = 'Text') {
|
||||
if (!text) {
|
||||
return;
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
<app-koreader-settings-component></app-koreader-settings-component>
|
||||
</div>
|
||||
<p-divider></p-divider>
|
||||
<div class="py-4">
|
||||
<app-hardcover-settings-component></app-hardcover-settings-component>
|
||||
</div>
|
||||
<p-divider></p-divider>
|
||||
<div class="py-4">
|
||||
<app-kobo-sync-setting-component></app-kobo-sync-setting-component>
|
||||
</div>
|
||||
|
||||
@@ -2,12 +2,14 @@ import {Component} from '@angular/core';
|
||||
import {Divider} from 'primeng/divider';
|
||||
import {KoreaderSettingsComponent} from './component/koreader-settings/koreader-settings-component';
|
||||
import {KoboSyncSettingsComponent} from './component/kobo-sync-settings/kobo-sync-settings-component';
|
||||
import {HardcoverSettingsComponent} from './component/hardcover-settings/hardcover-settings-component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-device-settings-component',
|
||||
imports: [
|
||||
KoreaderSettingsComponent,
|
||||
KoboSyncSettingsComponent,
|
||||
HardcoverSettingsComponent,
|
||||
Divider
|
||||
],
|
||||
templateUrl: './device-settings-component.html',
|
||||
|
||||
Reference in New Issue
Block a user