From 9be47b7ec0fed8ac056692d79b11c66117ed25e3 Mon Sep 17 00:00:00 2001 From: Alex <25013571+alexhb1@users.noreply.github.com> Date: Mon, 21 Sep 2026 13:01:51 +0100 Subject: [PATCH] refactor(books): add shelf data layer (#2188) --- .../data/book-command-pending-state.spec.ts | 24 ++++++- .../book/data/book-command-pending-state.ts | 45 +++++++++++++ .../book/data/book-shelf-command-keys.ts | 9 +++ .../book/data/book-shelf-command.models.ts | 9 +++ .../book/data/book-shelf-command.service.ts | 65 +++++++++++++++++++ .../book/data/shelf-definition-query-cache.ts | 9 +++ .../book/data/shelf-definition-query-keys.ts | 4 ++ .../data/shelf-definition-query.service.ts | 38 +++++++++++ .../book/data/shelf-definition.models.ts | 11 ++++ 9 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 frontend/src/app/features/book/data/book-shelf-command-keys.ts create mode 100644 frontend/src/app/features/book/data/book-shelf-command.models.ts create mode 100644 frontend/src/app/features/book/data/book-shelf-command.service.ts create mode 100644 frontend/src/app/features/book/data/shelf-definition-query-cache.ts create mode 100644 frontend/src/app/features/book/data/shelf-definition-query-keys.ts create mode 100644 frontend/src/app/features/book/data/shelf-definition-query.service.ts create mode 100644 frontend/src/app/features/book/data/shelf-definition.models.ts diff --git a/frontend/src/app/features/book/data/book-command-pending-state.spec.ts b/frontend/src/app/features/book/data/book-command-pending-state.spec.ts index 7c249409b..80326deac 100644 --- a/frontend/src/app/features/book/data/book-command-pending-state.spec.ts +++ b/frontend/src/app/features/book/data/book-command-pending-state.spec.ts @@ -1,6 +1,6 @@ import {describe, expect, it} from 'vitest'; -import {overlayPendingBookState} from './book-command-pending-state'; +import {overlayPendingBookState, overlayShelfIds} from './book-command-pending-state'; import {type BookSummary} from './book-response.models'; describe('overlayPendingBookState', () => { @@ -9,6 +9,10 @@ describe('overlayPendingBookState', () => { libraryId: 2, libraryName: 'Library', readStatus: 'UNREAD', + shelves: [ + {id: 20, name: 'Remove', publicShelf: false, bookCount: 1}, + {id: 30, name: 'Keep', publicShelf: false, bookCount: 1}, + ], epubProgress: { cfi: 'epubcfi(/6/2)', href: 'chapter-1.xhtml', @@ -56,4 +60,22 @@ describe('overlayPendingBookState', () => { expect(result.koreaderProgress).toBeUndefined(); expect(result.epubProgress).toEqual(book.epubProgress); }); + + it('leaves shelf membership off the overlaid book', () => { + expect(overlayPendingBookState(book, { + ...emptyOverlay, + readStatuses: new Map([[1, 'READ']]), + }).shelves).toBe(book.shelves); + }); + + it('reports confirmed shelf IDs when nothing is in flight', () => { + expect(overlayShelfIds(book, undefined)).toEqual(new Set([20, 30])); + }); + + it('applies in-flight assignments and removals to the shelf IDs', () => { + expect(overlayShelfIds(book, { + assignShelfIds: new Set([40]), + unassignShelfIds: new Set([20]), + })).toEqual(new Set([30, 40])); + }); }); diff --git a/frontend/src/app/features/book/data/book-command-pending-state.ts b/frontend/src/app/features/book/data/book-command-pending-state.ts index 0cabceefb..315ae3852 100644 --- a/frontend/src/app/features/book/data/book-command-pending-state.ts +++ b/frontend/src/app/features/book/data/book-command-pending-state.ts @@ -9,6 +9,13 @@ import { SetBookReadStatusVariables, } from './book-command.models'; import {BookReadStatus, BookSummary} from './book-response.models'; +import {bookShelfCommandKeys} from './book-shelf-command-keys'; +import {UpdateBookShelfMembershipVariables} from './book-shelf-command.models'; + +export interface PendingShelfMembership { + readonly assignShelfIds: ReadonlySet; + readonly unassignShelfIds: ReadonlySet; +} export interface PendingBookOverlay { readonly readStatuses: ReadonlyMap; @@ -49,6 +56,27 @@ export function injectPendingBookReadStatuses(): Signal> { + return injectPendingBookValues( + bookShelfCommandKeys.updateMembership(), + (current, variables) => { + const assignShelfIds = new Set(current?.assignShelfIds); + const unassignShelfIds = new Set(current?.unassignShelfIds); + + for (const shelfId of variables.unassignShelfIds) { + assignShelfIds.delete(shelfId); + unassignShelfIds.add(shelfId); + } + for (const shelfId of variables.assignShelfIds) { + unassignShelfIds.delete(shelfId); + assignShelfIds.add(shelfId); + } + + return {assignShelfIds, unassignShelfIds}; + }, + ); +} + export function injectPendingBookProgressResets(): Signal> { return injectPendingBookValues( bookCommandKeys.resetProgress(), @@ -95,3 +123,20 @@ function clearedProgress(source: BookProgressSource): Partial { audiobookProgress: undefined, }; } + +export function overlayShelfIds( + book: BookSummary, + membership: PendingShelfMembership | undefined, +): ReadonlySet { + const shelfIds = new Set((book.shelves ?? []).map(shelf => shelf.id)); + if (!membership) { + return shelfIds; + } + for (const shelfId of membership.unassignShelfIds) { + shelfIds.delete(shelfId); + } + for (const shelfId of membership.assignShelfIds) { + shelfIds.add(shelfId); + } + return shelfIds; +} diff --git a/frontend/src/app/features/book/data/book-shelf-command-keys.ts b/frontend/src/app/features/book/data/book-shelf-command-keys.ts new file mode 100644 index 000000000..929f132f7 --- /dev/null +++ b/frontend/src/app/features/book/data/book-shelf-command-keys.ts @@ -0,0 +1,9 @@ +import {bookCommandKeys} from './book-command-keys'; + +export const bookShelfCommandKeys = { + updateMembership: () => [...bookCommandKeys.all(), 'shelf', 'update-membership'] as const, +}; + +export const bookShelfCommandScopes = { + regularShelves: {id: 'books.command.shelf'} as const, +}; diff --git a/frontend/src/app/features/book/data/book-shelf-command.models.ts b/frontend/src/app/features/book/data/book-shelf-command.models.ts new file mode 100644 index 000000000..9a6e45927 --- /dev/null +++ b/frontend/src/app/features/book/data/book-shelf-command.models.ts @@ -0,0 +1,9 @@ +export interface UpdateBookShelfMembershipVariables { + readonly bookIds: readonly number[]; + readonly assignShelfIds: readonly number[]; + readonly unassignShelfIds: readonly number[]; +} + +export interface UpdateBookShelfMembershipResult { + readonly confirmedBookIds: readonly number[]; +} diff --git a/frontend/src/app/features/book/data/book-shelf-command.service.ts b/frontend/src/app/features/book/data/book-shelf-command.service.ts new file mode 100644 index 000000000..1616f5f9b --- /dev/null +++ b/frontend/src/app/features/book/data/book-shelf-command.service.ts @@ -0,0 +1,65 @@ +import {HttpClient} from '@angular/common/http'; +import {inject, Injectable} from '@angular/core'; +import {lastValueFrom} from 'rxjs'; + +import {API_CONFIG} from '../../../core/config/api-config'; +import {reconcilingMutationOptions} from '../../../core/data/command-options'; +import {bookCommandChangeSet, requireBookIds} from './book-command.models'; +import {bookShelfCommandKeys, bookShelfCommandScopes} from './book-shelf-command-keys'; +import { + UpdateBookShelfMembershipResult, + UpdateBookShelfMembershipVariables, +} from './book-shelf-command.models'; +import {applyBookQueryChangeSet} from './book-query-cache'; +import {invalidateShelfDefinitions} from './shelf-definition-query-cache'; + +@Injectable({providedIn: 'root'}) +export class BookShelfCommandService { + private readonly http = inject(HttpClient); + private readonly membershipUrl = `${API_CONFIG.BASE_URL}/api/v1/books/shelves`; + + updateMembership() { + return reconcilingMutationOptions({ + mutationKey: bookShelfCommandKeys.updateMembership(), + scope: bookShelfCommandScopes.regularShelves, + mutationFn: (variables: UpdateBookShelfMembershipVariables) => this.postMembership( + requireBookIds(variables.bookIds), + variables.assignShelfIds, + variables.unassignShelfIds, + ), + reconcile: async (outcome, variables, client) => { + await Promise.all([ + applyBookQueryChangeSet( + client, + bookCommandChangeSet(outcome, variables.bookIds, ({confirmedBookIds}) => ({ + changedBookIds: confirmedBookIds, + })), + ), + invalidateShelfDefinitions(client), + ]); + }, + }); + } + + private async postMembership( + bookIds: readonly number[], + assignShelfIds: readonly number[], + unassignShelfIds: readonly number[], + ): Promise { + const response = await lastValueFrom(this.http.post( + this.membershipUrl, + { + bookIds, + shelvesToAssign: assignShelfIds, + shelvesToUnassign: unassignShelfIds, + }, + )); + return { + confirmedBookIds: response.map(book => book.id), + }; + } +} + +interface MembershipBookResponse { + readonly id: number; +} diff --git a/frontend/src/app/features/book/data/shelf-definition-query-cache.ts b/frontend/src/app/features/book/data/shelf-definition-query-cache.ts new file mode 100644 index 000000000..7930c00bf --- /dev/null +++ b/frontend/src/app/features/book/data/shelf-definition-query-cache.ts @@ -0,0 +1,9 @@ +import {QueryClient} from '@tanstack/angular-query-experimental'; + +import {shelfDefinitionQueryKeys} from './shelf-definition-query-keys'; + +export function invalidateShelfDefinitions(client: QueryClient): Promise { + return client.invalidateQueries({ + queryKey: shelfDefinitionQueryKeys.all(), + }); +} diff --git a/frontend/src/app/features/book/data/shelf-definition-query-keys.ts b/frontend/src/app/features/book/data/shelf-definition-query-keys.ts new file mode 100644 index 000000000..eb0e6d1dc --- /dev/null +++ b/frontend/src/app/features/book/data/shelf-definition-query-keys.ts @@ -0,0 +1,4 @@ +export const shelfDefinitionQueryKeys = { + all: () => ['shelves', 'query'] as const, + definitions: () => [...shelfDefinitionQueryKeys.all(), 'definitions'] as const, +}; diff --git a/frontend/src/app/features/book/data/shelf-definition-query.service.ts b/frontend/src/app/features/book/data/shelf-definition-query.service.ts new file mode 100644 index 000000000..bcc3155fd --- /dev/null +++ b/frontend/src/app/features/book/data/shelf-definition-query.service.ts @@ -0,0 +1,38 @@ +import {HttpClient} from '@angular/common/http'; +import {effect, inject, Injectable} from '@angular/core'; +import {queryOptions, QueryClient} from '@tanstack/angular-query-experimental'; +import {lastValueFrom, takeUntil} from 'rxjs'; + +import {API_CONFIG} from '../../../core/config/api-config'; +import {abortSignal, QUERY_DEFAULTS} from '../../../core/data/query-transport'; +import {ShelfDefinition} from './shelf-definition.models'; +import {shelfDefinitionQueryKeys} from './shelf-definition-query-keys'; +import {AuthService} from '../../../shared/service/auth.service'; + +@Injectable({providedIn: 'root'}) +export class ShelfDefinitionQueryService { + private readonly http = inject(HttpClient); + private readonly authService = inject(AuthService); + private readonly queryClient = inject(QueryClient); + private readonly url = `${API_CONFIG.BASE_URL}/api/v1/shelves`; + + constructor() { + effect(() => { + if (this.authService.token() === null) { + this.queryClient.removeQueries({queryKey: shelfDefinitionQueryKeys.all()}); + } + }); + } + + definitions() { + return queryOptions({ + queryKey: shelfDefinitionQueryKeys.definitions(), + queryFn: ({signal}): Promise => lastValueFrom( + this.http.get(this.url).pipe( + takeUntil(abortSignal(signal)), + ), + ), + ...QUERY_DEFAULTS, + }); + } +} diff --git a/frontend/src/app/features/book/data/shelf-definition.models.ts b/frontend/src/app/features/book/data/shelf-definition.models.ts new file mode 100644 index 000000000..9d0dc9feb --- /dev/null +++ b/frontend/src/app/features/book/data/shelf-definition.models.ts @@ -0,0 +1,11 @@ +import {IconType} from '../../../shared/icons/icon-selection'; + +export interface ShelfDefinition { + readonly id: number; + readonly userId: number; + readonly name: string; + readonly icon?: string; + readonly iconType?: IconType; + readonly publicShelf: boolean; + readonly bookCount: number; +}