fix(ui): use signals to make ISBN import reactive (#2324)

This commit is contained in:
James Ward
2026-08-11 02:34:31 -04:00
committed by GitHub
parent df1e1aa32b
commit 42378ec8c9
2 changed files with 97 additions and 84 deletions
@@ -20,7 +20,7 @@
</div>
<!-- Phase 1: Upload & Parse -->
@if (phase === 'upload') {
@if (phase() === 'upload') {
<div class="form-container">
<div class="form-group highlight-group">
<label for="library" class="form-label">
@@ -40,7 +40,7 @@
<div class="gradient-divider"></div>
@if (entries.length === 0 && skipped.length === 0 && duplicatesRemoved === 0) {
@if (entries().length === 0 && skipped().length === 0 && duplicatesRemoved() === 0) {
<p-tabs value="file">
<p-tablist>
<p-tab value="file">{{ t('tabFile') }}</p-tab>
@@ -81,13 +81,13 @@
</p-tabpanels>
</p-tabs>
@if (parseError === 'fileTooLarge') {
@if (parseError() === 'fileTooLarge') {
<div class="parse-error">
<i class="pi pi-exclamation-triangle"></i>
{{ t('errorFileTooLarge') }}
</div>
}
@if (parseError === 'empty') {
@if (parseError() === 'empty') {
<div class="parse-error">
<i class="pi pi-exclamation-triangle"></i>
{{ t('errorNoIsbns') }}
@@ -98,22 +98,22 @@
<div class="parse-summary">
<div class="summary-stat success">
<i class="pi pi-check-circle"></i>
{{ t('validCount', { count: entries.length }) }}
{{ t('validCount', { count: entries().length }) }}
</div>
@if (skipped.length > 0) {
@if (skipped().length > 0) {
<div class="summary-stat warn" role="button" tabindex="0" (click)="showSkipped = !showSkipped" (keydown.enter)="showSkipped = !showSkipped">
<i class="pi pi-exclamation-triangle"></i>
{{ t('skippedCount', { count: skipped.length }) }}
{{ t('skippedCount', { count: skipped().length }) }}
<i class="pi" [class.pi-chevron-down]="!showSkipped" [class.pi-chevron-up]="showSkipped"></i>
</div>
}
@if (duplicatesRemoved > 0) {
@if (duplicatesRemoved() > 0) {
<div class="summary-stat info">
<i class="pi pi-copy"></i>
{{ t('duplicatesRemoved', { count: duplicatesRemoved }) }}
{{ t('duplicatesRemoved', { count: duplicatesRemoved() }) }}
</div>
}
@if (parseError === 'tooMany') {
@if (parseError() === 'tooMany') {
<div class="summary-stat warn">
<i class="pi pi-exclamation-triangle"></i>
{{ t('errorTooMany', { max: maxIsbnCount }) }}
@@ -121,9 +121,9 @@
}
</div>
@if (showSkipped && skipped.length > 0) {
@if (showSkipped && skipped().length > 0) {
<div class="skipped-list">
@for (item of skipped; track item.value) {
@for (item of skipped(); track item.value) {
<div class="skipped-item">
<code>{{ item.value }}</code>
<span class="skipped-reason">{{ t('reason.' + item.reason) }}</span>
@@ -144,7 +144,7 @@
(onClick)="clearParsed()"/>
</div>
<div class="preview-list">
@for (entry of entries; track entry.isbn; let i = $index) {
@for (entry of entries(); track entry.isbn; let i = $index) {
<div class="preview-item">
<span class="preview-index">{{ i + 1 }}.</span>
<code class="preview-isbn">{{ entry.isbn }}</code>
@@ -163,7 +163,7 @@
<i class="pi pi-exclamation-circle"></i>
<span>{{ t('validationLibraryRequired') }}</span>
</div>
} @else if (entries.length === 0) {
} @else if (entries().length === 0) {
<div class="validation-message error">
<i class="pi pi-exclamation-circle"></i>
<span>{{ t('validationNoIsbns') }}</span>
@@ -171,7 +171,7 @@
} @else {
<div class="validation-message success">
<i class="pi pi-check-circle"></i>
<span>{{ t('validationReady', { count: entries.length }) }}</span>
<span>{{ t('validationReady', { count: entries().length }) }}</span>
</div>
}
</div>
@@ -192,23 +192,23 @@
}
<!-- Phase 2: Processing -->
@if (phase === 'processing') {
@if (phase() === 'processing') {
<div class="form-container processing-container">
<div class="progress-section">
<div class="progress-header">
<span class="progress-label">{{ t('processingLabel', { current: processedCount, total: entries.length }) }}</span>
<span class="progress-percent">{{ progressPercent }}%</span>
<span class="progress-label">{{ t('processingLabel', { current: processedCount(), total: entries().length }) }}</span>
<span class="progress-percent">{{ progressPercent() }}%</span>
</div>
<p-progressbar [value]="progressPercent" [showValue]="false"/>
<p-progressbar [value]="progressPercent()" [showValue]="false"/>
<div class="progress-stats">
<span class="stat-created"><i class="pi pi-check-circle"></i> {{ createdCount }}</span>
<span class="stat-no-metadata"><i class="pi pi-exclamation-triangle"></i> {{ noMetadataCount }}</span>
<span class="stat-failed"><i class="pi pi-times-circle"></i> {{ failedCount }}</span>
<span class="stat-created"><i class="pi pi-check-circle"></i> {{ createdCount() }}</span>
<span class="stat-no-metadata"><i class="pi pi-exclamation-triangle"></i> {{ noMetadataCount() }}</span>
<span class="stat-failed"><i class="pi pi-times-circle"></i> {{ failedCount() }}</span>
</div>
</div>
<div class="isbn-progress-list">
@for (entry of entries; track entry.isbn) {
@for (entry of entries(); track entry.isbn) {
<div class="progress-item" [class]="'status-' + entry.status">
<div class="progress-item-icon">
@switch (entry.status) {
@@ -255,7 +255,7 @@
}
<!-- Phase 3: Summary -->
@if (phase === 'summary') {
@if (phase() === 'summary') {
<div class="form-container">
<div class="summary-header-bar">
<span class="summary-title">{{ t('summaryTitle') }}</span>
@@ -271,24 +271,24 @@
<div class="summary-card success">
<i class="pi pi-check-circle"></i>
<div class="summary-card-content">
<span class="summary-card-count">{{ createdCount }}</span>
<span class="summary-card-count">{{ createdCount() }}</span>
<span class="summary-card-label">{{ t('summaryCreated') }}</span>
</div>
</div>
@if (noMetadataCount > 0) {
@if (noMetadataCount() > 0) {
<div class="summary-card warn">
<i class="pi pi-exclamation-triangle"></i>
<div class="summary-card-content">
<span class="summary-card-count">{{ noMetadataCount }}</span>
<span class="summary-card-count">{{ noMetadataCount() }}</span>
<span class="summary-card-label">{{ t('summaryNoMetadata') }}</span>
</div>
</div>
}
@if (failedCount > 0) {
@if (failedCount() > 0) {
<div class="summary-card error">
<i class="pi pi-times-circle"></i>
<div class="summary-card-content">
<span class="summary-card-count">{{ failedCount }}</span>
<span class="summary-card-count">{{ failedCount() }}</span>
<span class="summary-card-label">{{ t('summaryFailed') }}</span>
</div>
</div>
@@ -297,7 +297,7 @@
<div class="summary-card info">
<i class="pi pi-info-circle"></i>
<div class="summary-card-content">
<span class="summary-card-count">{{ entries.length - processedCount }}</span>
<span class="summary-card-count">{{ entries().length - processedCount() }}</span>
<span class="summary-card-label">{{ t('summaryCancelled') }}</span>
</div>
</div>
@@ -305,7 +305,7 @@
</div>
<div class="summary-detail-list">
@for (entry of entries; track entry.isbn) {
@for (entry of entries(); track entry.isbn) {
@if (entry.status !== 'pending') {
<div class="summary-detail-item" [class]="'status-' + entry.status">
<div class="summary-detail-icon">
@@ -1,4 +1,4 @@
import {Component, effect, inject} from '@angular/core';
import {Component, computed, effect, inject, signal, WritableSignal} from '@angular/core';
import {DynamicDialogConfig, DynamicDialogRef} from '@openng/optimus-ui/dynamicdialog';
import {FormsModule} from '@angular/forms';
import {Button} from '@openng/optimus-ui/button';
@@ -66,20 +66,20 @@ export class BulkIsbnImportDialogComponent {
selectedLibraryId: number | null = null;
phase: ImportPhase = 'upload';
phase: WritableSignal<ImportPhase> = signal('upload');
pasteText = '';
entries: IsbnEntry[] = [];
skipped: SkippedEntry[] = [];
duplicatesRemoved = 0;
entries: WritableSignal<IsbnEntry[]> = signal([]);
skipped: WritableSignal<SkippedEntry[]> = signal([]);
duplicatesRemoved = signal(0);
showSkipped = false;
processedCount = 0;
createdCount = 0;
noMetadataCount = 0;
failedCount = 0;
processedCount = signal(0);
createdCount = signal(0);
noMetadataCount = signal(0);
failedCount = signal(0);
cancelled = false;
parseError = '';
parseError = signal('');
readonly maxIsbnCount = MAX_ISBN_COUNT;
private readonly initializeSelectedLibraryEffect = effect(() => {
const libraries = this.libraries;
@@ -106,7 +106,7 @@ export class BulkIsbnImportDialogComponent {
if (!file) return;
if (file.size > MAX_FILE_SIZE_BYTES) {
this.parseError = 'fileTooLarge';
this.parseError.set('fileTooLarge');
return;
}
@@ -124,39 +124,58 @@ export class BulkIsbnImportDialogComponent {
}
clearParsed(): void {
this.entries = [];
this.skipped = [];
this.duplicatesRemoved = 0;
this.parseError = '';
this.entries.set([]);
this.skipped.set([]);
this.duplicatesRemoved.set(0);
this.parseError.set('');
this.pasteText = '';
}
canStartImport(): boolean {
return this.phase === 'upload' && this.entries.length > 0 && !!this.selectedLibraryId;
return this.phase() === 'upload' && this.entries().length > 0 && !!this.selectedLibraryId;
}
async startImport(): Promise<void> {
if (!this.canStartImport()) return;
this.phase = 'processing';
this.processedCount = 0;
this.createdCount = 0;
this.noMetadataCount = 0;
this.failedCount = 0;
this.phase.set('processing');
this.processedCount.set(0);
this.createdCount.set(0);
this.noMetadataCount.set(0);
this.failedCount.set(0);
this.cancelled = false;
for (const entry of this.entries) {
const updateEntry = (index: number, value: Partial<IsbnEntry>) => {
this.entries.update((e) => {
e[index] = {
...e[index],
...value
}
return [...e];
});
};
for (let i = 0; i < this.entries().length; i++) {
if (this.cancelled) break;
entry.status = 'looking-up';
if (i > 0) {
await this.delay(DELAY_BETWEEN_REQUESTS_MS);
}
if (this.cancelled) break;
const { isbn } = this.entries()[i];
updateEntry(i, { status: 'looking-up' })
try {
const metadata = await this.lookupIsbn(entry.isbn);
const metadata = await this.lookupIsbn(isbn);
if (this.cancelled) break;
const request: CreatePhysicalBookRequest = {
libraryId: this.selectedLibraryId!,
isbn: entry.isbn,
isbn: isbn,
title: metadata?.title || undefined,
authors: metadata?.authors?.length ? [...metadata.authors] : undefined,
description: metadata?.description || undefined,
@@ -171,27 +190,21 @@ export class BulkIsbnImportDialogComponent {
await this.createBook(request);
if (metadata?.title) {
entry.status = 'created';
entry.title = metadata.title;
this.createdCount++;
updateEntry(i, { status: 'created', title: metadata.title })
this.createdCount.update(v => v + 1);
} else {
entry.status = 'created-no-metadata';
this.noMetadataCount++;
updateEntry(i, { status: 'created-no-metadata' })
this.noMetadataCount.update(v => v + 1);
}
} catch (err: unknown) {
entry.status = 'failed';
entry.error = err instanceof Error ? err.message : 'Unknown error';
this.failedCount++;
updateEntry(i, { status: 'failed', error: err instanceof Error ? err.message : 'Unknown error' })
this.failedCount.update(v => v + 1);
}
this.processedCount++;
if (!this.cancelled && entry !== this.entries[this.entries.length - 1]) {
await this.delay(DELAY_BETWEEN_REQUESTS_MS);
}
this.processedCount.update(v => v + 1);
}
this.phase = 'summary';
this.phase.set('summary');
}
cancelImport(): void {
@@ -202,18 +215,18 @@ export class BulkIsbnImportDialogComponent {
this.dynamicDialogRef.close();
}
get progressPercent(): number {
return this.entries.length > 0 ? Math.round((this.processedCount / this.entries.length) * 100) : 0;
}
progressPercent = computed(
() => this.entries().length > 0 ? Math.round((this.processedCount() / this.entries().length) * 100) : 0
);
get selectedLibraryName(): string {
return this.libraries.find(l => l.id === this.selectedLibraryId)?.name ?? '';
}
private parseContent(content: string, source: string): void {
this.parseError = '';
this.skipped = [];
this.duplicatesRemoved = 0;
this.parseError.set('');
this.skipped.set([]);
this.duplicatesRemoved.set(0);
// Strip BOM
const cleaned = content.replace(/^\uFEFF/, '');
@@ -241,17 +254,17 @@ export class BulkIsbnImportDialogComponent {
const normalized = this.normalizeIsbn(raw);
if (!this.isValidIsbn(normalized)) {
this.skipped.push({value: raw, reason: 'invalid'});
this.skipped.update(s => [...s, {value: raw, reason: 'invalid'}]);
continue;
}
if (seen.has(normalized)) {
this.duplicatesRemoved++;
this.duplicatesRemoved.update(v => v + 1);
continue;
}
if (existingIsbns.has(normalized)) {
this.skipped.push({value: normalized, reason: 'alreadyExists'});
this.skipped.update(s => [...s, {value: normalized, reason: 'alreadyExists'}]);
continue;
}
@@ -260,14 +273,14 @@ export class BulkIsbnImportDialogComponent {
}
if (validEntries.length > MAX_ISBN_COUNT) {
this.parseError = 'tooMany';
this.entries = validEntries.slice(0, MAX_ISBN_COUNT);
this.parseError.set('tooMany');
this.entries.set(validEntries.slice(0, MAX_ISBN_COUNT));
} else {
this.entries = validEntries;
this.entries.set(validEntries);
}
if (this.entries.length === 0 && this.skipped.length === 0) {
this.parseError = 'empty';
if (this.entries().length === 0 && this.skipped().length === 0) {
this.parseError.set('empty');
}
}