Refactor metadata picker and parsers for Amazon, Goodreads, and Google (#2327)
Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export * from './metadata-field.config';
|
||||
export * from './metadata-utils.service';
|
||||
export * from './metadata-form.builder';
|
||||
@@ -0,0 +1,79 @@
|
||||
import {MetadataProviderSpecificFields} from '../model/app-settings.model';
|
||||
|
||||
export type FieldType = 'string' | 'number' | 'array' | 'textarea';
|
||||
|
||||
export interface MetadataFieldConfig {
|
||||
label: string;
|
||||
controlName: string;
|
||||
lockedKey: string;
|
||||
fetchedKey: string;
|
||||
type: FieldType;
|
||||
providerKey?: keyof MetadataProviderSpecificFields;
|
||||
}
|
||||
|
||||
export const ALL_METADATA_FIELDS: MetadataFieldConfig[] = [
|
||||
{label: 'Title', controlName: 'title', lockedKey: 'titleLocked', fetchedKey: 'title', type: 'string'},
|
||||
{label: 'Subtitle', controlName: 'subtitle', lockedKey: 'subtitleLocked', fetchedKey: 'subtitle', type: 'string'},
|
||||
{label: 'Publisher', controlName: 'publisher', lockedKey: 'publisherLocked', fetchedKey: 'publisher', type: 'string'},
|
||||
{label: 'Published', controlName: 'publishedDate', lockedKey: 'publishedDateLocked', fetchedKey: 'publishedDate', type: 'string'},
|
||||
{label: 'Authors', controlName: 'authors', lockedKey: 'authorsLocked', fetchedKey: 'authors', type: 'array'},
|
||||
{label: 'Genres', controlName: 'categories', lockedKey: 'categoriesLocked', fetchedKey: 'categories', type: 'array'},
|
||||
{label: 'Moods', controlName: 'moods', lockedKey: 'moodsLocked', fetchedKey: 'moods', type: 'array'},
|
||||
{label: 'Tags', controlName: 'tags', lockedKey: 'tagsLocked', fetchedKey: 'tags', type: 'array'},
|
||||
{label: 'Description', controlName: 'description', lockedKey: 'descriptionLocked', fetchedKey: 'description', type: 'textarea'},
|
||||
{label: 'Series', controlName: 'seriesName', lockedKey: 'seriesNameLocked', fetchedKey: 'seriesName', type: 'string'},
|
||||
{label: 'Book #', controlName: 'seriesNumber', lockedKey: 'seriesNumberLocked', fetchedKey: 'seriesNumber', type: 'number'},
|
||||
{label: 'Total Books', controlName: 'seriesTotal', lockedKey: 'seriesTotalLocked', fetchedKey: 'seriesTotal', type: 'number'},
|
||||
{label: 'Language', controlName: 'language', lockedKey: 'languageLocked', fetchedKey: 'language', type: 'string'},
|
||||
{label: 'ISBN-10', controlName: 'isbn10', lockedKey: 'isbn10Locked', fetchedKey: 'isbn10', type: 'string'},
|
||||
{label: 'ISBN-13', controlName: 'isbn13', lockedKey: 'isbn13Locked', fetchedKey: 'isbn13', type: 'string'},
|
||||
{label: 'Pages', controlName: 'pageCount', lockedKey: 'pageCountLocked', fetchedKey: 'pageCount', type: 'number'},
|
||||
{label: 'Google ID', controlName: 'googleId', lockedKey: 'googleIdLocked', fetchedKey: 'googleId', type: 'string', providerKey: 'googleId'},
|
||||
{label: 'ASIN', controlName: 'asin', lockedKey: 'asinLocked', fetchedKey: 'asin', type: 'string', providerKey: 'asin'},
|
||||
{label: 'Amazon #', controlName: 'amazonReviewCount', lockedKey: 'amazonReviewCountLocked', fetchedKey: 'amazonReviewCount', type: 'number', providerKey: 'amazonReviewCount'},
|
||||
{label: 'Amazon ★', controlName: 'amazonRating', lockedKey: 'amazonRatingLocked', fetchedKey: 'amazonRating', type: 'number', providerKey: 'amazonRating'},
|
||||
{label: 'Goodreads ID', controlName: 'goodreadsId', lockedKey: 'goodreadsIdLocked', fetchedKey: 'goodreadsId', type: 'string', providerKey: 'goodreadsId'},
|
||||
{label: 'Goodreads ★', controlName: 'goodreadsReviewCount', lockedKey: 'goodreadsReviewCountLocked', fetchedKey: 'goodreadsReviewCount', type: 'number', providerKey: 'goodreadsReviewCount'},
|
||||
{label: 'Goodreads #', controlName: 'goodreadsRating', lockedKey: 'goodreadsRatingLocked', fetchedKey: 'goodreadsRating', type: 'number', providerKey: 'goodreadsRating'},
|
||||
{label: 'HC Book ID', controlName: 'hardcoverBookId', lockedKey: 'hardcoverBookIdLocked', fetchedKey: 'hardcoverBookId', type: 'number', providerKey: 'hardcoverId'},
|
||||
{label: 'Hardcover ID', controlName: 'hardcoverId', lockedKey: 'hardcoverIdLocked', fetchedKey: 'hardcoverId', type: 'string', providerKey: 'hardcoverId'},
|
||||
{label: 'Hardcover #', controlName: 'hardcoverReviewCount', lockedKey: 'hardcoverReviewCountLocked', fetchedKey: 'hardcoverReviewCount', type: 'number', providerKey: 'hardcoverReviewCount'},
|
||||
{label: 'Hardcover ★', controlName: 'hardcoverRating', lockedKey: 'hardcoverRatingLocked', fetchedKey: 'hardcoverRating', type: 'number', providerKey: 'hardcoverRating'},
|
||||
{label: 'Comicvine ID', controlName: 'comicvineId', lockedKey: 'comicvineIdLocked', fetchedKey: 'comicvineId', type: 'string', providerKey: 'comicvineId'},
|
||||
{label: 'LB ID', controlName: 'lubimyczytacId', lockedKey: 'lubimyczytacIdLocked', fetchedKey: 'lubimyczytacId', type: 'string', providerKey: 'lubimyczytacId'},
|
||||
{label: 'LB ★', controlName: 'lubimyczytacRating', lockedKey: 'lubimyczytacRatingLocked', fetchedKey: 'lubimyczytacRating', type: 'number', providerKey: 'lubimyczytacRating'},
|
||||
{label: 'Ranobedb ID', controlName: 'ranobedbId', lockedKey: 'ranobedbIdLocked', fetchedKey: 'ranobedbId', type: 'string', providerKey: 'ranobedbId'},
|
||||
{label: 'Ranobedb ★', controlName: 'ranobedbRating', lockedKey: 'ranobedbRatingLocked', fetchedKey: 'ranobedbRating', type: 'number', providerKey: 'ranobedbRating'}
|
||||
];
|
||||
|
||||
export const TOP_FIELD_NAMES = ['title', 'subtitle', 'publisher', 'publishedDate'];
|
||||
export const ARRAY_FIELD_NAMES = ['authors', 'categories', 'moods', 'tags'];
|
||||
export const TEXTAREA_FIELD_NAMES = ['description'];
|
||||
|
||||
export function getTopFields(): MetadataFieldConfig[] {
|
||||
return ALL_METADATA_FIELDS.filter(f => TOP_FIELD_NAMES.includes(f.controlName));
|
||||
}
|
||||
|
||||
export function getArrayFields(): MetadataFieldConfig[] {
|
||||
return ALL_METADATA_FIELDS.filter(f => f.type === 'array');
|
||||
}
|
||||
|
||||
export function getTextareaFields(): MetadataFieldConfig[] {
|
||||
return ALL_METADATA_FIELDS.filter(f => f.type === 'textarea');
|
||||
}
|
||||
|
||||
export function getBottomFields(enabledProviderFields?: MetadataProviderSpecificFields | null): MetadataFieldConfig[] {
|
||||
const bottomFields = ALL_METADATA_FIELDS.filter(f =>
|
||||
!TOP_FIELD_NAMES.includes(f.controlName) &&
|
||||
!ARRAY_FIELD_NAMES.includes(f.controlName) &&
|
||||
!TEXTAREA_FIELD_NAMES.includes(f.controlName)
|
||||
);
|
||||
|
||||
if (enabledProviderFields) {
|
||||
return bottomFields.filter(field =>
|
||||
!field.providerKey || enabledProviderFields[field.providerKey]
|
||||
);
|
||||
}
|
||||
|
||||
return bottomFields;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {FormControl, FormGroup} from '@angular/forms';
|
||||
import {ALL_METADATA_FIELDS, MetadataFieldConfig} from './metadata-field.config';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MetadataFormBuilder {
|
||||
|
||||
buildForm(
|
||||
includeLockedControls: boolean = true,
|
||||
fields: MetadataFieldConfig[] = ALL_METADATA_FIELDS
|
||||
): FormGroup {
|
||||
const controls: Record<string, FormControl> = {};
|
||||
|
||||
for (const field of fields) {
|
||||
const defaultValue = this.getDefaultValue(field.type);
|
||||
controls[field.controlName] = new FormControl(defaultValue);
|
||||
|
||||
if (includeLockedControls) {
|
||||
controls[field.lockedKey] = new FormControl(false);
|
||||
}
|
||||
}
|
||||
|
||||
controls['thumbnailUrl'] = new FormControl('');
|
||||
if (includeLockedControls) {
|
||||
controls['coverLocked'] = new FormControl(false);
|
||||
}
|
||||
|
||||
return new FormGroup(controls);
|
||||
}
|
||||
|
||||
private getDefaultValue(type: string): unknown {
|
||||
switch (type) {
|
||||
case 'array':
|
||||
return [];
|
||||
case 'number':
|
||||
return null;
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
applyLockStates(
|
||||
metadataForm: FormGroup,
|
||||
lockedFields: Record<string, boolean>,
|
||||
fields: MetadataFieldConfig[] = ALL_METADATA_FIELDS
|
||||
): void {
|
||||
for (const key of Object.keys(metadataForm.controls)) {
|
||||
if (!key.endsWith('Locked')) {
|
||||
metadataForm.get(key)?.enable({emitEvent: false});
|
||||
}
|
||||
}
|
||||
|
||||
for (const field of fields) {
|
||||
if (lockedFields[field.lockedKey]) {
|
||||
metadataForm.get(field.controlName)?.disable({emitEvent: false});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setAllFieldsLocked(metadataForm: FormGroup, locked: boolean): void {
|
||||
for (const key of Object.keys(metadataForm.controls)) {
|
||||
if (key.endsWith('Locked')) {
|
||||
metadataForm.get(key)?.setValue(locked);
|
||||
const fieldName = key.replace('Locked', '');
|
||||
if (locked) {
|
||||
metadataForm.get(fieldName)?.disable();
|
||||
} else {
|
||||
metadataForm.get(fieldName)?.enable();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {FormGroup} from '@angular/forms';
|
||||
import {BookMetadata} from '../../features/book/model/book.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MetadataUtilsService {
|
||||
|
||||
copyFieldToForm(
|
||||
field: string,
|
||||
fetchedMetadata: BookMetadata,
|
||||
metadataForm: FormGroup,
|
||||
copiedFields: Record<string, boolean>
|
||||
): boolean {
|
||||
const value = fetchedMetadata[field as keyof BookMetadata];
|
||||
if (value !== null && value !== undefined && value !== '') {
|
||||
metadataForm.get(field)?.setValue(value);
|
||||
copiedFields[field] = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
copyMissingFields(
|
||||
fetchedMetadata: BookMetadata,
|
||||
metadataForm: FormGroup,
|
||||
copiedFields: Record<string, boolean>,
|
||||
copyCallback: (field: string) => void
|
||||
): void {
|
||||
for (const field of Object.keys(fetchedMetadata)) {
|
||||
const isLocked = metadataForm.get(`${field}Locked`)?.value;
|
||||
const currentValue = metadataForm.get(field)?.value;
|
||||
const fetchedValue = fetchedMetadata[field as keyof BookMetadata];
|
||||
|
||||
const isEmpty = Array.isArray(currentValue)
|
||||
? currentValue.length === 0
|
||||
: (currentValue === null || currentValue === undefined || currentValue === '');
|
||||
|
||||
const hasFetchedValue = fetchedValue !== null && fetchedValue !== undefined && fetchedValue !== '';
|
||||
|
||||
if (!isLocked && isEmpty && hasFetchedValue) {
|
||||
copyCallback(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
copyAllFields(
|
||||
fetchedMetadata: BookMetadata,
|
||||
metadataForm: FormGroup,
|
||||
copyCallback: (field: string) => void,
|
||||
excludeFields: string[] = ['thumbnailUrl', 'bookId']
|
||||
): void {
|
||||
for (const field of Object.keys(fetchedMetadata)) {
|
||||
if (excludeFields.includes(field)) continue;
|
||||
|
||||
const isLocked = metadataForm.get(`${field}Locked`)?.value;
|
||||
const fetchedValue = fetchedMetadata[field as keyof BookMetadata];
|
||||
|
||||
if (!isLocked && fetchedValue != null && fetchedValue !== '') {
|
||||
copyCallback(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isValueEmpty(value: unknown): boolean {
|
||||
if (value === null || value === undefined || value === '') return true;
|
||||
if (Array.isArray(value)) return value.length === 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
areFieldsEqual(field1: unknown, field2: unknown): boolean {
|
||||
const [normalized1, normalized2] = this.normalizeForComparison(field1, field2);
|
||||
return normalized1 === normalized2;
|
||||
}
|
||||
|
||||
normalizeForComparison(field1: unknown, field2: unknown): [string | undefined, string | undefined] {
|
||||
let val1: string | undefined = undefined;
|
||||
let val2: string | undefined = undefined;
|
||||
|
||||
if (Array.isArray(field1)) {
|
||||
val1 = field1.length > 0 ? JSON.stringify([...field1].sort()) : undefined;
|
||||
} else if (field1 != null && field1 !== '') {
|
||||
val1 = String(field1);
|
||||
}
|
||||
|
||||
if (Array.isArray(field2)) {
|
||||
val2 = field2.length > 0 ? JSON.stringify([...field2].sort()) : undefined;
|
||||
} else if (field2 != null && field2 !== '') {
|
||||
val2 = String(field2);
|
||||
}
|
||||
|
||||
return [val1, val2];
|
||||
}
|
||||
|
||||
isValueChanged(field: string, metadataForm: FormGroup, originalMetadata?: BookMetadata): boolean {
|
||||
if (!originalMetadata) return false;
|
||||
const [current, original] = this.normalizeForComparison(
|
||||
metadataForm.get(field)?.value,
|
||||
originalMetadata[field as keyof BookMetadata]
|
||||
);
|
||||
return (!!current && current !== original) || (!current && !!original);
|
||||
}
|
||||
|
||||
isFetchedDifferent(field: string, metadataForm: FormGroup, fetchedMetadata: BookMetadata): boolean {
|
||||
const [current, fetched] = this.normalizeForComparison(
|
||||
metadataForm.get(field)?.value,
|
||||
fetchedMetadata[field as keyof BookMetadata]
|
||||
);
|
||||
return !!fetched && fetched !== current;
|
||||
}
|
||||
|
||||
resetField(
|
||||
field: string,
|
||||
metadataForm: FormGroup,
|
||||
originalMetadata: BookMetadata | undefined,
|
||||
copiedFields: Record<string, boolean>,
|
||||
hoveredFields?: Record<string, boolean>
|
||||
): void {
|
||||
metadataForm.get(field)?.setValue(originalMetadata?.[field as keyof BookMetadata]);
|
||||
copiedFields[field] = false;
|
||||
if (hoveredFields) {
|
||||
hoveredFields[field] = false;
|
||||
}
|
||||
}
|
||||
|
||||
patchMetadataToForm(
|
||||
metadata: BookMetadata,
|
||||
metadataForm: FormGroup,
|
||||
fields: Array<{ controlName: string; lockedKey: string; type: string }>
|
||||
): void {
|
||||
const patchData: Record<string, unknown> = {};
|
||||
|
||||
for (const field of fields) {
|
||||
const key = field.controlName as keyof BookMetadata;
|
||||
const lockedKey = field.lockedKey as keyof BookMetadata;
|
||||
const value = metadata[key];
|
||||
|
||||
if (field.type === 'array') {
|
||||
patchData[field.controlName] = [...(value as string[] ?? [])].sort();
|
||||
} else {
|
||||
patchData[field.controlName] = value ?? null;
|
||||
}
|
||||
patchData[field.lockedKey] = metadata[lockedKey] ?? false;
|
||||
}
|
||||
|
||||
metadataForm.patchValue(patchData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user