refactor(books): refresh paginated cache after legacy actions (#2222)
This commit is contained in:
@@ -10,6 +10,7 @@ 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';
|
||||
import {bookQueryKeys} from '../../book/data/book-query-keys';
|
||||
|
||||
function makeBook(authors?: string[]): Book {
|
||||
return {
|
||||
@@ -183,6 +184,28 @@ describe('AuthorService', () => {
|
||||
expect(errors[0]).toEqual(new Error('boom'));
|
||||
});
|
||||
|
||||
it('refreshes embedded book author data after an author rename', () => {
|
||||
service.updateAuthor(7, {name: 'Augusta Ada King'}).subscribe();
|
||||
|
||||
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: ['books'], exact: true});
|
||||
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: bookQueryKeys.all()});
|
||||
});
|
||||
|
||||
it('refreshes book caches on any author update, not just renames', () => {
|
||||
service.updateAuthor(7, {description: 'Mathematician'}).subscribe();
|
||||
|
||||
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: ['books'], exact: true});
|
||||
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: bookQueryKeys.all()});
|
||||
});
|
||||
|
||||
it('refreshes the author list and book caches after deleting authors', () => {
|
||||
service.deleteAuthors([7]).subscribe();
|
||||
|
||||
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: AUTHORS_QUERY_KEY, exact: true});
|
||||
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: ['books'], exact: true});
|
||||
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: bookQueryKeys.all()});
|
||||
});
|
||||
|
||||
it('builds tokenized author media urls with and without cache busters', () => {
|
||||
expect(service.getAuthorPhotoUrl(5)).toBe(`${API_CONFIG.BASE_URL}/api/v1/media/author/5/photo?token=token-123`);
|
||||
expect(service.getAuthorThumbnailUrl(5)).toBe(`${API_CONFIG.BASE_URL}/api/v1/media/author/5/thumbnail?token=token-123`);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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 {map, tap} 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';
|
||||
@@ -10,6 +10,7 @@ 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 {invalidateAuthorsQuery, patchAuthorInCache} from './author-query-cache';
|
||||
import {invalidateAllBookCaches} from '../../book/service/legacy-book-cache';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -136,7 +137,9 @@ export class AuthorService {
|
||||
}
|
||||
|
||||
updateAuthor(authorId: number, request: AuthorUpdateRequest): Observable<AuthorDetails> {
|
||||
return this.http.put<AuthorDetails>(`${this.baseUrl}/${authorId}`, request);
|
||||
return this.http.put<AuthorDetails>(`${this.baseUrl}/${authorId}`, request).pipe(
|
||||
tap(() => invalidateAllBookCaches(this.queryClient))
|
||||
);
|
||||
}
|
||||
|
||||
unmatchAuthors(authorIds: number[]): Observable<void> {
|
||||
@@ -144,7 +147,12 @@ export class AuthorService {
|
||||
}
|
||||
|
||||
deleteAuthors(authorIds: number[]): Observable<void> {
|
||||
return this.http.delete<void>(this.baseUrl, {body: authorIds});
|
||||
return this.http.delete<void>(this.baseUrl, {body: authorIds}).pipe(
|
||||
tap(() => {
|
||||
invalidateAllBookCaches(this.queryClient);
|
||||
invalidateAuthorsQuery(this.queryClient);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
searchAuthorPhotos(authorId: number, query: string): Observable<AuthorPhotoResult> {
|
||||
|
||||
Reference in New Issue
Block a user