perf(ui): load books by ID instead of the full book list (#2703)
This commit is contained in:
+1
-17
@@ -6,7 +6,6 @@ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {ConfirmationService, MessageService} from '@openng/optimus-ui/api';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {BookMetadataManageService} from '../../service/book-metadata-manage.service';
|
||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
@@ -44,9 +43,6 @@ describe('BookReviewsComponent', () => {
|
||||
delete: ReturnType<typeof vi.fn>;
|
||||
deleteAllByBookId: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
let bookService: {
|
||||
findBookById: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
let bookMetadataManageService: {
|
||||
toggleFieldLocks: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
@@ -77,9 +73,6 @@ describe('BookReviewsComponent', () => {
|
||||
delete: vi.fn(),
|
||||
deleteAllByBookId: vi.fn(),
|
||||
};
|
||||
bookService = {
|
||||
findBookById: vi.fn(),
|
||||
};
|
||||
bookMetadataManageService = {
|
||||
toggleFieldLocks: vi.fn(),
|
||||
};
|
||||
@@ -113,7 +106,6 @@ describe('BookReviewsComponent', () => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
{provide: BookReviewService, useValue: reviewService},
|
||||
{provide: BookService, useValue: bookService},
|
||||
{provide: BookMetadataManageService, useValue: bookMetadataManageService},
|
||||
{provide: ConfirmationService, useValue: confirmationService},
|
||||
{provide: MessageService, useValue: messageService},
|
||||
@@ -132,26 +124,18 @@ describe('BookReviewsComponent', () => {
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
it('loads reviews on book changes, sorts them by date, and mirrors the current lock state', () => {
|
||||
it('loads reviews on book changes and sorts them by date', () => {
|
||||
const undated = createReview(1, {date: undefined});
|
||||
const oldest = createReview(2, {date: '2026-03-02T12:00:00.000Z'});
|
||||
const newest = createReview(3, {date: '2026-03-03T12:00:00.000Z'});
|
||||
reviewService.getByBookId.mockReturnValue(of([oldest, undated, newest]));
|
||||
bookService.findBookById.mockReturnValue({
|
||||
metadata: {
|
||||
reviewsLocked: true,
|
||||
},
|
||||
});
|
||||
|
||||
component.ngOnChanges({
|
||||
bookId: new SimpleChange(undefined, 42, true),
|
||||
});
|
||||
TestBed.flushEffects();
|
||||
|
||||
expect(bookService.findBookById).toHaveBeenCalledWith(42);
|
||||
expect(reviewService.getByBookId).toHaveBeenCalledWith(42);
|
||||
expect(component.reviews?.map(review => review.id)).toEqual([3, 2, 1]);
|
||||
expect(component.reviewsLocked).toBe(true);
|
||||
expect(component.loading()).toBe(false);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {Component, DestroyRef, effect, inject, Input, OnChanges, OnInit, signal, SimpleChanges} from '@angular/core';
|
||||
import {Component, DestroyRef, inject, Input, OnChanges, OnInit, signal, SimpleChanges} from '@angular/core';
|
||||
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {finalize} from 'rxjs';
|
||||
@@ -13,7 +13,6 @@ import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/tran
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Tooltip} from '@openng/optimus-ui/tooltip';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {BookMetadataManageService} from '../../service/book-metadata-manage.service';
|
||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||
import {MetadataCatalogService} from '../../../../shared/metadata/metadata-catalog.service';
|
||||
@@ -29,9 +28,9 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
@Input() bookId!: number;
|
||||
@Input() reviews: BookReview[] | undefined = [];
|
||||
@Input() active: boolean = false;
|
||||
@Input() reviewsLocked = false;
|
||||
|
||||
private reviewService = inject(BookReviewService);
|
||||
private bookService = inject(BookService);
|
||||
private bookMetadataManageService = inject(BookMetadataManageService);
|
||||
private confirmationService = inject(ConfirmationService);
|
||||
private messageService = inject(MessageService);
|
||||
@@ -40,7 +39,6 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
private destroyRef = inject(DestroyRef);
|
||||
private readonly t = inject(TranslocoService);
|
||||
protected readonly catalog = inject(MetadataCatalogService);
|
||||
private bookIdState = signal<number | null>(null);
|
||||
private loadingBookId: number | null = null;
|
||||
private loadingRequestSeq = 0;
|
||||
private activeLoadingRequestSeq: number | null = null;
|
||||
@@ -51,23 +49,9 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
hasPermission = false;
|
||||
revealedSpoilers = new Set<number>();
|
||||
sortAscending = false;
|
||||
reviewsLocked = false;
|
||||
allSpoilersRevealed = false;
|
||||
reviewDownloadEnabled = true;
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
const bookId = this.bookIdState();
|
||||
if (!bookId) {
|
||||
this.reviewsLocked = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const book = this.bookService.findBookById(bookId);
|
||||
this.reviewsLocked = book?.metadata?.reviewsLocked ?? false;
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.checkUserPermissions();
|
||||
this.subscribeToAppSettings();
|
||||
@@ -75,7 +59,6 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
|
||||
ngOnChanges(changes: SimpleChanges): void {
|
||||
if (changes['bookId'] && changes['bookId'].currentValue) {
|
||||
this.bookIdState.set(changes['bookId'].currentValue);
|
||||
this.loadReviews();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@
|
||||
</p-tabpanel>
|
||||
<p-tabpanel value="reviews">
|
||||
<ng-template #content>
|
||||
<app-book-reviews [bookId]="currentBook.id"></app-book-reviews>
|
||||
<app-book-reviews [bookId]="currentBook.id" [reviewsLocked]="currentBook.metadata?.reviewsLocked ?? false"></app-book-reviews>
|
||||
</ng-template>
|
||||
</p-tabpanel>
|
||||
<p-tabpanel value="chapters">
|
||||
|
||||
@@ -41,13 +41,11 @@ export class CoverSearchComponent {
|
||||
private readonly t = inject(TranslocoService);
|
||||
|
||||
private readonly bookId: number = this.dynamicDialogConfig.data.bookId;
|
||||
private readonly book = this.bookService.findBookById(this.bookId);
|
||||
readonly searchForm = this.fb.nonNullable.group({
|
||||
title: ['', Validators.required],
|
||||
author: ['']
|
||||
});
|
||||
readonly coverType: 'ebook' | 'audiobook' = this.dynamicDialogConfig.data.coverType ??
|
||||
(this.book?.primaryFile?.bookType === 'AUDIOBOOK' ? 'audiobook' : 'ebook');
|
||||
coverType: 'ebook' | 'audiobook' = this.dynamicDialogConfig.data.coverType ?? 'ebook';
|
||||
private readonly search = signal<CoverSearchParams | null>(null);
|
||||
private readonly query = injectQuery(() => ({
|
||||
...this.sources.coverSearch(this.search() ?? NO_SEARCH),
|
||||
@@ -60,16 +58,20 @@ export class CoverSearchComponent {
|
||||
readonly hasSearched = computed(() => this.search() !== null);
|
||||
|
||||
constructor() {
|
||||
if (this.book) {
|
||||
const explicitCoverType = this.dynamicDialogConfig.data.coverType;
|
||||
this.bookService.ensureBookDetail(this.bookId, true).then(book => {
|
||||
if (!explicitCoverType && book.primaryFile?.bookType === 'AUDIOBOOK') {
|
||||
this.coverType = 'audiobook';
|
||||
}
|
||||
this.searchForm.patchValue({
|
||||
title: this.book.metadata?.title || '',
|
||||
author: this.book.metadata?.authors?.[0] ?? ''
|
||||
title: book.metadata?.title || '',
|
||||
author: book.metadata?.authors?.[0] ?? ''
|
||||
});
|
||||
|
||||
if (this.searchForm.valid) {
|
||||
this.onSearch();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onSearch() {
|
||||
|
||||
+12
-9
@@ -1,13 +1,14 @@
|
||||
import {signal} from '@angular/core';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {of, throwError} from 'rxjs';
|
||||
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {DynamicDialogConfig, DynamicDialogRef} from '@openng/optimus-ui/dynamicdialog';
|
||||
import {queryOptions} from '@tanstack/angular-query-experimental';
|
||||
|
||||
import {BookService} from '../../../book/service/book.service';
|
||||
import {FetchedMetadataProposalStatus, FetchedProposal, MetadataTaskService} from '../../../book/service/metadata-task';
|
||||
import {BookMetadata} from '../../../book/model/book.model';
|
||||
import {Book, BookMetadata} from '../../../book/model/book.model';
|
||||
import {MetadataProgressService} from '../../../../shared/service/metadata-progress.service';
|
||||
import {createQueryClientHarness, flushQueryAsync} from '../../../../core/testing/query-testing';
|
||||
import {MetadataPickerComponent} from '../book-metadata-center/metadata-picker/metadata-picker.component';
|
||||
import {MetadataReviewDialogComponent} from './metadata-review-dialog-component';
|
||||
|
||||
@@ -17,8 +18,10 @@ describe('MetadataReviewDialogComponent', () => {
|
||||
const updateProposalStatus = vi.fn(() => of(void 0));
|
||||
const deleteTask = vi.fn(() => of(void 0));
|
||||
const clearTask = vi.fn();
|
||||
const books = signal([{id: 11, title: 'Book 11'}]);
|
||||
const findBookById = vi.fn((bookId: number) => books().find(book => book.id === bookId));
|
||||
const bookDetailQueryOptions = (bookId: number) => queryOptions({
|
||||
queryKey: ['books', 'detail', bookId, true],
|
||||
queryFn: async (): Promise<Book> => ({id: bookId} as Book),
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
close.mockClear();
|
||||
@@ -26,7 +29,6 @@ describe('MetadataReviewDialogComponent', () => {
|
||||
updateProposalStatus.mockClear();
|
||||
deleteTask.mockClear();
|
||||
clearTask.mockClear();
|
||||
findBookById.mockClear();
|
||||
});
|
||||
|
||||
function createProposal(overrides: Partial<FetchedProposal>): FetchedProposal {
|
||||
@@ -54,10 +56,11 @@ describe('MetadataReviewDialogComponent', () => {
|
||||
function createComponent(taskId: string | undefined) {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
...createQueryClientHarness().providers,
|
||||
{provide: DynamicDialogConfig, useValue: {data: taskId ? {taskId} : {}}},
|
||||
{provide: DynamicDialogRef, useValue: {close}},
|
||||
{provide: MetadataTaskService, useValue: {getTaskWithProposals, updateProposalStatus, deleteTask}},
|
||||
{provide: BookService, useValue: {books, findBookById}},
|
||||
{provide: BookService, useValue: {bookDetailQueryOptions}},
|
||||
{provide: MetadataProgressService, useValue: {clearTask}},
|
||||
]
|
||||
});
|
||||
@@ -73,7 +76,7 @@ describe('MetadataReviewDialogComponent', () => {
|
||||
expect(close).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('loads proposals for the requested task and exposes the current book', () => {
|
||||
it('loads proposals for the requested task and exposes the current book', async () => {
|
||||
getTaskWithProposals.mockReturnValue(of({
|
||||
proposals: [
|
||||
createProposal({proposalId: 5}),
|
||||
@@ -82,11 +85,11 @@ describe('MetadataReviewDialogComponent', () => {
|
||||
|
||||
const component = createComponent('task-1');
|
||||
component.ngOnInit();
|
||||
TestBed.flushEffects();
|
||||
await flushQueryAsync();
|
||||
|
||||
expect(getTaskWithProposals).toHaveBeenCalledWith('task-1');
|
||||
expect(component.currentProposal).toEqual(createProposal({proposalId: 5}));
|
||||
expect(component.currentBook()).toEqual({id: 11, title: 'Book 11'});
|
||||
expect(component.currentBook()).toEqual({id: 11});
|
||||
expect(component.loading()).toBe(false);
|
||||
});
|
||||
|
||||
|
||||
+14
-18
@@ -1,8 +1,7 @@
|
||||
import {Component, computed, effect, inject, OnInit, signal, ViewChild} from '@angular/core';
|
||||
import {Component, computed, effect, inject, OnInit, signal, untracked, ViewChild} from '@angular/core';
|
||||
import {DynamicDialogConfig, DynamicDialogRef} from '@openng/optimus-ui/dynamicdialog';
|
||||
import {FetchedProposal, MetadataTaskService} from '../../../book/service/metadata-task';
|
||||
import {BookService} from '../../../book/service/book.service';
|
||||
import {Book} from '../../../book/model/book.model';
|
||||
import {ProgressSpinner} from '@openng/optimus-ui/progressspinner';
|
||||
import {Button} from '@openng/optimus-ui/button';
|
||||
import {Divider} from '@openng/optimus-ui/divider';
|
||||
@@ -11,6 +10,8 @@ import {Tooltip} from '@openng/optimus-ui/tooltip';
|
||||
import {MetadataProgressService} from '../../../../shared/service/metadata-progress.service';
|
||||
import {MetadataPickerComponent} from '../book-metadata-center/metadata-picker/metadata-picker.component';
|
||||
import {DecimalPipe} from '@angular/common';
|
||||
import {injectQuery} from '@tanstack/angular-query-experimental';
|
||||
import {retryTransientQueryError} from '../../../../core/data/query-transport';
|
||||
|
||||
@Component({
|
||||
selector: 'app-metadata-review-dialog-component',
|
||||
@@ -31,28 +32,23 @@ export class MetadataReviewDialogComponent implements OnInit {
|
||||
private bookService = inject(BookService);
|
||||
private progressService = inject(MetadataProgressService);
|
||||
|
||||
loading = signal(true);
|
||||
readonly proposals = signal<FetchedProposal[]>([]);
|
||||
readonly currentIndex = signal(0);
|
||||
readonly currentBook = computed<Book | null>(() => {
|
||||
const proposal = this.proposals()[this.currentIndex()];
|
||||
if (!proposal) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.bookService.findBookById(proposal.bookId) ?? null;
|
||||
});
|
||||
private readonly currentBookId = computed(() => this.proposals()[this.currentIndex()]?.bookId ?? null);
|
||||
private readonly bookDetailQuery = injectQuery(() => ({
|
||||
...this.bookService.bookDetailQueryOptions(this.currentBookId() ?? -1, true),
|
||||
enabled: this.currentBookId() != null,
|
||||
retry: retryTransientQueryError,
|
||||
}));
|
||||
readonly currentBook = computed(() => this.bookDetailQuery.data() ?? null);
|
||||
readonly loading = computed(() => this.currentBook() === null);
|
||||
|
||||
constructor() {
|
||||
// A proposal can outlive its book, so skip any whose book cannot be loaded.
|
||||
effect(() => {
|
||||
const proposals = this.proposals();
|
||||
if (proposals.length === 0) {
|
||||
return;
|
||||
if (this.bookDetailQuery.isError()) {
|
||||
untracked(() => this.onNext());
|
||||
}
|
||||
|
||||
const bookIds = new Set(proposals.map(proposal => proposal.bookId));
|
||||
const matchedBooks = this.bookService.books().filter(book => bookIds.has(book.id));
|
||||
this.loading.set(matchedBooks.length !== bookIds.size);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
</div>
|
||||
<div class="header-text">
|
||||
<h2 class="panel-title">Edit Metadata</h2>
|
||||
<p class="panel-description">{{ filteredBooks().length }} book{{ filteredBooks().length !== 1 ? 's' : '' }} selected</p>
|
||||
<p class="panel-description">{{ bookIds.length }} book{{ bookIds.length !== 1 ? 's' : '' }} selected</p>
|
||||
</div>
|
||||
<p-button
|
||||
icon="pi pi-times"
|
||||
|
||||
+3
-6
@@ -33,11 +33,8 @@ export class MultiBookMetadataEditorComponent {
|
||||
bookIds: number[] = this.config.data?.bookIds ?? [];
|
||||
loading = false;
|
||||
|
||||
filteredBooks = computed(() => this.bookService.books().filter(book =>
|
||||
!!book.metadata && this.bookIds.includes(book.id)
|
||||
));
|
||||
private currentIndex = signal(0);
|
||||
private currentBookId = computed(() => this.filteredBooks()[this.currentIndex()]?.id ?? null);
|
||||
private currentBookId = computed(() => this.bookIds[this.currentIndex()] ?? null);
|
||||
private bookDetailQuery = injectQuery(() => ({
|
||||
...this.bookService.bookDetailQueryOptions(this.currentBookId() ?? -1, true),
|
||||
enabled: this.currentBookId() != null,
|
||||
@@ -58,7 +55,7 @@ export class MultiBookMetadataEditorComponent {
|
||||
|
||||
handleNextBook() {
|
||||
const next = this.currentIndex() + 1;
|
||||
if (next < this.filteredBooks().length) {
|
||||
if (next < this.bookIds.length) {
|
||||
this.currentIndex.set(next);
|
||||
}
|
||||
}
|
||||
@@ -75,7 +72,7 @@ export class MultiBookMetadataEditorComponent {
|
||||
}
|
||||
|
||||
get disableNext(): boolean {
|
||||
return this.currentIndex() >= this.filteredBooks().length - 1;
|
||||
return this.currentIndex() >= this.bookIds.length - 1;
|
||||
}
|
||||
|
||||
get disablePrevious(): boolean {
|
||||
|
||||
@@ -8,4 +8,3 @@ export function debouncedSignal<T>(source: Signal<T>, ms: number, injector?: Inj
|
||||
{initialValue: source(), injector},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,4 +29,3 @@ describe('normalizeLocalSearchTerm', () => {
|
||||
expect(normalizeLocalSearchTerm(' \t ')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user