Checkpoint/WIP: Completely refactor state management

This commit is contained in:
aditya.chandel
2024-12-24 12:54:29 -07:00
parent 043c681132
commit 84724ec014
30 changed files with 535 additions and 923 deletions
@@ -1,12 +1,12 @@
import {Component, Input, OnInit} from '@angular/core';
import {Book} from '../../model/book.model';
import {Button} from 'primeng/button';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {Router} from '@angular/router';
import {MenuModule} from 'primeng/menu';
import {MenuItem} from 'primeng/api';
import {DialogService} from "primeng/dynamicdialog";
import {ShelfAssignerComponent} from "../shelf-assigner/shelf-assigner.component";
import {BookService} from '../../service/book.service';
@Component({
selector: 'app-book-card',
@@ -23,16 +23,16 @@ export class BookCardComponent implements OnInit {
items: MenuItem[] | undefined;
constructor(
private libraryBookService: LibraryAndBookService, private router: Router,
private bookService: BookService, private router: Router,
private dialogService: DialogService) {
}
coverImageSrc(book: Book): string {
return this.libraryBookService.getBookCoverUrl(book.id);
return this.bookService.getBookCoverUrl(book.id);
}
readBook(book: Book) {
this.libraryBookService.readBook(book);
this.bookService.readBook(book);
}
openBookInfo(book: Book) {
@@ -1,6 +1,5 @@
import {Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {Book} from '../../model/book.model';
import {Button} from 'primeng/button';
import {NgForOf, NgIf} from '@angular/common';
@@ -8,6 +7,8 @@ import {BooksMetadataDialogComponent} from '../books-metadata-dialog/books-metad
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Subscription} from 'rxjs';
import {TagModule} from 'primeng/tag';
import {BookService} from '../../service/book.service';
import {LibraryService} from '../../service/library.service';
@Component({
selector: 'app-book-metadata',
@@ -30,8 +31,9 @@ export class BookMetadataComponent implements OnInit, OnDestroy {
private dialogSubscription?: Subscription;
constructor(
private bookService: LibraryAndBookService, private activatedRoute: ActivatedRoute,
private dialogService: DialogService, private router: Router) {
private bookService: BookService, private activatedRoute: ActivatedRoute,
private dialogService: DialogService, private router: Router,
private libraryService: LibraryService) {
}
ngOnInit(): void {
@@ -45,7 +47,7 @@ export class BookMetadataComponent implements OnInit, OnDestroy {
}
private loadBookWithNeighbors(libraryId: number, bookId: number): void {
this.bookService.getBookWithNeighbours(bookId, libraryId).subscribe((response) => {
this.libraryService.getBookWithNeighbours(bookId, libraryId).subscribe((response) => {
this.book = response.currentBook;
this.nextBookId = response.nextBookId;
this.previousBookId = response.previousBookId;
@@ -1,6 +1,6 @@
<div class="library-header">
<div class="library-header-dial">
<h3 class="library-name">{{ title() }}</h3>
<h3 class="library-name">{{ (library$ | async)?.name }}</h3>
<p-speedDial
[model]="items!"
direction="right"
@@ -22,15 +22,15 @@
</div>
<div class="content-container">
<div class="card-container">
<virtual-scroller *ngIf="books().length > 0" class="virtual-scroller" #scroll [items]="books()">
<div class="card-container" *ngIf="books$ | async as books">
<virtual-scroller *ngIf="books.length > 0" class="virtual-scroller" #scroll [items]="books">
<div class="grid" #container>
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
<app-book-card [book]="book"></app-book-card>
</div>
</div>
</virtual-scroller>
<div *ngIf="books().length === 0" class="empty-message">
<div *ngIf="books.length === 0" class="empty-message">
<p>No books available to display.</p>
</div>
</div>
@@ -1,152 +1,115 @@
import {Component, OnInit, computed, signal, Signal} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
import {Book} from "../../model/book.model";
import {LibraryService} from '../../service/library.service';
import {BookService} from '../../service/book.service';
import {map, switchMap} from 'rxjs/operators';
import {Observable, of} from 'rxjs';
import {Book} from '../../model/book.model';
import {Library} from '../../model/library.model';
import {ShelfService} from '../../service/shelf.service';
@Component({
selector: 'app-books-browser',
standalone: false,
templateUrl: './books-browser.component.html',
styleUrls: ['./books-browser.component.scss']
selector: 'app-books-browser',
standalone: false,
templateUrl: './books-browser.component.html',
styleUrls: ['./books-browser.component.scss']
})
export class BooksBrowserComponent implements OnInit {
private currentEntityId = signal<number | null>(null);
private entityType = signal<'library' | 'shelf'>('library');
books!: Signal<Book[]>;
title = computed(() => {
const entityId = this.currentEntityId();
const type = this.entityType();
if (type === 'library') {
const library = this.libraryBookService.getLibraries()().find(library => library.id === entityId);
return library ? `Library: ${library.name}` : 'Library not found';
books$: Observable<Book[]> | undefined;
library$: Observable<Library | null> | undefined;
items: MenuItem[] | undefined;
constructor(
private activatedRoute: ActivatedRoute,
private router: Router,
private confirmationService: ConfirmationService,
private messageService: MessageService,
private libraryService: LibraryService,
private bookService: BookService,
private shelfService: ShelfService
) {
}
ngOnInit(): void {
const libraryId$ = this.activatedRoute.paramMap.pipe(
map(params => Number(params.get('libraryId')))
);
this.books$ = libraryId$.pipe(
switchMap(libraryId => {
if (!isNaN(libraryId)) {
return this.bookService.books$.pipe(
map(books => books.filter(book => book.libraryId === libraryId))
);
} else {
return of([]);
}
})
);
if (type === 'shelf') {
const shelf = this.libraryBookService.getShelves()().find(shelf => shelf.id === entityId);
return shelf ? `Shelf: ${shelf.name}` : 'Shelf not found';
this.library$ = this.activatedRoute.paramMap.pipe(
switchMap(params => {
const libraryId = Number(params.get('libraryId'));
if (!isNaN(libraryId)) {
return this.libraryService.libraries$.pipe(
map(libraries => libraries.find(lib => lib.id === libraryId) || null)
);
}
return '';
});
return of(null);
})
);
items: MenuItem[] | undefined;
this.initializeLibraryMenuItems();
}
constructor(
private activatedRoute: ActivatedRoute,
private router: Router,
private libraryBookService: LibraryAndBookService,
private confirmationService: ConfirmationService,
private messageService: MessageService
) {
}
ngOnInit(): void {
this.activatedRoute.paramMap.subscribe((params) => {
const libraryId = Number(params.get('libraryId'));
const shelfId = Number(params.get('shelfId'));
if (libraryId) {
this.entityType.set('library');
this.currentEntityId.set(libraryId);
this.books = this.libraryBookService.getLibraryBooks(libraryId);
this.libraryBookService.getLibraries();
} else if (shelfId) {
this.entityType.set('shelf');
this.currentEntityId.set(shelfId);
this.books = this.libraryBookService.getShelfBooks(shelfId);
this.libraryBookService.getShelves();
}
});
if (this.entityType() === 'library') {
this.initializeLibraryMenuItems();
} else if (this.entityType() === 'shelf') {
this.initializeShelfMenuItems();
}
}
private initializeLibraryMenuItems(): void {
this.items = [
{
icon: 'pi pi-trash',
tooltipOptions: {
tooltipLabel: 'Delete',
tooltipPosition: 'top'
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete ' + this.title() + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text',
accept: () => {
this.libraryBookService.deleteLibrary(this.currentEntityId()).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({
severity: 'info',
summary: 'Success',
detail: 'Library was deleted'
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete library',
life: 3000
});
}
});
}
private initializeLibraryMenuItems(): void {
this.items = [
{
icon: 'pi pi-trash',
tooltipOptions: {
tooltipLabel: 'Delete',
tooltipPosition: 'top'
},
command: () => {
this.library$?.subscribe(library => {
if (library) {
this.confirmationService.confirm({
message: `Are you sure you want to delete ${library.name}?`,
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text',
accept: () => {
if (library.id) {
this.libraryService.deleteLibrary(library.id).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({
severity: 'info',
summary: 'Success',
detail: 'Library was deleted'
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete library',
life: 3000
});
}
});
}
}
});
}
];
}
});
}
}
];
}
private initializeShelfMenuItems(): void {
this.items = [
{
icon: 'pi pi-trash',
tooltipOptions: {
tooltipLabel: 'Delete',
tooltipPosition: 'top'
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete ' + this.title() + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
rejectIcon: 'none',
rejectButtonStyleClass: 'p-button-text',
accept: () => {
this.libraryBookService.deleteShelf(this.currentEntityId()!).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({
severity: 'info',
summary: 'Success',
detail: 'Shelf was deleted'
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Failed',
detail: 'Failed to delete shelf',
life: 3000
});
}
});
}
});
}
}
];
}
}
@@ -1,11 +1,11 @@
import {Component, OnInit} from '@angular/core';
import {BookMetadata} from '../../model/book.model';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {NgForOf, NgIf} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Router} from '@angular/router';
import {Button} from 'primeng/button';
import {BookService} from '../../service/book.service';
@Component({
selector: 'app-books-metadata-dialog',
@@ -27,7 +27,7 @@ export class BooksMetadataDialogComponent implements OnInit {
bookId: number = 0;
libraryId: number = 0;
constructor(private bookService: LibraryAndBookService, public dynamicDialogConfig: DynamicDialogConfig, private dynamicDialogRef: DynamicDialogRef, private router: Router) {
constructor(private bookService: BookService, public dynamicDialogConfig: DynamicDialogConfig, private dynamicDialogRef: DynamicDialogRef, private router: Router) {
this.searchText = dynamicDialogConfig.data.bookTitle;
this.bookId = this.dynamicDialogConfig.data.bookId;
this.libraryId = this.dynamicDialogConfig.data.libraryId;
@@ -2,7 +2,7 @@
<div class="book-list-container">
<div class="last-read-title-container">
<h2 class="last-read-title">{{ title }}</h2>
<div *ngIf="booksSignal!().length === 0">
<div *ngIf="(books$ | async)?.length === 0">
<h4>No books available.</h4>
</div>
</div>
@@ -12,7 +12,7 @@
[infiniteScrollThrottle]="50"
#scrollContainer
[horizontal]="true">
<div class="book-item" *ngFor="let book of booksSignal!()">
<div class="book-item" *ngFor="let book of books$ | async">
<app-book-card [book]="book"></app-book-card>
</div>
</div>
@@ -1,22 +1,22 @@
import {Component, OnInit, ViewChild, ElementRef, Input, computed, signal, Signal} from '@angular/core';
import {Component, ElementRef, Input, OnInit, ViewChild} from '@angular/core';
import {Book} from '../../model/book.model';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {Button} from 'primeng/button';
import {InfiniteScrollDirective} from 'ngx-infinite-scroll';
import {NgForOf, NgIf} from '@angular/common';
import {Router} from '@angular/router';
import {AsyncPipe, NgForOf, NgIf} from '@angular/common';
import {BookCardComponent} from '../book-card/book-card.component';
import {Observable} from 'rxjs';
import {BookService} from '../../service/book.service';
import {map} from 'rxjs/operators';
@Component({
selector: 'app-dashboard-scroller',
templateUrl: './dashboard-scroller.component.html',
styleUrls: ['./dashboard-scroller.component.scss'],
imports: [
Button,
InfiniteScrollDirective,
NgForOf,
NgIf,
BookCardComponent
BookCardComponent,
AsyncPipe
],
})
export class DashboardScrollerComponent implements OnInit {
@@ -25,37 +25,31 @@ export class DashboardScrollerComponent implements OnInit {
@Input() title: string = 'Last Read Books';
@ViewChild('scrollContainer') scrollContainer!: ElementRef;
booksSignal: Signal<Book[]> | undefined;
books$: Observable<Book[]> | undefined;
constructor(private bookService: LibraryAndBookService, private router: Router) {
constructor(private bookService: BookService) {
}
ngOnInit(): void {
this.booksSignal = this.bookListType === 'lastRead' ? this.bookService.getLastReadBooks() : this.bookService.getLatestAddedBooks();;
this.loadBooks();
}
loadBooks(): void {
if (this.bookListType === 'lastRead' && !this.bookService.getLastReadBooks()) {
this.bookService.getLastReadBooks();
} else if (this.bookListType === 'latestAdded' && !this.bookService.getLatestAddedBooks()) {
this.bookService.getLatestAddedBooks();
if (this.bookListType === 'lastRead') {
this.books$ = this.bookService.books$.pipe((
map(books =>
books
.filter(book => book.lastReadTime)
.sort((a, b) => new Date(b.lastReadTime!).getTime() - new Date(a.lastReadTime!).getTime())
.slice(0, 25)
)
))
}
if (this.bookListType === 'latestAdded') {
this.books$ = this.bookService.books$.pipe((
map(books =>
books
.filter(book => book.addedOn)
.sort((a, b) => new Date(b.addedOn!).getTime() - new Date(a.addedOn!).getTime())
.slice(0, 25)
)
))
}
}
coverImageSrc(bookId: number): string {
return this.bookService.getBookCoverUrl(bookId);
}
getAuthorNames(book: Book): string {
return book.metadata?.authors?.map((author) => author.name).join(', ') || 'No authors available';
}
openBook(book: Book): void {
this.bookService.readBook(book);
}
openBookInfo(bookId: number, libraryId: number) {
this.router.navigate(['/library', libraryId, 'book', bookId, 'info']);
}
}
@@ -1,6 +1,6 @@
<div class="dashboard">
<div class="dashboard-no-library" *ngIf="isLibrariesEmpty; else dashboardScroller">
<div class="dashboard-no-library" *ngIf="isLibrariesEmpty$ | async; else dashboardScroller">
<h1 class="no-library-header">
Looks like you haven't added a library yet. Let's add one!
</h1>
@@ -1,11 +1,12 @@
import {Component, Signal} from '@angular/core';
import {Library} from '../../model/library.model';
import {Component} from '@angular/core';
import {Button} from 'primeng/button';
import {NgIf} from '@angular/common';
import {AsyncPipe, NgIf} from '@angular/common';
import {LibraryCreatorComponent} from '../library-creator/library-creator.component';
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {DashboardScrollerComponent} from '../dashboard-scroller/dashboard-scroller.component';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {LibraryService} from '../../service/library.service';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
@Component({
selector: 'app-home-page',
@@ -14,20 +15,19 @@ import {LibraryAndBookService} from '../../service/library-and-book.service';
imports: [
Button,
NgIf,
DashboardScrollerComponent
DashboardScrollerComponent,
AsyncPipe
],
providers: [DialogService],
})
export class DashboardComponent {
private libraries: Signal<Library[]>;
isLibrariesEmpty$: Observable<boolean>;
ref: DynamicDialogRef | undefined;
constructor(private libraryBookService: LibraryAndBookService, public dialogService: DialogService) {
this.libraries = this.libraryBookService.getLibraries();
}
get isLibrariesEmpty(): boolean {
return this.libraries()?.length === 0;
constructor(private libraryService: LibraryService, public dialogService: DialogService) {
this.isLibrariesEmpty$ = this.libraryService.libraries$.pipe(
map(libraries => libraries.length === 0)
);
}
createNewLibrary(event: MouseEvent) {
@@ -2,7 +2,7 @@
<div class="flex">
<p-dropdown
[options]="libraries()"
[options]="(libraries$ | async) || []"
optionLabel="name"
placeholder="Select Library"
[(ngModel)]="selectedLibrary">
@@ -1,13 +1,14 @@
import {Component, Signal} from '@angular/core';
import {Component} from '@angular/core';
import {Library} from '../../model/library.model';
import {FileUploadService} from '../../../file-upload.service';
import {Button} from 'primeng/button';
import {NgIf} from '@angular/common';
import {AsyncPipe, NgIf} from '@angular/common';
import {DropdownModule} from 'primeng/dropdown';
import {FormsModule} from '@angular/forms';
import {ToastService} from '../../../toast.service';
import {DynamicDialogRef} from 'primeng/dynamicdialog';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {LibraryService} from '../../service/library.service';
import {Observable} from 'rxjs';
@Component({
selector: 'app-file-upload-v2',
@@ -17,11 +18,12 @@ import {LibraryAndBookService} from '../../service/library-and-book.service';
Button,
NgIf,
DropdownModule,
FormsModule
FormsModule,
AsyncPipe
],
})
export class FileUploadComponent {
libraries: Signal<Library[]>;
libraries$: Observable<Library[]>;
selectedLibrary: Library | null = null;
selectedPath: string = '';
selectedFileName: string = '';
@@ -29,11 +31,11 @@ export class FileUploadComponent {
errorMessage: string | null = null;
constructor(
private libraryBookService: LibraryAndBookService,
private libraryService: LibraryService,
private fileUploadService: FileUploadService,
private toastService: ToastService,
private dynamicDialogRef: DynamicDialogRef) {
this.libraries = this.libraryBookService.getLibraries();
this.libraries$ = this.libraryService.libraries$;
}
triggerFileInput(fileInput: HTMLInputElement): void {
@@ -6,15 +6,19 @@
</ul>
<ul class="layout-menu">
<ng-container *ngFor="let item of libraryMenu(); let i = index;">
<li app-menuitem *ngIf="!item.separator" [item]="item" [index]="i" [root]="true"></li>
<li *ngIf="item.separator" class="menu-separator"></li>
<ng-container *ngIf="libraryMenu$ | async as libraryMenu">
<ng-container *ngFor="let item of libraryMenu; let i = index;">
<li app-menuitem *ngIf="!item.separator" [item]="item" [index]="i" [root]="true"></li>
<li *ngIf="item.separator" class="menu-separator"></li>
</ng-container>
</ng-container>
</ul>
<ul class="layout-menu">
<ng-container *ngFor="let item of shelfMenu(); let i = index;">
<li app-menuitem *ngIf="!item.separator" [item]="item" [index]="i" [root]="true"></li>
<li *ngIf="item.separator" class="menu-separator"></li>
<ng-container *ngIf="shelfMenu$ | async as shelfMenu">
<ng-container *ngFor="let item of shelfMenu; let i = index;">
<li app-menuitem *ngIf="!item.separator" [item]="item" [index]="i" [root]="true"></li>
<li *ngIf="item.separator" class="menu-separator"></li>
</ng-container>
</ng-container>
</ul>
@@ -1,21 +1,54 @@
import {Component, computed, OnInit} from '@angular/core';
import {AppMenuitemComponent} from './app.menuitem.component';
import {NgForOf, NgIf} from '@angular/common';
import {AsyncPipe, NgForOf, NgIf} from '@angular/common';
import {MenuModule} from 'primeng/menu';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {LibraryService} from '../../service/library.service';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {ShelfService} from '../../service/shelf.service';
@Component({
selector: 'app-menu',
imports: [AppMenuitemComponent, NgIf, NgForOf, MenuModule],
imports: [AppMenuitemComponent, NgIf, NgForOf, MenuModule, AsyncPipe],
templateUrl: './app.menu.component.html',
})
export class AppMenuComponent implements OnInit {
home: any[] = [];
libraryMenu$: Observable<any> | undefined;
shelfMenu$: Observable<any> | undefined;
constructor(private libraryBookService: LibraryAndBookService) {
constructor(private libraryService: LibraryService, private shelfService: ShelfService) {
}
ngOnInit() {
this.libraryMenu$ = this.libraryService.libraries$.pipe(
map((libraries) => [
{
label: 'Library',
separator: false,
items: libraries.map((library) => ({
label: library.name,
icon: 'pi pi-fw pi-book',
routerLink: [`/library/${library.id}/books`],
})),
},
])
);
this.shelfMenu$ = this.shelfService.shelves$.pipe(
map((shelves) => [
{
label: 'Shelves',
separator: false,
items: shelves.map((shelf) => ({
label: shelf.name,
icon: 'pi pi-fw pi-heart',
routerLink: [`/shelf/${shelf.id}/books`],
})),
},
])
)
this.populateHome();
}
@@ -24,39 +57,10 @@ export class AppMenuComponent implements OnInit {
{
label: 'Home',
items: [
{label: 'Dashboard', icon: 'pi pi-fw pi-home', routerLink: ['/']},
{label: 'Dashboard', icon: 'pi pi-fw pi-home', routerLink: ['/']}
],
},
];
}
libraryMenu = computed(() => {
const libraries = this.libraryBookService.getLibraries()();
return [
{
label: 'Library',
separator: false,
items: libraries.map((lib) => ({
label: lib.name,
icon: 'pi pi-fw pi-book',
routerLink: [`/library/${lib.id}/books`],
})),
},
];
});
shelfMenu = computed(() => {
const shelves = this.libraryBookService.getShelves()();
return [
{
label: 'Shelves',
separator: false,
items: shelves.map((shelf) => ({
label: shelf.name,
icon: 'pi pi-fw pi-heart',
routerLink: [`/shelf/${shelf.id}/books`],
})),
},
];
});
}
@@ -3,7 +3,7 @@ import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {DirectoryPickerComponent} from '../directory-picker/directory-picker.component';
import {MessageService} from 'primeng/api';
import {Router} from '@angular/router';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {LibraryService} from '../../service/library.service';
@Component({
selector: 'app-library-creator',
@@ -20,7 +20,7 @@ export class LibraryCreatorComponent {
constructor(
private dialogService: DialogService,
private dynamicDialogRef: DynamicDialogRef,
private libraryAndBookService: LibraryAndBookService,
private libraryService: LibraryService,
private router: Router,
) {
}
@@ -55,7 +55,7 @@ export class LibraryCreatorComponent {
name: this.value,
paths: this.folders,
};
this.libraryAndBookService.createLibrary(newLibrary).subscribe({
this.libraryService.createLibrary(newLibrary).subscribe({
next: (createdLibrary) => {
this.router.navigate(
['/library', createdLibrary.id, 'books']
@@ -1,7 +1,7 @@
import { ChangeDetectorRef, Component, NgZone, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NgxExtendedPdfViewerModule, ScrollModeType } from 'ngx-extended-pdf-viewer';
import { LibraryAndBookService } from '../../service/library-and-book.service';
import {Component, NgZone, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {NgxExtendedPdfViewerModule, ScrollModeType} from 'ngx-extended-pdf-viewer';
import {BookService} from '../../service/book.service';
@Component({
selector: 'app-pdf-viewer',
@@ -22,7 +22,7 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
private isInitialLoad = true;
constructor(
private bookService: LibraryAndBookService,
private bookService: BookService,
private zone: NgZone,
private route: ActivatedRoute
) {}
@@ -40,7 +40,7 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
}
private loadBook(bookId: number): void {
this.bookService.getBook(bookId).subscribe((book) => {
this.bookService.getBookById(bookId).subscribe((book) => {
this.zone.run(() => {
this.bookService.getBookSetting(bookId).subscribe((bookSetting) => {
const { pageNumber, zoom, sidebar_visible, spread } = bookSetting;
@@ -1,13 +1,13 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { debounceTime, distinctUntilChanged, switchMap, catchError } from 'rxjs/operators';
import { Book } from '../../model/book.model';
import { LibraryAndBookService } from '../../service/library-and-book.service';
import { FormsModule } from '@angular/forms';
import { InputTextModule } from 'primeng/inputtext';
import { NgForOf, NgIf } from '@angular/common';
import { ButtonDirective } from 'primeng/button';
import {Component, OnDestroy, OnInit} from '@angular/core';
import {Subject, Subscription} from 'rxjs';
import {catchError, debounceTime, distinctUntilChanged, switchMap} from 'rxjs/operators';
import {Book} from '../../model/book.model';
import {FormsModule} from '@angular/forms';
import {InputTextModule} from 'primeng/inputtext';
import {NgForOf, NgIf} from '@angular/common';
import {ButtonDirective} from 'primeng/button';
import {Router} from '@angular/router';
import {BookService} from '../../service/book.service';
@Component({
selector: 'app-search',
@@ -27,7 +27,7 @@ export class SearchComponent implements OnInit, OnDestroy {
#searchSubject = new Subject<string>();
#subscription!: Subscription;
constructor(private bookService: LibraryAndBookService, private router: Router) {}
constructor(private bookService: BookService, private router: Router) {}
ngOnInit(): void {
this.initializeSearch();
@@ -1,6 +1,6 @@
<div class="dialog-container">
<div class="checkbox-list">
<div *ngFor="let shelf of shelves()" class="flex items-center">
<div *ngFor="let shelf of shelves$ | async" class="flex items-center">
<p-checkbox [inputId]="shelf.name" name="group" [value]="shelf" [(ngModel)]="selectedShelves" />
<label [for]="shelf.id" class="ml-2">{{ shelf.name }}</label>
</div>
@@ -1,8 +1,10 @@
import {Component, computed, OnInit} from '@angular/core';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {Component, OnInit} from '@angular/core';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Book} from '../../model/book.model';
import {Book, Shelf} from '../../model/book.model';
import {MessageService} from 'primeng/api';
import {ShelfService} from '../../service/shelf.service';
import {Observable} from 'rxjs';
import {BookService} from '../../service/book.service';
@Component({
selector: 'app-shelf-assigner',
@@ -13,29 +15,31 @@ import {MessageService} from 'primeng/api';
})
export class ShelfAssignerComponent implements OnInit {
shelves;
shelves$: Observable<Shelf[]>;
book: Book;
selectedShelves: any[] = [];
displayShelfDialog: boolean = false;
shelfName: string = '';
constructor(
private libraryBookService: LibraryAndBookService, private dynamicDialogConfig: DynamicDialogConfig,
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService) {
this.shelves = this.libraryBookService.getShelves();
private shelfService: ShelfService, private dynamicDialogConfig: DynamicDialogConfig,
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService,
private bookService: BookService) {
this.shelves$ = this.shelfService.shelves$;
this.book = this.dynamicDialogConfig.data.book;
}
ngOnInit(): void {
if (this.book.shelves) {
const bookShelfIds = this.book.shelves.map(shelf => shelf.id);
this.selectedShelves = this.shelves().filter(shelf => bookShelfIds.includes(shelf.id));
}
this.shelves$.subscribe(shelves => {
if (this.book.shelves) {
const bookShelfIds = this.book.shelves.map(shelf => shelf.id);
this.selectedShelves = shelves.filter(shelf => bookShelfIds.includes(shelf.id));
}
});
}
saveNewShelf() {
this.libraryBookService.createShelf(this.shelfName).subscribe(
this.shelfService.createShelf(this.shelfName).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf created: ' + this.shelfName});
this.displayShelfDialog = false;
@@ -49,7 +53,7 @@ export class ShelfAssignerComponent implements OnInit {
updateBooksShelves() {
const shelfIds = this.selectedShelves.map((shelf) => shelf.id);
this.libraryBookService.assignShelvesToBook(this.book, shelfIds).subscribe(
this.bookService.assignShelvesToBook(this.book, shelfIds).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
this.dynamicDialogRef.close();