Allow users to disable kobo shelf

This commit is contained in:
aditya.chandel
2025-08-28 21:12:45 -06:00
committed by Aditya Chandel
parent 8d04bc3498
commit 12b7aaa8e9
9 changed files with 154 additions and 77 deletions
@@ -1,7 +1,7 @@
<p-confirmDialog></p-confirmDialog>
<p class="text-lg flex items-center gap-2 px-4 pt-4 pb-2">
Kobo Integration Token:
Kobo Integration Settings:
<i class="pi pi-info-circle text-sky-600"
pTooltip="Click to view Kobo setup documentation"
tooltipPosition="right"
@@ -12,42 +12,53 @@
@if (hasKoboTokenPermission) {
<form #koboForm="ngForm" class="px-4 py-2">
<div class="flex flex-col px-4 py-4 space-y-4">
<div class="flex flex-col gap-1 max-w-[33rem]">
<label for="koboToken">Kobo Sync Token</label>
<div class="flex items-center gap-2">
<input
pInputText
id="koboToken"
[(ngModel)]="koboToken"
[type]="showToken ? 'text' : 'password'"
readonly
fluid
name="koboToken"
/>
<p-button
icon="pi pi-copy"
outlined="true"
severity="info"
(onClick)="copyText(koboToken)">
</p-button>
<p-button
[icon]="showToken ? 'pi pi-eye-slash' : 'pi pi-eye'"
outlined="true"
severity="info"
(onClick)="toggleShowToken()">
</p-button>
</div>
<div class="flex flex-col p-4 space-y-6">
<div class="flex gap-2">
<p-toggle-switch
id="syncEnabled"
name="syncEnabled"
[(ngModel)]="koboSyncSettings.syncEnabled"
(ngModelChange)="onSyncToggle()">
</p-toggle-switch>
<label for="syncEnabled">Enable Kobo Sync</label>
</div>
<p-button
icon="pi pi-refresh"
outlined="true"
severity="warn"
label="Regenerate Token"
(onClick)="confirmRegenerateToken()"
[disabled]="!credentialsSaved">
</p-button>
@if (koboSyncSettings.syncEnabled) {
<div class="flex flex-col gap-1 max-w-[33rem]">
<label for="koboToken">Kobo Sync Token</label>
<div class="flex items-center gap-2">
<input
pInputText
id="koboToken"
[(ngModel)]="koboSyncSettings.token"
[type]="showToken ? 'text' : 'password'"
readonly
fluid
name="koboToken"
/>
<p-button
icon="pi pi-copy"
outlined="true"
severity="info"
(onClick)="copyText(koboSyncSettings.token)">
</p-button>
<p-button
[icon]="showToken ? 'pi pi-eye-slash' : 'pi pi-eye'"
outlined="true"
severity="info"
(onClick)="toggleShowToken()">
</p-button>
</div>
</div>
<p-button
icon="pi pi-refresh"
outlined="true"
severity="warn"
label="Regenerate Token"
(onClick)="confirmRegenerateToken()"
[disabled]="!credentialsSaved">
</p-button>
}
</div>
</form>
@@ -15,6 +15,7 @@ import {Slider} from 'primeng/slider';
import {AppSettingsService} from '../../../core/service/app-settings.service';
import {SettingsHelperService} from '../../../core/service/settings-helper.service';
import {AppSettingKey, KoboSettings} from '../../../core/model/app-settings.model';
import {ShelfService} from '../../../book/service/shelf.service';
@Component({
selector: 'app-kobo-sync-setting-component',
@@ -32,13 +33,13 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
protected userService = inject(UserService);
protected appSettingsService = inject(AppSettingsService);
protected settingsHelperService = inject(SettingsHelperService);
private shelfService = inject(ShelfService);
private readonly destroy$ = new Subject<void>();
private readonly sliderChange$ = new Subject<void>();
hasKoboTokenPermission = false;
isAdmin = false;
koboToken = '';
credentialsSaved = false;
showToken = false;
@@ -47,6 +48,11 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
conversionLimitInMb: 100
};
koboSyncSettings: KoboSyncSettings = {
token: '',
syncEnabled: false
}
ngOnInit() {
this.setupSliderDebouncing();
this.setupUserStateSubscription();
@@ -69,18 +75,19 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
this.hasKoboTokenPermission = (userState.user?.permissions.canSyncKobo) ?? false;
this.isAdmin = userState.user?.permissions.admin ?? false;
if (this.hasKoboTokenPermission) {
this.loadKoboToken();
this.loadKoboUserSettings();
}
if (this.isAdmin) {
this.loadKoboSettings();
this.loadKoboAdminSettings();
}
});
}
private loadKoboToken() {
private loadKoboUserSettings() {
this.koboService.getUser().subscribe({
next: (settings: KoboSyncSettings) => {
this.koboToken = settings.token;
this.koboSyncSettings.token = settings.token;
this.koboSyncSettings.syncEnabled = settings.syncEnabled;
this.credentialsSaved = !!settings.token;
},
error: () => {
@@ -89,7 +96,7 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
});
}
private loadKoboSettings() {
private loadKoboAdminSettings() {
this.appSettingsService.appSettings$
.pipe(
filter(settings => settings != null),
@@ -122,7 +129,7 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
private regenerateToken() {
this.koboService.createOrUpdateToken().subscribe({
next: (settings) => {
this.koboToken = settings.token;
this.koboSyncSettings.token = settings.token;
this.credentialsSaved = true;
this.messageService.add({severity: 'success', summary: 'Token regenerated', detail: 'New token generated successfully'});
},
@@ -140,6 +147,44 @@ export class KoboSyncSettingsComponent implements OnInit, OnDestroy {
this.sliderChange$.next();
}
onSyncToggle() {
if (!this.koboSyncSettings.syncEnabled) {
this.confirmationService.confirm({
message: 'Disabling Kobo sync will delete your Kobo shelf. Are you sure you want to proceed?',
header: 'Confirm Disable',
icon: 'pi pi-exclamation-triangle',
accept: () => this.performToggle(false),
reject: () => {
this.koboSyncSettings.syncEnabled = true;
}
});
} else {
this.performToggle(true);
}
}
private performToggle(enabled: boolean) {
this.koboService.toggleSync(enabled).subscribe({
next: () => {
this.messageService.add({
severity: 'success',
summary: 'Sync Updated',
detail: enabled
? 'Kobo sync enabled'
: 'Kobo sync disabled'
});
this.shelfService.reloadShelves();
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'Failed to update sync setting'
});
}
});
}
saveSettings() {
this.settingsHelperService.saveSetting(AppSettingKey.KOBO_SETTINGS, this.koboSettings)
.subscribe({
@@ -5,6 +5,7 @@ import {API_CONFIG} from '../../config/api-config';
export interface KoboSyncSettings {
token: string;
syncEnabled: boolean;
}
@Injectable({
@@ -14,11 +15,16 @@ export class KoboService {
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/kobo-settings`;
private readonly http = inject(HttpClient);
getUser(): Observable<KoboSyncSettings> {
return this.http.get<KoboSyncSettings>(`${this.baseUrl}`);
}
createOrUpdateToken(): Observable<KoboSyncSettings> {
return this.http.put<KoboSyncSettings>(`${this.baseUrl}`, null);
}
getUser(): Observable<KoboSyncSettings> {
return this.http.get<KoboSyncSettings>(`${this.baseUrl}`);
toggleSync(enabled: boolean): Observable<void> {
const params = new HttpParams().set('enabled', enabled.toString());
return this.http.put<void>(`${this.baseUrl}/sync`, null, { params });
}
}