fix(ui): handle author adding when books are added (#1572)

This commit is contained in:
Alex
2026-05-29 13:07:16 +01:00
committed by GitHub
parent e138648dcf
commit 49a7593e42
5 changed files with 96 additions and 3 deletions
@@ -8,3 +8,7 @@ export function patchAuthorInCache(queryClient: QueryClient, authorId: number, f
(current ?? []).map(author => author.id === authorId ? {...author, ...fields} : author)
);
}
export function invalidateAuthorsQuery(queryClient: QueryClient): void {
void queryClient.invalidateQueries({queryKey: AUTHORS_QUERY_KEY, exact: true});
}
@@ -7,8 +7,23 @@ import {of} from 'rxjs';
import {API_CONFIG} from '../../../core/config/api-config';
import {AuthService} from '../../../shared/service/auth.service';
import {Book} from '../../book/model/book.model';
import {AUTHORS_QUERY_KEY} from './author-query-keys';
import {AuthorService} from './author.service';
function makeBook(authors?: string[]): Book {
return {
id: 1,
libraryId: 1,
libraryName: 'Library',
metadata: {
bookId: 1,
title: 'Book 1',
authors,
},
};
}
describe('AuthorService', () => {
const httpClient = {
get: vi.fn(),
@@ -66,6 +81,7 @@ describe('AuthorService', () => {
});
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
TestBed.resetTestingModule();
});
@@ -104,6 +120,40 @@ describe('AuthorService', () => {
);
});
it('coalesces author query invalidation when newly created books introduce authors', () => {
vi.useFakeTimers();
service.handleNewlyCreatedBook(makeBook(['Ada Lovelace']));
service.handleNewlyCreatedBook(makeBook(['Grace Hopper']));
service.handleNewlyCreatedBook(makeBook(['Katherine Johnson']));
expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
vi.advanceTimersByTime(199);
expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: AUTHORS_QUERY_KEY, exact: true});
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(1);
});
it('clears pending author invalidation when the service is destroyed', () => {
vi.useFakeTimers();
service.handleNewlyCreatedBook(makeBook(['Ada Lovelace']));
TestBed.resetTestingModule();
vi.advanceTimersByTime(200);
expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
});
it('does not invalidate authors when the newly created book has no authors', () => {
service.handleNewlyCreatedBook(makeBook([]));
service.handleNewlyCreatedBook(makeBook(undefined));
expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
});
it('maps SSE auto-match responses and throws on error events', () => {
sseClient.stream.mockReturnValueOnce(of({type: 'message', data: '{"id":3,"name":"Grace"}'}));
@@ -1,14 +1,15 @@
import {computed, effect, inject, Injectable} from '@angular/core';
import {computed, DestroyRef, effect, inject, Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {lastValueFrom, Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {SseClient} from 'ngx-sse-client';
import {API_CONFIG} from '../../../core/config/api-config';
import {AuthorSummary, AuthorDetails, AuthorSearchResult, AuthorMatchRequest, AuthorUpdateRequest, AuthorPhotoResult} from '../model/author.model';
import {Book} from '../../book/model/book.model';
import {AuthService} from '../../../shared/service/auth.service';
import {injectQuery, queryOptions, QueryClient} from '@tanstack/angular-query-experimental';
import {AUTHORS_QUERY_KEY} from './author-query-keys';
import {patchAuthorInCache} from './author-query-cache';
import {invalidateAuthorsQuery, patchAuthorInCache} from './author-query-cache';
@Injectable({
providedIn: 'root'
@@ -19,9 +20,12 @@ export class AuthorService {
private authService = inject(AuthService);
private sseClient = inject(SseClient);
private queryClient = inject(QueryClient);
private destroyRef = inject(DestroyRef);
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/authors`;
private readonly mediaBaseUrl = `${API_CONFIG.BASE_URL}/api/v1/media`;
private readonly token = this.authService.token;
private readonly authorInvalidationDebounceMs = 200;
private authorInvalidationTimer: ReturnType<typeof setTimeout> | null = null;
private authorsQuery = injectQuery(() => ({
...this.getAuthorsQueryOptions(),
@@ -32,6 +36,8 @@ export class AuthorService {
isAuthorsLoading = computed(() => !!this.token() && this.authorsQuery.isPending());
constructor() {
this.destroyRef.onDestroy(() => this.clearAuthorInvalidationTimer());
effect(() => {
const token = this.token();
if (token === null) {
@@ -48,7 +54,32 @@ export class AuthorService {
}
invalidateAuthors(): void {
void this.queryClient.invalidateQueries({queryKey: AUTHORS_QUERY_KEY, exact: true});
invalidateAuthorsQuery(this.queryClient);
}
handleNewlyCreatedBook(book: Book): void {
if (!book.metadata?.authors?.length) {
return;
}
this.debouncedInvalidateAuthors();
}
private debouncedInvalidateAuthors(): void {
this.clearAuthorInvalidationTimer();
this.authorInvalidationTimer = setTimeout(() => {
this.authorInvalidationTimer = null;
this.invalidateAuthors();
}, this.authorInvalidationDebounceMs);
}
private clearAuthorInvalidationTimer(): void {
if (this.authorInvalidationTimer === null) {
return;
}
clearTimeout(this.authorInvalidationTimer);
this.authorInvalidationTimer = null;
}
patchAuthorInCache(authorId: number, fields: Partial<AuthorSummary>): void {