refactor(books): refresh paginated cache after legacy actions (#2222)

This commit is contained in:
Alex
2026-09-21 13:01:54 +01:00
committed by GitHub
parent 79ebf13be3
commit 49cc7add81
16 changed files with 216 additions and 16 deletions
@@ -1,21 +1,27 @@
import {provideHttpClient} from '@angular/common/http';
import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing';
import {TestBed} from '@angular/core/testing';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {QueryClient} from '@tanstack/angular-query-experimental';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {API_CONFIG} from '../../../core/config/api-config';
import {bookQueryKeys} from '../../book/data/book-query-keys';
import {AUTHORS_QUERY_KEY} from '../../author-browser/service/author-query-keys';
import {SidecarService} from './sidecar.service';
describe('SidecarService', () => {
let service: SidecarService;
let httpTestingController: HttpTestingController;
let queryClient: QueryClient;
beforeEach(() => {
queryClient = new QueryClient();
TestBed.configureTestingModule({
providers: [
provideHttpClient(),
provideHttpClientTesting(),
SidecarService,
{provide: QueryClient, useValue: queryClient},
]
});
@@ -25,6 +31,7 @@ describe('SidecarService', () => {
afterEach(() => {
httpTestingController.verify();
queryClient.clear();
TestBed.resetTestingModule();
});
@@ -65,6 +72,8 @@ describe('SidecarService', () => {
});
it('imports a sidecar for a book', () => {
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries').mockResolvedValue(undefined);
service.importFromSidecar(42).subscribe(response => {
expect(response).toEqual({message: 'done'});
});
@@ -73,6 +82,10 @@ describe('SidecarService', () => {
expect(request.request.method).toBe('POST');
expect(request.request.body).toEqual({});
request.flush({message: 'done'});
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: ['books', 'detail', 42]});
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: bookQueryKeys.collections()});
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: AUTHORS_QUERY_KEY, exact: true});
});
it('bulk exports sidecars for a library', () => {
@@ -87,6 +100,8 @@ describe('SidecarService', () => {
});
it('bulk imports sidecars for a library', () => {
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries').mockResolvedValue(undefined);
service.bulkImport(7).subscribe(response => {
expect(response).toEqual({message: 'imported', imported: 2});
});
@@ -95,5 +110,8 @@ describe('SidecarService', () => {
expect(request.request.method).toBe('POST');
expect(request.request.body).toEqual({});
request.flush({message: 'imported', imported: 2});
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: bookQueryKeys.all()});
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: AUTHORS_QUERY_KEY, exact: true});
});
});
@@ -1,7 +1,11 @@
import {inject, Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {Observable, tap} from 'rxjs';
import {QueryClient} from '@tanstack/angular-query-experimental';
import {API_CONFIG} from '../../../core/config/api-config';
import {invalidateAllBookCaches, invalidateBooksById} from '../../book/service/legacy-book-cache';
import {invalidateAuthorsQuery} from '../../author-browser/service/author-query-cache';
export interface SidecarCoverInfo {
source: string;
@@ -80,6 +84,7 @@ export type SidecarSyncStatus = 'IN_SYNC' | 'OUTDATED' | 'MISSING' | 'CONFLICT'
})
export class SidecarService {
private http = inject(HttpClient);
private queryClient = inject(QueryClient);
private readonly apiUrl = `${API_CONFIG.BASE_URL}/api/v1`;
getSidecarContent(bookId: number): Observable<SidecarMetadata> {
@@ -95,7 +100,12 @@ export class SidecarService {
}
importFromSidecar(bookId: number): Observable<{message: string}> {
return this.http.post<{message: string}>(`${this.apiUrl}/books/${bookId}/sidecar/import`, {});
return this.http.post<{message: string}>(`${this.apiUrl}/books/${bookId}/sidecar/import`, {}).pipe(
tap(() => {
invalidateBooksById(this.queryClient, [bookId]);
invalidateAuthorsQuery(this.queryClient);
})
);
}
bulkExport(libraryId: number): Observable<{message: string, exported: number}> {
@@ -107,6 +117,11 @@ export class SidecarService {
bulkImport(libraryId: number): Observable<{message: string, imported: number}> {
return this.http.post<{message: string, imported: number}>(
`${this.apiUrl}/libraries/${libraryId}/sidecar/import-all`, {}
).pipe(
tap(() => {
invalidateAllBookCaches(this.queryClient);
invalidateAuthorsQuery(this.queryClient);
})
);
}
}