From 237171ffe4372f3772b46dc474c0b0aaa96cb35e Mon Sep 17 00:00:00 2001 From: brios <127139797+balazs-szucs@users.noreply.github.com> Date: Mon, 4 May 2026 16:41:48 +0200 Subject: [PATCH] fix(frontend): unify custom metadata fetch dialog flow (#1088) Co-authored-by: Alex <25013571+alexhb1@users.noreply.github.com> --- .../book-dialog-helper.service.ts | 18 +- .../library-shelf-menu.service.spec.ts | 11 +- .../service/library-shelf-menu.service.ts | 323 +++++++++--------- .../metadata-fetch-options.component.spec.ts | 34 ++ .../metadata-fetch-options.component.ts | 39 ++- .../multi-book-metadata-fetch-component.html | 31 +- ...ulti-book-metadata-fetch-component.spec.ts | 14 + .../multi-book-metadata-fetch-component.ts | 44 ++- .../services/dialog-launcher.service.ts | 13 - 9 files changed, 318 insertions(+), 209 deletions(-) diff --git a/frontend/src/app/features/book/components/book-browser/book-dialog-helper.service.ts b/frontend/src/app/features/book/components/book-browser/book-dialog-helper.service.ts index b9703d53b..cb78ee73c 100644 --- a/frontend/src/app/features/book/components/book-browser/book-dialog-helper.service.ts +++ b/frontend/src/app/features/book/components/book-browser/book-dialog-helper.service.ts @@ -19,6 +19,12 @@ import {AddPhysicalBookDialogComponent} from '../add-physical-book-dialog/add-ph import {BulkIsbnImportDialogComponent} from '../bulk-isbn-import-dialog/bulk-isbn-import-dialog.component'; import {DuplicateMergerComponent} from '../duplicate-merger/duplicate-merger.component'; +interface MetadataRefreshDialogContext { + metadataRefreshType: MetadataRefreshType; + bookIds?: number[]; + libraryId?: number; +} + @Injectable({providedIn: 'root'}) export class BookDialogHelperService { @@ -75,12 +81,20 @@ export class BookDialogHelperService { } openMetadataRefreshDialog(bookIds: Set): DynamicDialogRef | null { + return this.openMetadataRefreshDialogWithContext({ + metadataRefreshType: MetadataRefreshType.BOOKS, + bookIds: Array.from(bookIds) + }); + } + + openMetadataRefreshDialogWithContext(context: MetadataRefreshDialogContext): DynamicDialogRef | null { return this.openDialog(MultiBookMetadataFetchComponent, { showHeader: false, styleClass: `${DialogSize.FULL} ${DialogStyle.MINIMAL}`, data: { - bookIds: Array.from(bookIds), - metadataRefreshType: MetadataRefreshType.BOOKS, + bookIds: context.bookIds ?? [], + libraryId: context.libraryId, + metadataRefreshType: context.metadataRefreshType, }, }); } diff --git a/frontend/src/app/features/book/service/library-shelf-menu.service.spec.ts b/frontend/src/app/features/book/service/library-shelf-menu.service.spec.ts index 93d316419..e5d9de490 100644 --- a/frontend/src/app/features/book/service/library-shelf-menu.service.spec.ts +++ b/frontend/src/app/features/book/service/library-shelf-menu.service.spec.ts @@ -88,7 +88,6 @@ describe('LibraryShelfMenuService', () => { }; const dialogLauncherService = { openLibraryEditDialog: vi.fn(), - openLibraryMetadataFetchDialog: vi.fn(), openShelfEditDialog: vi.fn(), openMagicShelfEditDialog: vi.fn(), }; @@ -103,6 +102,7 @@ describe('LibraryShelfMenuService', () => { openAddPhysicalBookDialog: vi.fn(), openBulkIsbnImportDialog: vi.fn(), openDuplicateMergerDialog: vi.fn(), + openMetadataRefreshDialogWithContext: vi.fn(), }; const translocoService = { translate: vi.fn((key: string) => key), @@ -143,7 +143,6 @@ describe('LibraryShelfMenuService', () => { userService.getCurrentUser.mockClear(); router.navigate.mockClear(); dialogLauncherService.openLibraryEditDialog.mockClear(); - dialogLauncherService.openLibraryMetadataFetchDialog.mockClear(); dialogLauncherService.openShelfEditDialog.mockClear(); dialogLauncherService.openMagicShelfEditDialog.mockClear(); magicShelfService.deleteShelf.mockClear(); @@ -152,6 +151,7 @@ describe('LibraryShelfMenuService', () => { bookDialogHelperService.openAddPhysicalBookDialog.mockClear(); bookDialogHelperService.openBulkIsbnImportDialog.mockClear(); bookDialogHelperService.openDuplicateMergerDialog.mockClear(); + bookDialogHelperService.openMetadataRefreshDialogWithContext.mockClear(); translocoService.translate.mockClear(); }); @@ -179,7 +179,7 @@ describe('LibraryShelfMenuService', () => { expect(bookDialogHelperService.openBulkIsbnImportDialog).not.toHaveBeenCalled(); expect(bookDialogHelperService.openDuplicateMergerDialog).not.toHaveBeenCalled(); expect(dialogLauncherService.openLibraryEditDialog).not.toHaveBeenCalled(); - expect(dialogLauncherService.openLibraryMetadataFetchDialog).not.toHaveBeenCalled(); + expect(bookDialogHelperService.openMetadataRefreshDialogWithContext).not.toHaveBeenCalled(); expect(taskHelperService.refreshMetadataTask).not.toHaveBeenCalled(); expect(confirmationService.confirm).not.toHaveBeenCalled(); }); @@ -199,7 +199,10 @@ describe('LibraryShelfMenuService', () => { expect(bookDialogHelperService.openAddPhysicalBookDialog).toHaveBeenCalledWith(42); expect(bookDialogHelperService.openBulkIsbnImportDialog).toHaveBeenCalledWith(42); expect(dialogLauncherService.openLibraryEditDialog).toHaveBeenCalledWith(42); - expect(dialogLauncherService.openLibraryMetadataFetchDialog).toHaveBeenCalledWith(42); + expect(bookDialogHelperService.openMetadataRefreshDialogWithContext).toHaveBeenCalledWith({ + metadataRefreshType: MetadataRefreshType.LIBRARY, + libraryId: 42 + }); expect(taskHelperService.refreshMetadataTask).toHaveBeenCalledWith({ refreshType: MetadataRefreshType.LIBRARY, libraryId: 42, diff --git a/frontend/src/app/features/book/service/library-shelf-menu.service.ts b/frontend/src/app/features/book/service/library-shelf-menu.service.ts index 2855e1ffe..eeb5c9ca1 100644 --- a/frontend/src/app/features/book/service/library-shelf-menu.service.ts +++ b/frontend/src/app/features/book/service/library-shelf-menu.service.ts @@ -36,177 +36,176 @@ export class LibraryShelfMenuService { initializeLibraryMenuItems(entity: Library | null): MenuItem[] { const libraryId = entity?.id; - const disableLibraryActions = libraryId == null; return [ { - label: this.t.translate('book.shelfMenuService.library.addPhysicalBook'), - icon: 'pi pi-book', - disabled: disableLibraryActions, - command: () => { - if (libraryId == null) { - return; - } - - this.bookDialogHelperService.openAddPhysicalBookDialog(libraryId); - } - }, - { - label: this.t.translate('book.shelfMenuService.library.bulkIsbnImport'), - icon: 'pi pi-barcode', - disabled: disableLibraryActions, - command: () => { - if (libraryId == null) { - return; - } - - this.bookDialogHelperService.openBulkIsbnImportDialog(libraryId); - } - }, - { - separator: true - }, - { - label: this.t.translate('book.shelfMenuService.library.editLibrary'), - icon: 'pi pi-pen-to-square', - disabled: disableLibraryActions, - command: () => { - if (libraryId == null) { - return; - } - - this.dialogLauncherService.openLibraryEditDialog(libraryId); - } - }, - { - label: this.t.translate('book.shelfMenuService.library.rescanLibrary'), - icon: 'pi pi-refresh', - disabled: disableLibraryActions, - command: () => { - if (libraryId == null) { - return; - } - - this.confirmationService.confirm({ - message: this.t.translate('book.shelfMenuService.confirm.rescanLibraryMessage', {name: entity?.name}), - header: this.t.translate('book.shelfMenuService.confirm.header'), - icon: undefined, - acceptLabel: this.t.translate('book.shelfMenuService.confirm.rescanLabel'), - rejectLabel: this.t.translate('common.cancel'), - acceptIcon: undefined, - rejectIcon: undefined, - acceptButtonStyleClass: undefined, - rejectButtonStyleClass: undefined, - rejectButtonProps: { - label: this.t.translate('common.cancel'), - severity: 'secondary', - }, - acceptButtonProps: { - label: this.t.translate('book.shelfMenuService.confirm.rescanLabel'), - severity: 'success', - }, - accept: () => { - this.libraryService.refreshLibrary(libraryId).subscribe({ - complete: () => { - this.messageService.add({severity: 'info', summary: this.t.translate('common.success'), detail: this.t.translate('book.shelfMenuService.toast.libraryRefreshSuccessDetail')}); + label: this.t.translate('book.shelfMenuService.library.optionsLabel'), + items: [ + { + label: this.t.translate('book.shelfMenuService.library.addPhysicalBook'), + icon: 'pi pi-book', + disabled: libraryId == null, + command: () => { + if (libraryId == null) { + return; + } + this.bookDialogHelperService.openAddPhysicalBookDialog(libraryId); + } + }, + { + label: this.t.translate('book.shelfMenuService.library.bulkIsbnImport'), + icon: 'pi pi-barcode', + disabled: libraryId == null, + command: () => { + if (libraryId == null) { + return; + } + this.bookDialogHelperService.openBulkIsbnImportDialog(libraryId); + } + }, + { + separator: true + }, + { + label: this.t.translate('book.shelfMenuService.library.editLibrary'), + icon: 'pi pi-pen-to-square', + disabled: libraryId == null, + command: () => { + if (libraryId == null) { + return; + } + this.dialogLauncherService.openLibraryEditDialog(libraryId); + } + }, + { + label: this.t.translate('book.shelfMenuService.library.rescanLibrary'), + icon: 'pi pi-refresh', + disabled: libraryId == null, + command: () => { + if (libraryId == null) { + return; + } + this.confirmationService.confirm({ + message: this.t.translate('book.shelfMenuService.confirm.rescanLibraryMessage', {name: entity?.name}), + header: this.t.translate('book.shelfMenuService.confirm.header'), + icon: undefined, + acceptLabel: this.t.translate('book.shelfMenuService.confirm.rescanLabel'), + rejectLabel: this.t.translate('common.cancel'), + acceptIcon: undefined, + rejectIcon: undefined, + acceptButtonStyleClass: undefined, + rejectButtonStyleClass: undefined, + rejectButtonProps: { + label: this.t.translate('common.cancel'), + severity: 'secondary', }, - error: () => { - this.messageService.add({ - severity: 'error', - summary: this.t.translate('book.shelfMenuService.toast.failedSummary'), - detail: this.t.translate('book.shelfMenuService.toast.libraryRefreshFailedDetail'), + acceptButtonProps: { + label: this.t.translate('book.shelfMenuService.confirm.rescanLabel'), + severity: 'success', + }, + accept: () => { + this.libraryService.refreshLibrary(libraryId).subscribe({ + complete: () => { + this.messageService.add({severity: 'info', summary: this.t.translate('common.success'), detail: this.t.translate('book.shelfMenuService.toast.libraryRefreshSuccessDetail')}); + }, + error: () => { + this.messageService.add({ + severity: 'error', + summary: this.t.translate('book.shelfMenuService.toast.failedSummary'), + detail: this.t.translate('book.shelfMenuService.toast.libraryRefreshFailedDetail'), + }); + } }); } }); } - }); - } - }, - { - label: this.t.translate('book.shelfMenuService.library.customFetchMetadata'), - icon: 'pi pi-sync', - disabled: disableLibraryActions, - command: () => { - if (libraryId == null) { - return; - } - - this.dialogLauncherService.openLibraryMetadataFetchDialog(libraryId); - } - }, - { - label: this.t.translate('book.shelfMenuService.library.autoFetchMetadata'), - icon: 'pi pi-bolt', - disabled: disableLibraryActions, - command: () => { - if (libraryId == null) { - return; - } - - this.taskHelperService.refreshMetadataTask({ - refreshType: MetadataRefreshType.LIBRARY, - libraryId - }).subscribe(); - } - }, - { - label: this.t.translate('book.shelfMenuService.library.findDuplicates'), - icon: 'pi pi-copy', - disabled: disableLibraryActions, - command: () => { - if (libraryId == null) { - return; - } - - this.bookDialogHelperService.openDuplicateMergerDialog(libraryId); - } - }, - { - separator: true - }, - { - label: this.t.translate('book.shelfMenuService.library.deleteLibrary'), - icon: 'pi pi-trash', - disabled: disableLibraryActions, - command: () => { - if (libraryId == null) { - return; - } - - this.confirmationService.confirm({ - message: this.t.translate('book.shelfMenuService.confirm.deleteLibraryMessage', {name: entity?.name}), - header: this.t.translate('book.shelfMenuService.confirm.header'), - acceptLabel: this.t.translate('common.yes'), - rejectLabel: this.t.translate('common.cancel'), - rejectButtonProps: { - label: this.t.translate('common.cancel'), - severity: 'secondary', - }, - acceptButtonProps: { - label: this.t.translate('common.yes'), - severity: 'danger', - }, - accept: () => { - const loader = this.loadingService.show(this.t.translate('book.shelfMenuService.loading.deletingLibrary', {name: entity?.name})); - - this.libraryService.deleteLibrary(libraryId) - .pipe(finalize(() => this.loadingService.hide(loader))) - .subscribe({ - complete: () => { - this.router.navigate(['/']); - this.messageService.add({severity: 'info', summary: this.t.translate('common.success'), detail: this.t.translate('book.shelfMenuService.toast.libraryDeletedDetail')}); - }, - error: () => { - this.messageService.add({ - severity: 'error', - summary: this.t.translate('book.shelfMenuService.toast.failedSummary'), - detail: this.t.translate('book.shelfMenuService.toast.libraryDeleteFailedDetail'), - }); - } - }); + }, + { + label: this.t.translate('book.shelfMenuService.library.customFetchMetadata'), + icon: 'pi pi-sync', + disabled: libraryId == null, + command: () => { + if (libraryId == null) { + return; + } + this.bookDialogHelperService.openMetadataRefreshDialogWithContext({ + metadataRefreshType: MetadataRefreshType.LIBRARY, + libraryId + }); } - }); - } + }, + { + label: this.t.translate('book.shelfMenuService.library.autoFetchMetadata'), + icon: 'pi pi-bolt', + disabled: libraryId == null, + command: () => { + if (libraryId == null) { + return; + } + this.taskHelperService.refreshMetadataTask({ + refreshType: MetadataRefreshType.LIBRARY, + libraryId + }).subscribe(); + } + }, + { + label: this.t.translate('book.shelfMenuService.library.findDuplicates'), + icon: 'pi pi-copy', + disabled: libraryId == null, + command: () => { + if (libraryId == null) { + return; + } + this.bookDialogHelperService.openDuplicateMergerDialog(libraryId); + } + }, + { + separator: true + }, + { + label: this.t.translate('book.shelfMenuService.library.deleteLibrary'), + icon: 'pi pi-trash', + disabled: libraryId == null, + command: () => { + if (libraryId == null) { + return; + } + this.confirmationService.confirm({ + message: this.t.translate('book.shelfMenuService.confirm.deleteLibraryMessage', {name: entity?.name}), + header: this.t.translate('book.shelfMenuService.confirm.header'), + acceptLabel: this.t.translate('common.yes'), + rejectLabel: this.t.translate('common.cancel'), + rejectButtonProps: { + label: this.t.translate('common.cancel'), + severity: 'secondary', + }, + acceptButtonProps: { + label: this.t.translate('common.yes'), + severity: 'danger', + }, + accept: () => { + const loader = this.loadingService.show(this.t.translate('book.shelfMenuService.loading.deletingLibrary', {name: entity?.name})); + + this.libraryService.deleteLibrary(libraryId) + .pipe(finalize(() => this.loadingService.hide(loader))) + .subscribe({ + complete: () => { + this.router.navigate(['/']); + this.messageService.add({severity: 'info', summary: this.t.translate('common.success'), detail: this.t.translate('book.shelfMenuService.toast.libraryDeletedDetail')}); + }, + error: () => { + this.messageService.add({ + severity: 'error', + summary: this.t.translate('book.shelfMenuService.toast.failedSummary'), + detail: this.t.translate('book.shelfMenuService.toast.libraryDeleteFailedDetail'), + }); + } + }); + } + }); + } + } + ] } ]; } diff --git a/frontend/src/app/features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component.spec.ts b/frontend/src/app/features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component.spec.ts index c709b02b3..b6be2f640 100644 --- a/frontend/src/app/features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component.spec.ts +++ b/frontend/src/app/features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component.spec.ts @@ -52,6 +52,7 @@ describe('MetadataFetchOptionsComponent', () => { }); const component = TestBed.runInInjectionContext(() => new MetadataFetchOptionsComponent()); + component.ngOnInit(); expect(component.libraryId).toBe(7); expect(component.bookIds).toEqual([1, 2]); @@ -59,6 +60,38 @@ describe('MetadataFetchOptionsComponent', () => { expect(component.currentMetadataOptions).toEqual(defaultMetadataRefreshOptions); }); + it('gives precedence to dialogData Input over dynamicDialogConfig.data', () => { + TestBed.configureTestingModule({ + providers: [ + { + provide: DynamicDialogConfig, + useValue: { + data: { + libraryId: 7, + bookIds: [1, 2], + metadataRefreshType: MetadataRefreshType.LIBRARY, + }, + }, + }, + {provide: DynamicDialogRef, useValue: {close}}, + {provide: TaskHelperService, useValue: {refreshMetadataTask}}, + {provide: AppSettingsService, useValue: {appSettings: () => null}}, + ] + }); + + const component = TestBed.runInInjectionContext(() => new MetadataFetchOptionsComponent()); + component.dialogData = { + libraryId: 9, + bookIds: [10], + metadataRefreshType: MetadataRefreshType.BOOKS, + }; + component.ngOnInit(); + + expect(component.libraryId).toBe(9); + expect(component.bookIds).toEqual([10]); + expect(component.metadataRefreshType).toBe(MetadataRefreshType.BOOKS); + }); + it('submits a metadata refresh task and closes the dialog', () => { TestBed.configureTestingModule({ providers: [ @@ -79,6 +112,7 @@ describe('MetadataFetchOptionsComponent', () => { }); const component = TestBed.runInInjectionContext(() => new MetadataFetchOptionsComponent()); + component.ngOnInit(); const options: MetadataRefreshOptions = { libraryId: 7, refreshCovers: false, diff --git a/frontend/src/app/features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component.ts b/frontend/src/app/features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component.ts index e8b5acd7c..edf4d7a36 100644 --- a/frontend/src/app/features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component.ts +++ b/frontend/src/app/features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component.ts @@ -1,4 +1,4 @@ -import {Component, inject} from '@angular/core'; +import {Component, inject, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core'; import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog'; import {MetadataRefreshRequest} from '../../../model/request/metadata-refresh-request.model'; import {MetadataRefreshType} from '../../../model/request/metadata-refresh-type.enum'; @@ -18,10 +18,16 @@ import {TranslocoDirective} from '@jsverse/transloco'; ], styleUrl: './metadata-fetch-options.component.scss' }) -export class MetadataFetchOptionsComponent { - libraryId!: number; - bookIds!: number[]; - metadataRefreshType!: MetadataRefreshType; +export class MetadataFetchOptionsComponent implements OnInit, OnChanges { + @Input() dialogData?: { + libraryId?: number | null; + bookIds?: number[]; + metadataRefreshType?: MetadataRefreshType; + }; + + libraryId?: number; + bookIds: number[] = []; + metadataRefreshType: MetadataRefreshType = MetadataRefreshType.BOOKS; currentMetadataOptions!: MetadataRefreshOptions; private dynamicDialogConfig = inject(DynamicDialogConfig); @@ -30,15 +36,22 @@ export class MetadataFetchOptionsComponent { private appSettingsService = inject(AppSettingsService); constructor() { - this.libraryId = this.dynamicDialogConfig.data.libraryId; - this.bookIds = this.dynamicDialogConfig.data.bookIds; - this.metadataRefreshType = this.dynamicDialogConfig.data.metadataRefreshType; const settings = this.appSettingsService.appSettings(); if (settings) { this.currentMetadataOptions = settings.defaultMetadataRefreshOptions; } } + ngOnInit(): void { + this.applyContext(this.dialogData ?? this.dynamicDialogConfig.data ?? {}); + } + + ngOnChanges(changes: SimpleChanges): void { + if ('dialogData' in changes) { + this.applyContext(changes['dialogData'].currentValue ?? {}); + } + } + onMetadataSubmit(metadataRefreshOptions: MetadataRefreshOptions) { const metadataRefreshRequest: MetadataRefreshRequest = { refreshType: this.metadataRefreshType, @@ -49,4 +62,14 @@ export class MetadataFetchOptionsComponent { this.taskHelperService.refreshMetadataTask(metadataRefreshRequest).subscribe(); this.dynamicDialogRef.close(); } + + private applyContext(context: { + libraryId?: number | null; + bookIds?: number[]; + metadataRefreshType?: MetadataRefreshType; + }): void { + this.libraryId = context.libraryId ?? undefined; + this.bookIds = context.bookIds ?? []; + this.metadataRefreshType = context.metadataRefreshType ?? MetadataRefreshType.BOOKS; + } } diff --git a/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.html b/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.html index 714bba5d5..a089619f9 100644 --- a/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.html +++ b/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.html @@ -5,7 +5,11 @@

Metadata Refresh Options

-

{{ booksToShow.length }} books selected

+ @if (isLibraryRefresh) { +

Library-level custom metadata fetch

+ } @else { +

{{ booksToShow.length }} books selected

+ }
-
-
-
    - @for (book of booksToShow; track book.id) { -
  • - #{{ book.id }}: {{ book.metadata?.title || 'Untitled' }} -
  • - } -
+ @if (!isLibraryRefresh) { +
+
+
    + @for (book of booksToShow; track book.id) { +
  • + #{{ book.id }}: {{ book.metadata?.title || 'Untitled' }} +
  • + } +
+
-
+ } - +
diff --git a/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.spec.ts b/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.spec.ts index 0d799db6b..3c00efab4 100644 --- a/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.spec.ts +++ b/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.spec.ts @@ -43,6 +43,7 @@ describe('MultiBookMetadataFetchComponent', () => { it('reads dialog data and resolves the books to show on construction', () => { const component = TestBed.runInInjectionContext(() => new MultiBookMetadataFetchComponent()); + component.ngOnInit(); expect(component.bookIds).toEqual([3, 5]); expect(component.metadataRefreshType).toBe(MetadataRefreshType.BOOKS); @@ -53,8 +54,21 @@ describe('MultiBookMetadataFetchComponent', () => { ]); }); + it('gives precedence to dialogData Input over dynamicDialogConfig.data', () => { + const component = TestBed.runInInjectionContext(() => new MultiBookMetadataFetchComponent()); + component.dialogData = { + bookIds: [10], + metadataRefreshType: MetadataRefreshType.BOOKS, + }; + component.ngOnInit(); + + expect(component.bookIds).toEqual([10]); + expect(getBooksByIds).toHaveBeenCalledWith([10]); + }); + it('adopts the default metadata refresh options when app settings become available', () => { const component = TestBed.runInInjectionContext(() => new MultiBookMetadataFetchComponent()); + component.ngOnInit(); expect(component.currentMetadataOptions).toBeUndefined(); diff --git a/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.ts b/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.ts index 4d326fb1c..7b442c852 100644 --- a/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.ts +++ b/frontend/src/app/features/metadata/component/multi-book-metadata-fetch/multi-book-metadata-fetch-component.ts @@ -1,4 +1,4 @@ -import {Component, effect, inject} from '@angular/core'; +import {Component, effect, inject, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core'; import {MetadataRefreshType} from '../../model/request/metadata-refresh-type.enum'; import {MetadataRefreshOptions} from '../../model/request/metadata-refresh-options.model'; @@ -18,10 +18,17 @@ import {Button} from 'primeng/button'; styleUrl: './multi-book-metadata-fetch-component.scss', imports: [MetadataFetchOptionsComponent, FormsModule, Button], }) -export class MultiBookMetadataFetchComponent { - bookIds!: number[]; +export class MultiBookMetadataFetchComponent implements OnInit, OnChanges { + @Input() dialogData?: { + libraryId?: number | null; + bookIds?: number[]; + metadataRefreshType?: MetadataRefreshType; + }; + + bookIds: number[] = []; + libraryId: number | null = null; booksToShow: Book[] = []; - metadataRefreshType!: MetadataRefreshType; + metadataRefreshType: MetadataRefreshType = MetadataRefreshType.BOOKS; currentMetadataOptions!: MetadataRefreshOptions; private dynamicDialogConfig = inject(DynamicDialogConfig); @@ -31,10 +38,6 @@ export class MultiBookMetadataFetchComponent { expanded = false; constructor() { - this.bookIds = this.dynamicDialogConfig.data.bookIds; - this.metadataRefreshType = this.dynamicDialogConfig.data.metadataRefreshType; - this.booksToShow = this.bookService.getBooksByIds(this.bookIds); - effect(() => { const settings = this.appSettingsService.appSettings(); if (settings) { @@ -42,4 +45,29 @@ export class MultiBookMetadataFetchComponent { } }); } + + ngOnInit(): void { + this.applyContext(this.dialogData ?? this.dynamicDialogConfig.data ?? {}); + } + + ngOnChanges(changes: SimpleChanges): void { + if ('dialogData' in changes) { + this.applyContext(changes['dialogData'].currentValue ?? {}); + } + } + + private applyContext(context: { + libraryId?: number | null; + bookIds?: number[]; + metadataRefreshType?: MetadataRefreshType; + }): void { + this.bookIds = context.bookIds ?? []; + this.libraryId = context.libraryId ?? null; + this.metadataRefreshType = context.metadataRefreshType ?? MetadataRefreshType.BOOKS; + this.booksToShow = this.bookService.getBooksByIds(this.bookIds); + } + + get isLibraryRefresh(): boolean { + return this.metadataRefreshType === MetadataRefreshType.LIBRARY; + } } diff --git a/frontend/src/app/shared/services/dialog-launcher.service.ts b/frontend/src/app/shared/services/dialog-launcher.service.ts index 048300f30..899c4fbb2 100644 --- a/frontend/src/app/shared/services/dialog-launcher.service.ts +++ b/frontend/src/app/shared/services/dialog-launcher.service.ts @@ -13,8 +13,6 @@ import {DirectoryPickerComponent} from '../components/directory-picker/directory import {BookdropFinalizeResultDialogComponent} from '../../features/bookdrop/component/bookdrop-finalize-result-dialog/bookdrop-finalize-result-dialog.component'; import {BookdropFinalizeResult} from '../../features/bookdrop/service/bookdrop.service'; import {MetadataReviewDialogComponent} from '../../features/metadata/component/metadata-review-dialog/metadata-review-dialog-component'; -import {MetadataRefreshType} from '../../features/metadata/model/request/metadata-refresh-type.enum'; -import {MetadataFetchOptionsComponent} from '../../features/metadata/component/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component'; import {ShelfEditDialogComponent} from '../../features/book/components/shelf-edit-dialog/shelf-edit-dialog.component'; import {IconPickerComponent} from '../components/icon-picker/icon-picker-component'; @@ -94,17 +92,6 @@ export class DialogLauncherService { }); } - openLibraryMetadataFetchDialog(libraryId: number): DynamicDialogRef | null { - return this.openDialog(MetadataFetchOptionsComponent, { - showHeader: false, - styleClass: `${DialogSize.SM} ${DialogStyle.MINIMAL}`, - data: { - libraryId: libraryId, - metadataRefreshType: MetadataRefreshType.LIBRARY, - }, - }); - } - openShelfEditDialog(shelfId: number): DynamicDialogRef | null { return this.openDialog(ShelfEditDialogComponent, { showHeader: false,