refactor(books): add reader navigation, book permission checks, and additional commands (#2350)
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import {computed, Injectable, signal} from '@angular/core';
|
||||
import {computed, inject, Injectable, signal} from '@angular/core';
|
||||
import {Router} from '@angular/router';
|
||||
|
||||
import {type BookFileResponse, type BookFileType, type BookSummary} from '../data/book-response.models';
|
||||
|
||||
export interface BookNavigationState {
|
||||
bookIds: number[];
|
||||
@@ -9,6 +12,8 @@ export interface BookNavigationState {
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class BookNavigationService {
|
||||
private readonly router = inject(Router);
|
||||
|
||||
private readonly _navigationState = signal<BookNavigationState | null>(null);
|
||||
readonly navigationState = this._navigationState.asReadonly();
|
||||
|
||||
@@ -53,6 +58,21 @@ export class BookNavigationService {
|
||||
this._availableBookIds.set(bookIds);
|
||||
}
|
||||
|
||||
readBook(book: BookSummary, file: BookFileResponse | undefined = book.primaryFile): void {
|
||||
const baseUrl = this.readerRoute(file?.bookType);
|
||||
if (!baseUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const path = [`/${baseUrl}/book/${book.id}`];
|
||||
if (file?.bookType !== book.primaryFile?.bookType) {
|
||||
void this.router.navigate(path, {queryParams: {bookType: file?.bookType}});
|
||||
return;
|
||||
}
|
||||
|
||||
void this.router.navigate(path);
|
||||
}
|
||||
|
||||
setNavigationContext(bookIds: number[], currentBookId: number): void {
|
||||
const currentIndex = bookIds.indexOf(currentBookId);
|
||||
if (currentIndex !== -1) {
|
||||
@@ -74,4 +94,22 @@ export class BookNavigationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private readerRoute(bookType: BookFileType | undefined): string | null {
|
||||
switch (bookType) {
|
||||
case 'PDF':
|
||||
return 'pdf-reader';
|
||||
case 'EPUB':
|
||||
case 'FB2':
|
||||
case 'MOBI':
|
||||
case 'AZW3':
|
||||
return 'ebook-reader';
|
||||
case 'CBX':
|
||||
return 'cbx-reader';
|
||||
case 'AUDIOBOOK':
|
||||
return 'audiobook-player';
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user