refactor(frontend): Migrate state to Tanstack Query + signals (#117)
* refactor(frontend): migrate state to TanStack Query and signals * fix(frontend): keep book cache in sync after patches
This commit is contained in:
@@ -5,10 +5,9 @@ import {catchError, map, tap} from 'rxjs/operators';
|
||||
import {Book, BookMetadata, BulkMetadataUpdateRequest, MetadataUpdateWrapper} from '../model/book.model';
|
||||
import {API_CONFIG} from '../../../core/config/api-config';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {BookStateService} from './book-state.service';
|
||||
import {BookSocketService} from './book-socket.service';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {BookService} from './book.service';
|
||||
import {QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {invalidateBookQueries, invalidateBooksQuery, patchBookInCacheWith, patchBookMetadataInCache} from './book-query-cache';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -19,23 +18,25 @@ export class BookMetadataManageService {
|
||||
|
||||
private http = inject(HttpClient);
|
||||
private messageService = inject(MessageService);
|
||||
private bookStateService = inject(BookStateService);
|
||||
private bookSocketService = inject(BookSocketService);
|
||||
private bookService = inject(BookService);
|
||||
private queryClient = inject(QueryClient);
|
||||
private readonly t = inject(TranslocoService);
|
||||
|
||||
updateBookMetadata(bookId: number | undefined, wrapper: MetadataUpdateWrapper, mergeCategories: boolean, replaceMode: 'REPLACE_ALL' | 'REPLACE_WHEN_PROVIDED' = 'REPLACE_ALL'): Observable<BookMetadata> {
|
||||
const params = new HttpParams().set('mergeCategories', mergeCategories.toString()).set('replaceMode', replaceMode);
|
||||
return this.http.put<BookMetadata>(`${this.url}/${bookId}/metadata`, wrapper, {params}).pipe(
|
||||
map(updatedMetadata => {
|
||||
this.bookSocketService.handleBookMetadataUpdate(bookId!, updatedMetadata);
|
||||
return updatedMetadata;
|
||||
tap(updatedMetadata => {
|
||||
if (bookId != null) {
|
||||
patchBookMetadataInCache(this.queryClient, bookId, updatedMetadata);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
updateBooksMetadata(request: BulkMetadataUpdateRequest): Observable<void> {
|
||||
return this.http.put(`${this.url}/bulk-edit-metadata`, request).pipe(
|
||||
tap(() => {
|
||||
invalidateBooksQuery(this.queryClient);
|
||||
}),
|
||||
map(() => void 0)
|
||||
);
|
||||
}
|
||||
@@ -43,16 +44,15 @@ export class BookMetadataManageService {
|
||||
toggleAllLock(bookIds: Set<number>, lock: string): Observable<void> {
|
||||
const requestBody = {
|
||||
bookIds: Array.from(bookIds),
|
||||
lock: lock
|
||||
lock
|
||||
};
|
||||
return this.http.put<BookMetadata[]>(`${this.url}/metadata/toggle-all-lock`, requestBody).pipe(
|
||||
tap((updatedMetadataList) => {
|
||||
const currentState = this.bookStateService.getCurrentBookState();
|
||||
const updatedBooks = (currentState.books || []).map(book => {
|
||||
const updatedMetadata = updatedMetadataList.find(meta => meta.bookId === book.id);
|
||||
return updatedMetadata ? {...book, metadata: updatedMetadata} : book;
|
||||
tap(updatedMetadataList => {
|
||||
updatedMetadataList.forEach(metadata => {
|
||||
if (metadata.bookId != null) {
|
||||
patchBookMetadataInCache(this.queryClient, metadata.bookId, metadata);
|
||||
}
|
||||
});
|
||||
this.bookStateService.updateBookState({...currentState, books: updatedBooks});
|
||||
}),
|
||||
map(() => void 0),
|
||||
catchError((error) => {
|
||||
@@ -64,32 +64,24 @@ export class BookMetadataManageService {
|
||||
toggleFieldLocks(bookIds: number[] | Set<number>, fieldActions: Record<string, 'LOCK' | 'UNLOCK'>): Observable<void> {
|
||||
const bookIdSet = bookIds instanceof Set ? bookIds : new Set(bookIds);
|
||||
|
||||
const requestBody = {
|
||||
return this.http.put<void>(`${this.url}/metadata/toggle-field-locks`, {
|
||||
bookIds: Array.from(bookIdSet),
|
||||
fieldActions
|
||||
};
|
||||
|
||||
return this.http.put<void>(`${this.url}/metadata/toggle-field-locks`, requestBody).pipe(
|
||||
}).pipe(
|
||||
tap(() => {
|
||||
const currentState = this.bookStateService.getCurrentBookState();
|
||||
const updatedBooks = (currentState.books || []).map(book => {
|
||||
if (!bookIdSet.has(book.id)) return book;
|
||||
const updatedMetadata = {...book.metadata};
|
||||
for (const [field, action] of Object.entries(fieldActions)) {
|
||||
const lockField = field.endsWith('Locked') ? field : `${field}Locked`;
|
||||
if (lockField in updatedMetadata) {
|
||||
(updatedMetadata as Record<string, unknown>)[lockField] = action === 'LOCK';
|
||||
for (const bookId of bookIdSet) {
|
||||
patchBookInCacheWith(this.queryClient, bookId, book => {
|
||||
if (!book.metadata) return book;
|
||||
const updatedMetadata = {...book.metadata} as Record<string, unknown>;
|
||||
for (const [field, action] of Object.entries(fieldActions)) {
|
||||
const lockField = field.endsWith('Locked') ? field : `${field}Locked`;
|
||||
if (lockField in updatedMetadata) {
|
||||
updatedMetadata[lockField] = action === 'LOCK';
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
...book,
|
||||
metadata: updatedMetadata
|
||||
};
|
||||
});
|
||||
this.bookStateService.updateBookState({
|
||||
...currentState,
|
||||
books: updatedBooks as Book[]
|
||||
});
|
||||
return {...book, metadata: updatedMetadata as BookMetadata};
|
||||
});
|
||||
}
|
||||
}),
|
||||
catchError(error => {
|
||||
this.messageService.add({
|
||||
@@ -103,39 +95,47 @@ export class BookMetadataManageService {
|
||||
}
|
||||
|
||||
consolidateMetadata(metadataType: 'authors' | 'categories' | 'moods' | 'tags' | 'series' | 'publishers' | 'languages', targetValues: string[], valuesToMerge: string[]): Observable<unknown> {
|
||||
const payload = {metadataType, targetValues, valuesToMerge};
|
||||
return this.http.post(`${this.url}/metadata/manage/consolidate`, payload).pipe(
|
||||
return this.http.post(`${this.url}/metadata/manage/consolidate`, {metadataType, targetValues, valuesToMerge}).pipe(
|
||||
tap(() => {
|
||||
this.bookService.refreshBooks();
|
||||
invalidateBooksQuery(this.queryClient);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
deleteMetadata(metadataType: 'authors' | 'categories' | 'moods' | 'tags' | 'series' | 'publishers' | 'languages', valuesToDelete: string[]): Observable<unknown> {
|
||||
const payload = {metadataType, valuesToDelete};
|
||||
return this.http.post(`${this.url}/metadata/manage/delete`, payload).pipe(
|
||||
return this.http.post(`${this.url}/metadata/manage/delete`, {metadataType, valuesToDelete}).pipe(
|
||||
tap(() => {
|
||||
this.bookService.refreshBooks();
|
||||
invalidateBooksQuery(this.queryClient);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/*------------------ Cover Operations ------------------*/
|
||||
|
||||
getUploadCoverUrl(bookId: number): string {
|
||||
return this.url + '/' + bookId + "/metadata/cover/upload"
|
||||
return this.url + '/' + bookId + '/metadata/cover/upload';
|
||||
}
|
||||
|
||||
uploadCoverFromUrl(bookId: number, url: string): Observable<BookMetadata> {
|
||||
return this.http.post<BookMetadata>(`${this.url}/${bookId}/metadata/cover/from-url`, {url});
|
||||
return this.http.post<BookMetadata>(`${this.url}/${bookId}/metadata/cover/from-url`, {url}).pipe(
|
||||
tap(updatedMetadata => {
|
||||
patchBookMetadataInCache(this.queryClient, bookId, updatedMetadata);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
regenerateCovers(missingOnly = false): Observable<void> {
|
||||
return this.http.post<void>(`${this.url}/regenerate-covers?missingOnly=${missingOnly}`, {});
|
||||
return this.http.post<void>(`${this.url}/regenerate-covers?missingOnly=${missingOnly}`, {}).pipe(
|
||||
tap(() => {
|
||||
invalidateBooksQuery(this.queryClient);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
regenerateCover(bookId: number): Observable<void> {
|
||||
return this.http.post<void>(`${this.url}/${bookId}/regenerate-cover`, {});
|
||||
return this.http.post<void>(`${this.url}/${bookId}/regenerate-cover`, {}).pipe(
|
||||
tap(() => {
|
||||
invalidateBookQueries(this.queryClient, [bookId]);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
getFileMetadata(bookId: number): Observable<BookMetadata> {
|
||||
@@ -143,37 +143,65 @@ export class BookMetadataManageService {
|
||||
}
|
||||
|
||||
generateCustomCover(bookId: number): Observable<void> {
|
||||
return this.http.post<void>(`${this.url}/${bookId}/generate-custom-cover`, {});
|
||||
return this.http.post<void>(`${this.url}/${bookId}/generate-custom-cover`, {}).pipe(
|
||||
tap(() => {
|
||||
invalidateBookQueries(this.queryClient, [bookId]);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
generateCustomCoversForBooks(bookIds: number[]): Observable<void> {
|
||||
return this.http.post<void>(`${this.url}/bulk-generate-custom-covers`, {bookIds});
|
||||
return this.http.post<void>(`${this.url}/bulk-generate-custom-covers`, {bookIds}).pipe(
|
||||
tap(() => {
|
||||
invalidateBookQueries(this.queryClient, bookIds);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
regenerateCoversForBooks(bookIds: number[]): Observable<void> {
|
||||
return this.http.post<void>(`${this.url}/bulk-regenerate-covers`, {bookIds});
|
||||
return this.http.post<void>(`${this.url}/bulk-regenerate-covers`, {bookIds}).pipe(
|
||||
tap(() => {
|
||||
invalidateBookQueries(this.queryClient, bookIds);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
uploadAudiobookCoverFromUrl(bookId: number, url: string): Observable<BookMetadata> {
|
||||
return this.http.post<BookMetadata>(`${this.url}/${bookId}/metadata/audiobook-cover/from-url`, {url});
|
||||
return this.http.post<BookMetadata>(`${this.url}/${bookId}/metadata/audiobook-cover/from-url`, {url}).pipe(
|
||||
tap(updatedMetadata => {
|
||||
patchBookMetadataInCache(this.queryClient, bookId, updatedMetadata);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
uploadAudiobookCoverFromFile(bookId: number, file: File): Observable<void> {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
return this.http.post<void>(`${this.url}/${bookId}/metadata/audiobook-cover/upload`, formData);
|
||||
return this.http.post<void>(`${this.url}/${bookId}/metadata/audiobook-cover/upload`, formData).pipe(
|
||||
tap(() => {
|
||||
invalidateBookQueries(this.queryClient, [bookId]);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
getUploadAudiobookCoverUrl(bookId: number): string {
|
||||
return this.url + '/' + bookId + "/metadata/audiobook-cover/upload";
|
||||
return this.url + '/' + bookId + '/metadata/audiobook-cover/upload';
|
||||
}
|
||||
|
||||
regenerateAudiobookCover(bookId: number): Observable<void> {
|
||||
return this.http.post<void>(`${this.url}/${bookId}/regenerate-audiobook-cover`, {});
|
||||
return this.http.post<void>(`${this.url}/${bookId}/regenerate-audiobook-cover`, {}).pipe(
|
||||
tap(() => {
|
||||
invalidateBookQueries(this.queryClient, [bookId]);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
generateCustomAudiobookCover(bookId: number): Observable<void> {
|
||||
return this.http.post<void>(`${this.url}/${bookId}/generate-custom-audiobook-cover`, {});
|
||||
return this.http.post<void>(`${this.url}/${bookId}/generate-custom-audiobook-cover`, {}).pipe(
|
||||
tap(() => {
|
||||
invalidateBookQueries(this.queryClient, [bookId]);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
supportsDualCovers(book: Book): boolean {
|
||||
@@ -187,6 +215,10 @@ export class BookMetadataManageService {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('bookIds', bookIds.join(','));
|
||||
return this.http.post<void>(`${this.url}/bulk-upload-cover`, formData);
|
||||
return this.http.post<void>(`${this.url}/bulk-upload-cover`, formData).pipe(
|
||||
tap(() => {
|
||||
invalidateBookQueries(this.queryClient, bookIds);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user