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
@@ -9,6 +9,7 @@ import type {GroupRule} from '../component/magic-shelf-component';
import {BookService} from '../../book/service/book.service';
import {BookRuleEvaluatorService} from './book-rule-evaluator.service';
import {MagicShelfService, type MagicShelf} from './magic-shelf.service';
import {bookQueryKeys} from '../../book/data/book-query-keys';
function buildMagicShelf(overrides: Partial<MagicShelf> = {}): MagicShelf {
return {
@@ -170,6 +171,26 @@ describe('MagicShelfService', () => {
request.flush(buildMagicShelf({id: 11, name: 'Magic', filterJson: JSON.stringify(group), isPublic: true}));
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: ['magicShelves'], exact: true});
expect(invalidateQueriesSpy).not.toHaveBeenCalledWith({queryKey: bookQueryKeys.collections()});
});
it('invalidates browser collections after changing an existing shelf rule', () => {
const invalidateQueriesSpy = vi.spyOn(queryClientHarness.queryClient, 'invalidateQueries').mockResolvedValue(undefined);
httpTestingController.expectOne(req => req.url.endsWith('/api/magic-shelves')).flush([]);
service.saveShelf({
id: 11,
name: 'Changed',
icon: 'zap',
group: buildGroupRule({name: 'Changed'}),
}).subscribe();
httpTestingController.expectOne(req => req.url.endsWith('/api/magic-shelves')).flush(
buildMagicShelf({id: 11, name: 'Changed'}),
);
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: bookQueryKeys.collections()});
});
it('invalidates shelf queries after delete', () => {
@@ -184,6 +205,7 @@ describe('MagicShelfService', () => {
request.flush(null);
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: ['magicShelves'], exact: true});
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: bookQueryKeys.collections()});
});
it('finds shelves by id from the hydrated query state', async () => {
@@ -10,6 +10,7 @@ import {BookRuleEvaluatorService} from './book-rule-evaluator.service';
import {AuthService} from '../../../shared/service/auth.service';
import {GroupRule} from '../component/magic-shelf-component';
import {IconType} from '../../../shared/icons/icon-selection';
import {invalidateBookCollections} from '../../book/data/book-query-cache';
export interface MagicShelf {
id?: number | null;
@@ -89,6 +90,9 @@ export class MagicShelfService {
return this.http.post<MagicShelf>(this.url, payload).pipe(
tap(() => {
void this.queryClient.invalidateQueries({queryKey: MAGIC_SHELVES_QUERY_KEY, exact: true});
if (data.id != null) {
void invalidateBookCollections(this.queryClient);
}
})
);
}
@@ -131,6 +135,7 @@ export class MagicShelfService {
return this.http.delete<void>(`${this.url}/${id}`).pipe(
tap(() => {
void this.queryClient.invalidateQueries({queryKey: MAGIC_SHELVES_QUERY_KEY, exact: true});
void invalidateBookCollections(this.queryClient);
})
);
}