Support multi select for metadata

This commit is contained in:
aditya.chandel
2025-01-08 11:57:05 -07:00
parent 65b7e6df8d
commit ccc2fa23b6
16 changed files with 632 additions and 47 deletions
+2
View File
@@ -42,6 +42,7 @@ import {providePrimeNG} from 'primeng/config';
import Aura from '@primeng/themes/aura';
import {IftaLabel} from 'primeng/iftalabel';
import {LoadingOverlayComponent} from './loading-overlay/loading-overlay.component';
import {MultiSelectModule} from 'primeng/multiselect';
@NgModule({
declarations: [
@@ -81,6 +82,7 @@ import {LoadingOverlayComponent} from './loading-overlay/loading-overlay.compone
ProgressSpinnerModule,
IftaLabel,
LoadingOverlayComponent,
MultiSelectModule
],
providers: [
DialogService,
@@ -1,8 +1,8 @@
<form *ngIf="!selectedFetchedMetadata" class="flex flex-col w-full h-full pt-2" [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="flex w-full gap-x-4 items-end">
<div class="flex flex-col gap-1 w-1/6">
<label for="provider">Provider</label>
<p-select id="provider" class="w-full" [options]="providers" formControlName="provider" placeholder="Select Provider.."></p-select>
<label for="provider">Providers</label>
<p-multiselect id="provider" class="w-full" [filter]="false" showToggleAll="false" [options]="providers" formControlName="provider" placeholder="Select Providers"></p-multiselect>
</div>
<div class="flex flex-col gap-1 w-1/6">
@@ -30,21 +30,25 @@
</div>
<div *ngIf="!loading" class="mt-8 flex-1 overflow-y-auto">
<div *ngFor="let book of allFetchedMetadata" class="book-item flex flex-col w-full">
<div class="flex w-full flex-row book-item-content" (click)="onBookClick(book)">
<img [src]="book.thumbnailUrl" alt="Image" class="img-fixed-dimensions pr-2"/>
<div *ngFor="let metadata of allFetchedMetadata" class="book-item flex flex-col w-full">
<div class="flex w-full flex-row book-item-content" (click)="onBookClick(metadata)">
<img [src]="metadata.thumbnailUrl || 'assets/book-cover-metadata.png'" alt="Image" class="img-fixed-dimensions pr-2"/>
<div class=" flex flex-col w-full m-2">
<div class="flex flex-row gap-4 justify-between">
<p class="font-bold">{{ truncateText(book['title'], 90) }}</p>
<div class="flex gap-4">
<p>ISBN: {{ book['isbn10'] }}</p>
<p class="font-bold">{{ truncateText(metadata['title'], 90) }}</p>
<div class="flex gap-2">
<p>ISBN: {{ metadata['isbn10'] || metadata['asin'] }}</p>
<p> | </p>
<p>Published: {{ book['publishedDate'] }}</p>
<p>Published: {{ metadata['publishedDate'] }}</p>
</div>
</div>
<p>by {{ truncateText(book['authors'].join(', '), 70) }}</p>
<div class="flex gap-2">
<p>by {{ truncateText(metadata['authors'].join(', '), 70) }}</p>
<p> | </p>
<p>Source: <span [innerHTML]="buildProviderLink(metadata)"></span></p>
</div>
<p class="mt-4 truncate-text p-secondary">
{{ book['description'] }}
{{ sanitizeHtml(metadata['description']) }}
</p>
</div>
</div>
@@ -12,6 +12,7 @@ import {FetchedMetadata} from '../../book/model/book.model';
import {ProgressSpinner} from 'primeng/progressspinner';
import {MetadataPickerComponent} from '../metadata-picker/metadata-picker.component';
import {BookMetadataCenterService} from '../book-metadata-center.service';
import {MultiSelect} from 'primeng/multiselect';
@Component({
selector: 'app-metadata-searcher',
@@ -26,7 +27,8 @@ import {BookMetadataCenterService} from '../book-metadata-center.service';
NgForOf,
NgIf,
ProgressSpinner,
MetadataPickerComponent
MetadataPickerComponent,
MultiSelect
],
standalone: true
})
@@ -70,16 +72,16 @@ export class MetadataSearcherComponent implements OnInit {
onSubmit(): void {
if (this.form.valid) {
const providerKey = Object.keys(Provider).find(
key => Provider[key as keyof typeof Provider] === this.form.get('provider')?.value
const providerKeys = Object.keys(Provider).filter(key =>
(this.form.get('provider')?.value as string[]).includes(Provider[key as keyof typeof Provider])
);
if (!providerKey) {
if (!providerKeys) {
console.error('Invalid provider selected.');
return;
}
const fetchRequest: FetchMetadataRequest = {
bookId: this.bookId,
provider: providerKey,
providers: providerKeys,
title: this.form.get('title')?.value,
isbn: this.form.get('isbn')?.value,
author: this.form.get('author')?.value
@@ -103,6 +105,20 @@ export class MetadataSearcherComponent implements OnInit {
}
}
buildProviderLink(metadata: { provider: string; providerBookId: string }): string {
if (!metadata.provider || !metadata.providerBookId) {
throw new Error("Invalid metadata: 'provider' or 'providerBookId' is missing.");
}
switch (metadata.provider) {
case "AMAZON":
return `<a href="https://www.amazon.com/dp/${metadata.providerBookId}" target="_blank">Amazon</a>`;
case "GOOD_READS":
return `<a href="https://www.goodreads.com/book/show/${metadata.providerBookId}" target="_blank">Goodreads</a>`;
default:
throw new Error(`Unsupported provider: ${metadata.provider}`);
}
}
truncateText(text: string | null, length: number): string {
const safeText = text ?? '';
return safeText.length > length ? safeText.substring(0, length) + '...' : safeText;
@@ -115,4 +131,11 @@ export class MetadataSearcherComponent implements OnInit {
onGoBack() {
this.selectedFetchedMetadata = null;
}
sanitizeHtml(htmlString: string | null | undefined): string {
if (!htmlString) {
return '';
}
return htmlString.replace(/<\/?[^>]+(>|$)/g, '').trim();
}
}
@@ -10,7 +10,7 @@
<th class="max-w-16">Cover</th>
<th class="w-1/2 max-w-52 overflow-hidden truncate" pResizableColumn>Title</th>
<th class="w-1/6 max-w-48 overflow-hidden truncate">Publisher</th>
<th class="w-1/12 max-w-40 overflow-hidden truncate" pResizableColumn>Subtitle</th>
<th class="w-1/8 max-w-44 overflow-hidden truncate" pResizableColumn>Subtitle</th>
<th class="w-1/8 max-w-36 overflow-hidden truncate">ISBN</th>
<th class="w-1/8 max-w-32 overflow-hidden truncate">Language</th>
<th class="w-1/8 max-w-36 overflow-hidden truncate">Rating</th>
@@ -21,12 +21,10 @@
</ng-template>
<ng-template #body let-book>
<tr style="height:46px">
<td class="w-6 h-6">
<img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="size-7"/>
</td>
<td class="w-1/2 max-w-52 overflow-hidden truncate" [title]="book.metadata?.title">{{ book.metadata?.title }}</td>
<td (click)="openMetadataCenter(book.id)" class="w-6 h-6"><img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="size-7"/></td>
<td (click)="openMetadataCenter(book.id)" class="w-1/2 max-w-52 overflow-hidden truncate" [title]="book.metadata?.title">{{ book.metadata?.title }} </td>
<td class="w-1/6 max-w-48 overflow-hidden truncate">{{ book.metadata?.publisher }}</td>
<td class="w-1/12 max-w-40 overflow-hidden truncate">{{ book.metadata?.subtitle }}</td>
<td class="w-1/8 max-w-44 overflow-hidden truncate">{{ book.metadata?.subtitle }}</td>
<td class="w-1/8 max-w-36 overflow-hidden truncate">{{ book.metadata?.isbn10 }}</td>
<td class="w-1/8 max-w-32 overflow-hidden truncate">{{ book.metadata?.language }}</td>
<td class="w-1/8 max-w-36 overflow-hidden truncate">
@@ -7,6 +7,8 @@ import {ProgressSpinner} from 'primeng/progressspinner';
import {BookService} from '../../service/book.service';
import {Rating} from 'primeng/rating';
import {FormsModule} from '@angular/forms';
import {BookMetadataCenterComponent} from '../../../book-metadata-center/book-metadata-center.component';
import {DialogService} from 'primeng/dynamicdialog';
@Component({
selector: 'app-book-table',
@@ -25,11 +27,38 @@ import {FormsModule} from '@angular/forms';
export class BookTableComponent {
bookState$: Observable<BookState>;
constructor(private bookService: BookService) {
constructor(private bookService: BookService, private dialogService: DialogService) {
this.bookState$ = this.bookService.bookState$;
}
getBookCoverUrl(bookId: number): string {
return this.bookService.getBookCoverUrl(bookId);
}
openMetadataCenter(id: number) {
this.openBookDetailsDialog(id);
}
openBookDetailsDialog(bookId: number): void {
this.bookService.getBookByIdFromAPI(bookId, true).subscribe(({
next: (book) => {
this.dialogService.open(BookMetadataCenterComponent, {
header: 'Open book details',
modal: true,
closable: true,
width: '1200px',
height: '835px',
showHeader: false,
closeOnEscape: true,
dismissableMask: true,
data: {
book: book
}
});
},
error: (error) => {
console.error('Error fetching book:', error);
}
}))
}
}
+4 -3
View File
@@ -1,5 +1,5 @@
import {Shelf} from './shelf.model';
import {SafeUrl} from '@angular/platform-browser';
import {Provider} from './provider.model';
export interface Book {
id: number;
@@ -30,8 +30,8 @@ export interface BookMetadata {
export interface FetchedMetadata {
bookId: number | null;
googleBookId: string | null;
amazonBookId: string | null;
provider: string,
providerBookId: string,
title: string | null;
subtitle?: string | null;
publisher: string | null;
@@ -39,6 +39,7 @@ export interface FetchedMetadata {
description: string | null;
isbn13: string | null;
isbn10: string | null;
asin: string | null;
pageCount: number | null;
thumbnailUrl: string | undefined;
language: string | null;
@@ -1,6 +1,6 @@
export interface FetchMetadataRequest {
bookId: number,
provider: string,
providers: string[],
title: string,
isbn: string,
author: string
@@ -1,5 +1,5 @@
export enum Provider {
AMAZON = 'Amazon',
GOOGLE_BOOKS = 'Google Books',
GOOGLE_BOOKS_API = 'Google Books (API)'
GOOGLE = 'Google Books',
GOOD_READS = 'Good Reads'
}