refactor(metadata): consolidate metadata providers on the frontend (#2719)
This commit is contained in:
+1
-1
@@ -36,7 +36,7 @@
|
||||
|
||||
<form [formGroup]="form" class="weights-form">
|
||||
<div class="weights-grid">
|
||||
@for (key of orderedKeys; track key) {
|
||||
@for (key of orderedKeys(); track key) {
|
||||
<div class="weight-field">
|
||||
<label class="field-label" [for]="'metadata-weight-' + key">{{ getFieldLabel(key) }}</label>
|
||||
<p-inputNumber
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ describe('MetadataMatchWeightsComponent', () => {
|
||||
|
||||
expect(component.form.get('title')?.value).toBe(10);
|
||||
expect(component.form.get('authors')?.value).toBe(9);
|
||||
expect(component.orderedKeys[0]).toBe('title');
|
||||
expect(component.orderedKeys()[0]).toBe('title');
|
||||
});
|
||||
|
||||
it('hydrates the reactive form when weights resolve after initial render', () => {
|
||||
|
||||
+26
-47
@@ -1,4 +1,4 @@
|
||||
import {Component, DestroyRef, effect, inject} from '@angular/core';
|
||||
import {Component, computed, DestroyRef, effect, inject} from '@angular/core';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
|
||||
import {MessageService} from '@openng/optimus-ui/api';
|
||||
@@ -7,8 +7,17 @@ import {Button} from '@openng/optimus-ui/button';
|
||||
import {AppSettingKey, MetadataMatchWeights} from '../../../../shared/model/app-settings.model';
|
||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||
import {InputNumber} from '@openng/optimus-ui/inputnumber';
|
||||
import {MetadataProviderFieldsService} from '../../../../shared/metadata';
|
||||
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
|
||||
const GENERIC_WEIGHT_KEYS = [
|
||||
'title', 'subtitle', 'authors', 'description', 'publisher', 'publishedDate',
|
||||
'categories', 'coverImage', 'seriesName', 'seriesNumber', 'seriesTotal',
|
||||
'language', 'isbn13', 'isbn10', 'pageCount',
|
||||
];
|
||||
|
||||
const DOUBAN_WEIGHT_KEYS = ['doubanRating', 'doubanReviewCount'];
|
||||
|
||||
@Component({
|
||||
selector: 'app-metadata-match-weights-component',
|
||||
imports: [
|
||||
@@ -22,26 +31,24 @@ import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
})
|
||||
export class MetadataMatchWeightsComponent {
|
||||
|
||||
readonly orderedFieldKeys: string[] = [
|
||||
'title', 'subtitle', 'authors', 'description', 'publisher', 'publishedDate',
|
||||
'categories', 'coverImage', 'seriesName', 'seriesNumber', 'seriesTotal',
|
||||
'language', 'isbn13', 'isbn10', 'pageCount',
|
||||
'amazonRating', 'amazonReviewCount', 'goodreadsRating', 'goodreadsReviewCount',
|
||||
'hardcoverRating', 'hardcoverReviewCount', 'audibleRating', 'audibleReviewCount',
|
||||
'doubanRating', 'doubanReviewCount', 'ranobedbRating'
|
||||
];
|
||||
|
||||
private fb = inject(FormBuilder);
|
||||
form: FormGroup = this.buildForm();
|
||||
isSaving = false;
|
||||
isRecalculating = false;
|
||||
|
||||
private weightsService = inject(MetadataMatchWeightsService);
|
||||
private appSettingsService = inject(AppSettingsService);
|
||||
private messageService = inject(MessageService);
|
||||
private t = inject(TranslocoService);
|
||||
private providerFields = inject(MetadataProviderFieldsService);
|
||||
private destroyRef = inject(DestroyRef);
|
||||
|
||||
readonly orderedKeys = computed(() => [
|
||||
...GENERIC_WEIGHT_KEYS,
|
||||
...this.providerFields.scoreFields().map(field => field.name),
|
||||
...DOUBAN_WEIGHT_KEYS,
|
||||
]);
|
||||
|
||||
form: FormGroup = this.buildForm();
|
||||
isSaving = false;
|
||||
isRecalculating = false;
|
||||
|
||||
private readonly syncSettingsEffect = effect(() => {
|
||||
const settings = this.appSettingsService.appSettings();
|
||||
if (settings?.metadataMatchWeights) {
|
||||
@@ -50,42 +57,14 @@ export class MetadataMatchWeightsComponent {
|
||||
});
|
||||
|
||||
private buildForm(): FormGroup {
|
||||
return this.fb.group({
|
||||
title: [0, [Validators.required, Validators.min(0)]],
|
||||
subtitle: [0, [Validators.required, Validators.min(0)]],
|
||||
description: [0, [Validators.required, Validators.min(0)]],
|
||||
publisher: [0, [Validators.required, Validators.min(0)]],
|
||||
publishedDate: [0, [Validators.required, Validators.min(0)]],
|
||||
authors: [0, [Validators.required, Validators.min(0)]],
|
||||
categories: [0, [Validators.required, Validators.min(0)]],
|
||||
seriesName: [0, [Validators.required, Validators.min(0)]],
|
||||
seriesNumber: [0, [Validators.required, Validators.min(0)]],
|
||||
seriesTotal: [0, [Validators.required, Validators.min(0)]],
|
||||
isbn13: [0, [Validators.required, Validators.min(0)]],
|
||||
isbn10: [0, [Validators.required, Validators.min(0)]],
|
||||
pageCount: [0, [Validators.required, Validators.min(0)]],
|
||||
language: [0, [Validators.required, Validators.min(0)]],
|
||||
amazonRating: [0, [Validators.required, Validators.min(0)]],
|
||||
amazonReviewCount: [0, [Validators.required, Validators.min(0)]],
|
||||
goodreadsRating: [0, [Validators.required, Validators.min(0)]],
|
||||
goodreadsReviewCount: [0, [Validators.required, Validators.min(0)]],
|
||||
hardcoverRating: [0, [Validators.required, Validators.min(0)]],
|
||||
hardcoverReviewCount: [0, [Validators.required, Validators.min(0)]],
|
||||
doubanRating: [0, [Validators.required, Validators.min(0)]],
|
||||
doubanReviewCount: [0, [Validators.required, Validators.min(0)]],
|
||||
ranobedbRating: [0, [Validators.required, Validators.min(0)]],
|
||||
audibleRating: [0, [Validators.required, Validators.min(0)]],
|
||||
audibleReviewCount: [0, [Validators.required, Validators.min(0)]],
|
||||
coverImage: [0, [Validators.required, Validators.min(0)]],
|
||||
});
|
||||
}
|
||||
|
||||
get orderedKeys(): string[] {
|
||||
return this.orderedFieldKeys;
|
||||
return this.fb.group(Object.fromEntries(
|
||||
this.orderedKeys().map(key => [key, [0, [Validators.required, Validators.min(0)]]]),
|
||||
));
|
||||
}
|
||||
|
||||
getFieldLabel(key: string): string {
|
||||
return this.t.translate('settingsMeta.matchWeights.fields.' + key);
|
||||
const providerField = this.providerFields.fields().find(field => field.name === key);
|
||||
return providerField ? this.providerFields.label(providerField.name) : this.t.translate('settingsMeta.matchWeights.fields.' + key);
|
||||
}
|
||||
|
||||
save(): void {
|
||||
|
||||
+143
-194
@@ -16,177 +16,145 @@
|
||||
<h4 class="provider-group-title">{{ t('configurableTitle') }}</h4>
|
||||
|
||||
<div class="configurable-providers-list">
|
||||
<div class="setting-item">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="amazonEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">Amazon</span>
|
||||
</div>
|
||||
@if (amazonEnabled) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('region') }}</span>
|
||||
<p-select
|
||||
[options]="amazonDomains"
|
||||
[(ngModel)]="selectedAmazonDomain"
|
||||
[placeholder]="t('selectDomain')"
|
||||
class="config-select">
|
||||
</p-select>
|
||||
</div>
|
||||
<div class="config-field">
|
||||
<div class="config-label-row">
|
||||
<span class="config-label">{{ t('cookieLabel') }}</span>
|
||||
<app-external-doc-link docType="amazonCookie"></app-external-doc-link>
|
||||
</div>
|
||||
<p class="config-description">{{ t('cookieDesc') }}</p>
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[placeholder]="t('cookiePlaceholder')"
|
||||
[(ngModel)]="amazonCookie"
|
||||
class="config-input"/>
|
||||
</div>
|
||||
@for (provider of configurableProviders(); track provider.id) {
|
||||
<div class="setting-item">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="enabled[provider.id]" [disabled]="isToggleDisabled(provider.id)"></p-toggleswitch>
|
||||
<span class="setting-label">{{ provider.labelKey | transloco }}</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="setting-item">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch
|
||||
[(ngModel)]="googleEnabled"
|
||||
[disabled]="!googleApiKeyConfigured">
|
||||
</p-toggleswitch>
|
||||
<span class="setting-label">Google Books</span>
|
||||
</div>
|
||||
<div class="setting-options">
|
||||
@if (googleEnabled) {
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('languageRestriction') }}</span>
|
||||
<p-select
|
||||
[options]="googleLanguages"
|
||||
[(ngModel)]="selectedGoogleLanguage"
|
||||
[showClear]="!!selectedGoogleLanguage"
|
||||
[placeholder]="t('selectLanguage')"
|
||||
class="config-select">
|
||||
</p-select>
|
||||
</div>
|
||||
@switch (provider.id) {
|
||||
@case ('Amazon') {
|
||||
@if (enabled.Amazon) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('region') }}</span>
|
||||
<p-select
|
||||
[options]="amazonDomains"
|
||||
[(ngModel)]="selectedAmazonDomain"
|
||||
[placeholder]="t('selectDomain')"
|
||||
class="config-select">
|
||||
</p-select>
|
||||
</div>
|
||||
<div class="config-field">
|
||||
<div class="config-label-row">
|
||||
<span class="config-label">{{ t('cookieLabel') }}</span>
|
||||
<app-external-doc-link docType="amazonCookie"></app-external-doc-link>
|
||||
</div>
|
||||
<p class="config-description">{{ t('cookieDesc') }}</p>
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[placeholder]="t('cookiePlaceholder')"
|
||||
[(ngModel)]="amazonCookie"
|
||||
class="config-input"/>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@case ('Google') {
|
||||
<div class="setting-options">
|
||||
@if (enabled.Google) {
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('languageRestriction') }}</span>
|
||||
<p-select
|
||||
[options]="googleLanguages"
|
||||
[(ngModel)]="selectedGoogleLanguage"
|
||||
[showClear]="!!selectedGoogleLanguage"
|
||||
[placeholder]="t('selectLanguage')"
|
||||
class="config-select">
|
||||
</p-select>
|
||||
</div>
|
||||
}
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('googleApiLabel') }}</span>
|
||||
<p class="config-description">{{ t('googleApiDesc') }}</p>
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[placeholder]="t('googleApiPlaceholder')"
|
||||
[(ngModel)]="googleApiKey"
|
||||
(ngModelChange)="onKeyChange('Google', $event)"
|
||||
class="config-input"/>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@case ('Hardcover') {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<div class="config-label-row">
|
||||
<span class="config-label">{{ t('apiToken') }}</span>
|
||||
<app-external-doc-link docType="hardcover"></app-external-doc-link>
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[placeholder]="t('hardcoverPlaceholder')"
|
||||
[(ngModel)]="hardcoverToken"
|
||||
(ngModelChange)="onKeyChange('Hardcover', $event)"
|
||||
class="config-input"/>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@case ('Comicvine') {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('apiToken') }}</span>
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[placeholder]="t('comicvinePlaceholder')"
|
||||
[(ngModel)]="comicvineToken"
|
||||
(ngModelChange)="onKeyChange('Comicvine', $event)"
|
||||
class="config-input"/>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@case ('Ranobedb') {
|
||||
@if (enabled.Ranobedb) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<div class="config-label-row">
|
||||
<p-toggleswitch [(ngModel)]="ranobedbPreferRomaji"></p-toggleswitch>
|
||||
<span class="config-label">{{ t('ranobedbPreferRomajiLabel') }}</span>
|
||||
</div>
|
||||
<p class="config-description">{{ t('ranobedbPreferRomajiDesc') }}</p>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@case ('Audible') {
|
||||
@if (enabled.Audible) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('region') }}</span>
|
||||
<p-select
|
||||
[options]="audibleDomains"
|
||||
[(ngModel)]="selectedAudibleDomain"
|
||||
[placeholder]="t('selectDomain')"
|
||||
class="config-select">
|
||||
</p-select>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@case ('AppleBooks') {
|
||||
@if (enabled.AppleBooks) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('region') }}</span>
|
||||
<p-select
|
||||
[options]="appleBooksCountries"
|
||||
[(ngModel)]="selectedAppleBooksCountry"
|
||||
[placeholder]="t('selectCountry')"
|
||||
class="config-select">
|
||||
</p-select>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('googleApiLabel') }}</span>
|
||||
<p class="config-description">{{ t('googleApiDesc') }}</p>
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[placeholder]="t('googleApiPlaceholder')"
|
||||
[(ngModel)]="googleApiKey"
|
||||
class="config-input"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="setting-item">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch
|
||||
[(ngModel)]="hardcoverEnabled"
|
||||
[disabled]="!hardcoverToken">
|
||||
</p-toggleswitch>
|
||||
<span class="setting-label">Hardcover</span>
|
||||
</div>
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<div class="config-label-row">
|
||||
<span class="config-label">{{ t('apiToken') }}</span>
|
||||
<app-external-doc-link docType="hardcover"></app-external-doc-link>
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[placeholder]="t('hardcoverPlaceholder')"
|
||||
[(ngModel)]="hardcoverToken"
|
||||
(ngModelChange)="onTokenChange($event)"
|
||||
class="config-input"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="setting-item">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="comicvineEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">Comic Vine</span>
|
||||
</div>
|
||||
@if (comicvineEnabled) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('apiToken') }}</span>
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[placeholder]="t('comicvinePlaceholder')"
|
||||
[(ngModel)]="comicvineToken"
|
||||
(ngModelChange)="onComicTokenChange($event)"
|
||||
class="config-input"/>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="setting-item">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="audibleEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">Audible</span>
|
||||
</div>
|
||||
@if (audibleEnabled) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('region') }}</span>
|
||||
<p-select
|
||||
[options]="audibleDomains"
|
||||
[(ngModel)]="selectedAudibleDomain"
|
||||
[placeholder]="t('selectDomain')"
|
||||
class="config-select">
|
||||
</p-select>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="setting-item">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="ranobedbEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">RanobeDB</span>
|
||||
</div>
|
||||
@if (ranobedbEnabled) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<div class="config-label-row">
|
||||
<p-toggleswitch [(ngModel)]="ranobedbPreferRomaji"></p-toggleswitch>
|
||||
<span class="config-label">{{ t('ranobedbPreferRomajiLabel') }}</span>
|
||||
</div>
|
||||
<p class="config-description">{{ t('ranobedbPreferRomajiDesc') }}</p>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="setting-item">
|
||||
<div class="setting-header">
|
||||
|
||||
<p-toggleswitch [(ngModel)]="appleBooksEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">Apple Books</span>
|
||||
</div>
|
||||
@if (appleBooksEnabled) {
|
||||
<div class="setting-options">
|
||||
<div class="config-field">
|
||||
<span class="config-label">{{ t('region') }}</span>
|
||||
<p-select
|
||||
[options]="appleBooksCountries"
|
||||
[(ngModel)]="selectedAppleBooksCountry"
|
||||
[placeholder]="t('selectCountry')"
|
||||
class="config-select">
|
||||
</p-select>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -194,33 +162,14 @@
|
||||
<h4 class="provider-group-title">{{ t('readyToUseTitle') }}</h4>
|
||||
|
||||
<div class="simple-providers-grid">
|
||||
<div class="setting-item simple-provider">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="openLibraryEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">OpenLibrary</span>
|
||||
@for (provider of simpleProviders(); track provider.id) {
|
||||
<div class="setting-item simple-provider">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="enabled[provider.id]"></p-toggleswitch>
|
||||
<span class="setting-label">{{ provider.labelKey | transloco }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="setting-item simple-provider">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="goodreadsEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">Goodreads</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="setting-item simple-provider">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="doubanEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">Douban</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="setting-item simple-provider">
|
||||
<div class="setting-header">
|
||||
<p-toggleswitch [(ngModel)]="lubimyCzytacEnabled"></p-toggleswitch>
|
||||
<span class="setting-label">Lubimyczytac</span>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
+16
-31
@@ -8,6 +8,9 @@ import {MessageService} from '@openng/optimus-ui/api';
|
||||
import {getTranslocoModule} from '../../../../core/testing/transloco-testing';
|
||||
import {type AppSettings, AppSettingKey} from '../../../../shared/model/app-settings.model';
|
||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||
import {MetadataCatalogService} from '../../../../shared/metadata/metadata-catalog.service';
|
||||
import {METADATA_PROVIDER_LIST} from '../../../../shared/metadata/metadata-providers';
|
||||
import {MetadataSourceQueryService} from '../../../metadata/sources/metadata-source-query.service';
|
||||
import {MetadataProviderSettingsComponent} from './metadata-provider-settings.component';
|
||||
|
||||
describe('MetadataProviderSettingsComponent', () => {
|
||||
@@ -15,14 +18,21 @@ describe('MetadataProviderSettingsComponent', () => {
|
||||
let component: MetadataProviderSettingsComponent;
|
||||
let appSettingsSignal: WritableSignal<AppSettings | null>;
|
||||
let saveSettings: ReturnType<typeof vi.fn>;
|
||||
let refreshProviders: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(async () => {
|
||||
appSettingsSignal = signal<AppSettings | null>(null);
|
||||
saveSettings = vi.fn(() => of(void 0));
|
||||
refreshProviders = vi.fn(() => Promise.resolve());
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [MetadataProviderSettingsComponent, getTranslocoModule()],
|
||||
providers: [
|
||||
{provide: MetadataCatalogService, useValue: {providers: () => METADATA_PROVIDER_LIST}},
|
||||
{
|
||||
provide: MetadataSourceQueryService,
|
||||
useValue: {refreshProviders},
|
||||
},
|
||||
{
|
||||
provide: AppSettingsService,
|
||||
useValue: {
|
||||
@@ -43,24 +53,8 @@ describe('MetadataProviderSettingsComponent', () => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('does not hydrate Google Books as enabled without an API key', () => {
|
||||
appSettingsSignal.set(buildSettings({enabled: true, apiKey: ''}));
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.googleEnabled).toBe(false);
|
||||
expect(component.googleApiKeyConfigured).toBe(false);
|
||||
});
|
||||
|
||||
it('hydrates Google Books as enabled with an API key', () => {
|
||||
appSettingsSignal.set(buildSettings({enabled: true, apiKey: 'valid-key'}));
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.googleEnabled).toBe(true);
|
||||
expect(component.googleApiKeyConfigured).toBe(true);
|
||||
});
|
||||
|
||||
it('persists Google Books as enabled when an API key is configured', () => {
|
||||
component.googleEnabled = true;
|
||||
component.enabled.Google = true;
|
||||
component.googleApiKey = ' configured-key ';
|
||||
|
||||
component.saveSettings();
|
||||
@@ -73,10 +67,12 @@ describe('MetadataProviderSettingsComponent', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('does not persist Google Books as enabled without an API key', () => {
|
||||
component.googleEnabled = true;
|
||||
component.googleApiKey = '';
|
||||
it('switches a provider off when its required key is cleared', () => {
|
||||
component.googleApiKey = 'configured-key';
|
||||
component.enabled.Google = true;
|
||||
|
||||
component.googleApiKey = '';
|
||||
component.onKeyChange('Google', '');
|
||||
component.saveSettings();
|
||||
|
||||
expect(getSavedProviderSettings().google.enabled).toBe(false);
|
||||
@@ -92,14 +88,3 @@ describe('MetadataProviderSettingsComponent', () => {
|
||||
return payload[0].newValue;
|
||||
}
|
||||
});
|
||||
|
||||
function buildSettings(google: {enabled: boolean; apiKey: string}): AppSettings {
|
||||
return {
|
||||
metadataProviderSettings: {
|
||||
google: {
|
||||
...google,
|
||||
language: '',
|
||||
},
|
||||
},
|
||||
} as AppSettings;
|
||||
}
|
||||
|
||||
+47
-76
@@ -1,4 +1,4 @@
|
||||
import {Component, DestroyRef, effect, inject} from '@angular/core';
|
||||
import {Component, computed, DestroyRef, effect, inject} from '@angular/core';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
|
||||
import {TableModule} from '@openng/optimus-ui/table';
|
||||
@@ -6,11 +6,14 @@ import {InputText} from '@openng/optimus-ui/inputtext';
|
||||
import {Button} from '@openng/optimus-ui/button';
|
||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||
import {MessageService} from '@openng/optimus-ui/api';
|
||||
import {AppSettingKey} from '../../../../shared/model/app-settings.model';
|
||||
import {AppSettingKey, type MetadataProviderSettings} from '../../../../shared/model/app-settings.model';
|
||||
import {MetadataCatalogService} from '../../../../shared/metadata/metadata-catalog.service';
|
||||
import type {MetadataProviderId} from '../../../../shared/metadata/metadata-providers';
|
||||
import {MetadataSourceQueryService} from '../../../metadata/sources/metadata-source-query.service';
|
||||
import {Select} from '@openng/optimus-ui/select';
|
||||
import {ExternalDocLinkComponent} from '../../../../shared/components/external-doc-link/external-doc-link.component';
|
||||
import { ToggleSwitch } from '@openng/optimus-ui/toggleswitch';
|
||||
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/transloco';
|
||||
|
||||
@Component({
|
||||
selector: 'app-metadata-provider-settings',
|
||||
@@ -23,12 +26,18 @@ import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
Select,
|
||||
ExternalDocLinkComponent,
|
||||
ToggleSwitch,
|
||||
TranslocoDirective
|
||||
TranslocoDirective,
|
||||
TranslocoPipe
|
||||
],
|
||||
templateUrl: './metadata-provider-settings.component.html',
|
||||
styleUrl: './metadata-provider-settings.component.scss'
|
||||
})
|
||||
export class MetadataProviderSettingsComponent {
|
||||
private readonly catalog = inject(MetadataCatalogService);
|
||||
private readonly sourceQuery = inject(MetadataSourceQueryService);
|
||||
readonly configurableProviders = computed(() => this.catalog.providers().filter(provider => provider.hasOptions));
|
||||
readonly simpleProviders = computed(() => this.catalog.providers().filter(provider => !provider.hasOptions));
|
||||
readonly enabled: Partial<Record<MetadataProviderId, boolean>> = {};
|
||||
|
||||
amazonDomains = [
|
||||
{label: 'amazon.com', value: 'com'},
|
||||
@@ -97,23 +106,12 @@ export class MetadataProviderSettingsComponent {
|
||||
|
||||
selectedAppleBooksCountry = 'US';
|
||||
selectedAudibleDomain = 'com';
|
||||
audibleEnabled: boolean = false;
|
||||
|
||||
hardcoverToken: string = '';
|
||||
amazonCookie: string = '';
|
||||
hardcoverEnabled: boolean = false;
|
||||
amazonEnabled: boolean = false;
|
||||
openLibraryEnabled: boolean = false;
|
||||
goodreadsEnabled: boolean = false;
|
||||
googleEnabled: boolean = false;
|
||||
comicvineEnabled: boolean = false;
|
||||
comicvineToken: string = '';
|
||||
doubanEnabled: boolean = false;
|
||||
lubimyCzytacEnabled: boolean = false;
|
||||
ranobedbEnabled: boolean = false;
|
||||
ranobedbPreferRomaji: boolean = false;
|
||||
googleApiKey: string = '';
|
||||
appleBooksEnabled: boolean = false;
|
||||
|
||||
private appSettingsService = inject(AppSettingsService);
|
||||
private messageService = inject(MessageService);
|
||||
@@ -129,97 +127,70 @@ export class MetadataProviderSettingsComponent {
|
||||
|
||||
private applySettings(settings: NonNullable<ReturnType<typeof this.appSettingsService.appSettings>>): void {
|
||||
const metadataProviderSettings = settings.metadataProviderSettings;
|
||||
this.openLibraryEnabled = metadataProviderSettings?.openLibrary?.enabled ?? false;
|
||||
this.amazonEnabled = metadataProviderSettings?.amazon?.enabled ?? false;
|
||||
this.amazonCookie = metadataProviderSettings?.amazon?.cookie ?? "";
|
||||
this.selectedAmazonDomain = metadataProviderSettings?.amazon?.domain ?? 'com';
|
||||
this.goodreadsEnabled = metadataProviderSettings?.goodReads?.enabled ?? false;
|
||||
this.selectedGoogleLanguage = metadataProviderSettings?.google?.language ?? '';
|
||||
this.googleApiKey = metadataProviderSettings?.google?.apiKey ?? '';
|
||||
this.googleEnabled = (metadataProviderSettings?.google?.enabled ?? false) && this.googleApiKeyConfigured;
|
||||
this.hardcoverToken = metadataProviderSettings?.hardcover?.apiKey ?? '';
|
||||
this.hardcoverEnabled = metadataProviderSettings?.hardcover?.enabled ?? false;
|
||||
this.comicvineEnabled = metadataProviderSettings?.comicvine?.enabled ?? false;
|
||||
this.comicvineToken = metadataProviderSettings?.comicvine?.apiKey ?? '';
|
||||
this.doubanEnabled = metadataProviderSettings?.douban?.enabled ?? false;
|
||||
this.lubimyCzytacEnabled = metadataProviderSettings?.lubimyczytac?.enabled ?? false;
|
||||
this.ranobedbEnabled = metadataProviderSettings?.ranobedb?.enabled ?? false;
|
||||
this.ranobedbPreferRomaji = metadataProviderSettings?.ranobedb?.preferRomaji ?? false;
|
||||
this.audibleEnabled = metadataProviderSettings?.audible?.enabled ?? false;
|
||||
this.selectedAudibleDomain = metadataProviderSettings?.audible?.domain ?? 'com';
|
||||
this.appleBooksEnabled = metadataProviderSettings?.appleBooks?.enabled ?? false;
|
||||
this.selectedAppleBooksCountry = metadataProviderSettings?.appleBooks?.country ?? 'US';
|
||||
}
|
||||
|
||||
onTokenChange(newToken: string): void {
|
||||
this.hardcoverToken = newToken;
|
||||
if (!newToken.trim()) {
|
||||
this.hardcoverEnabled = false;
|
||||
for (const provider of this.catalog.providers()) {
|
||||
this.enabled[provider.id] = !!metadataProviderSettings?.[provider.settingsKey]?.enabled && !this.isToggleDisabled(provider.id);
|
||||
}
|
||||
}
|
||||
|
||||
onComicTokenChange(newToken: string): void {
|
||||
this.comicvineToken = newToken;
|
||||
private readonly requiredKey: Partial<Record<MetadataProviderId, () => string>> = {
|
||||
Google: () => this.googleApiKey,
|
||||
Hardcover: () => this.hardcoverToken,
|
||||
Comicvine: () => this.comicvineToken,
|
||||
};
|
||||
|
||||
isToggleDisabled(id: MetadataProviderId): boolean {
|
||||
const key = this.requiredKey[id];
|
||||
return key !== undefined && !key().trim();
|
||||
}
|
||||
|
||||
get googleApiKeyConfigured(): boolean {
|
||||
return this.googleApiKey.trim().length > 0;
|
||||
onKeyChange(id: MetadataProviderId, key: string): void {
|
||||
if (!key.trim()) {
|
||||
this.enabled[id] = false;
|
||||
}
|
||||
}
|
||||
|
||||
saveSettings(): void {
|
||||
const options: {
|
||||
[Id in MetadataProviderId]?: Omit<MetadataProviderSettings[Uncapitalize<Id>], 'enabled'>;
|
||||
} = {
|
||||
Amazon: {cookie: this.amazonCookie, domain: this.selectedAmazonDomain},
|
||||
Google: {language: this.selectedGoogleLanguage, apiKey: this.googleApiKey.trim()},
|
||||
Hardcover: {apiKey: this.hardcoverToken.trim()},
|
||||
Comicvine: {apiKey: this.comicvineToken.trim()},
|
||||
Ranobedb: {preferRomaji: this.ranobedbPreferRomaji},
|
||||
Audible: {domain: this.selectedAudibleDomain},
|
||||
AppleBooks: {country: this.selectedAppleBooksCountry},
|
||||
};
|
||||
const payload = [
|
||||
{
|
||||
key: AppSettingKey.METADATA_PROVIDER_SETTINGS,
|
||||
newValue: {
|
||||
openLibrary: {
|
||||
enabled: this.openLibraryEnabled,
|
||||
},
|
||||
amazon: {
|
||||
enabled: this.amazonEnabled,
|
||||
cookie: this.amazonCookie,
|
||||
domain: this.selectedAmazonDomain
|
||||
},
|
||||
comicvine: {
|
||||
enabled: this.comicvineEnabled,
|
||||
apiKey: this.comicvineToken.trim()
|
||||
},
|
||||
goodReads: {enabled: this.goodreadsEnabled},
|
||||
google: {
|
||||
enabled: this.googleEnabled && this.googleApiKeyConfigured,
|
||||
language: this.selectedGoogleLanguage,
|
||||
apiKey: this.googleApiKey.trim()
|
||||
},
|
||||
hardcover: {
|
||||
enabled: this.hardcoverEnabled,
|
||||
apiKey: this.hardcoverToken.trim()
|
||||
},
|
||||
douban: {enabled: this.doubanEnabled},
|
||||
lubimyczytac: {enabled: this.lubimyCzytacEnabled},
|
||||
ranobedb: {
|
||||
enabled: this.ranobedbEnabled,
|
||||
preferRomaji: this.ranobedbPreferRomaji
|
||||
},
|
||||
audible: {
|
||||
enabled: this.audibleEnabled,
|
||||
domain: this.selectedAudibleDomain
|
||||
},
|
||||
appleBooks: {
|
||||
enabled: this.appleBooksEnabled,
|
||||
country: this.selectedAppleBooksCountry,
|
||||
},
|
||||
}
|
||||
newValue: Object.fromEntries(this.catalog.providers().map(provider => [
|
||||
provider.settingsKey,
|
||||
{enabled: this.enabled[provider.id] ?? false, ...options[provider.id]},
|
||||
])),
|
||||
}
|
||||
];
|
||||
|
||||
this.appSettingsService.saveSettings(payload).pipe(
|
||||
takeUntilDestroyed(this.destroyRef)
|
||||
).subscribe({
|
||||
next: () =>
|
||||
next: () => {
|
||||
void this.sourceQuery.refreshProviders();
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: this.t.translate('common.success'),
|
||||
detail: this.t.translate('settingsMeta.providers.saveSuccess')
|
||||
}),
|
||||
});
|
||||
},
|
||||
error: () =>
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
|
||||
Reference in New Issue
Block a user