Revamp state management

This commit is contained in:
aditya.chandel
2024-12-22 11:57:06 -07:00
parent e343e7e7f3
commit 4704d9f5f0
17 changed files with 624 additions and 419 deletions
@@ -1,12 +1,12 @@
<div class="book-card">
<div class="cover-container">
<img [src]="coverImageSrc(book)" class="book-cover placeholder" alt="Cover of {{ book.metadata.title }}" loading="lazy" />
<img [src]="coverImageSrc(book)" class="book-cover placeholder" alt="Cover of {{ book.metadata?.title }}" loading="lazy" />
<p-button [rounded]="true" icon="pi pi-info" class="read-btn" (click)="openBookInfo(book)"></p-button>
<p-button [rounded]="true" icon="pi pi-book" class="info-btn" (click)="readBook(book)"></p-button>
</div>
<div class="book-info">
<div class="book-title-container">
<h4 class="book-title">{{ book.metadata.title }}</h4>
<h4 class="book-title">{{ book.metadata?.title }}</h4>
<p-menu #menu [model]="items" [popup]="true" appendTo="body" />
<p-button [text]="true" (click)="menu.toggle($event)" icon="pi pi-ellipsis-v" />
</div>
@@ -65,7 +65,7 @@ export class BookCardComponent implements OnInit {
private openShelfDialog(book: Book) {
this.dialogService.open(ShelfAssignerComponent, {
header: 'Update Shelves: ' + book.metadata.title,
header: 'Update Shelves: ' + book.metadata?.title,
modal: true,
width: '30%',
height: '70%',
@@ -82,7 +82,7 @@ export class BookMetadataComponent implements OnInit, OnDestroy {
data: {
bookId: bookId,
libraryId: libraryId,
bookTitle: this.book?.metadata.title,
bookTitle: this.book?.metadata?.title,
},
});
@@ -1,6 +1,6 @@
<div class="library-header">
<div class="library-header-dial">
<h3 class="library-name">{{ currentEntityName() }}</h3>
<h3 class="library-name">{{ title() }}</h3>
<p-speedDial
[model]="items!"
direction="right"
@@ -23,7 +23,7 @@
<div class="content-container">
<div class="card-container">
<virtual-scroller class="virtual-scroller" #scroll [items]="currentEntityBooks()">
<virtual-scroller 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>
@@ -1,7 +1,8 @@
import { Component, OnInit, computed, signal } 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 {Component, OnInit, computed, signal, Signal} 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";
@Component({
selector: 'app-books-browser',
@@ -10,27 +11,27 @@ import { ConfirmationService, MenuItem, MessageService } from 'primeng/api';
styleUrls: ['./books-browser.component.scss']
})
export class BooksBrowserComponent implements OnInit {
private currentEntityId = signal<number | null>(null);
private entityType = signal<'library' | 'shelf'>('library'); // Default to library
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';
}
if (type === 'shelf') {
const shelf = this.libraryBookService.getShelves()().find(shelf => shelf.id === entityId);
return shelf ? `Shelf: ${shelf.name}` : 'Shelf not found';
}
return '';
});
items: MenuItem[] | undefined;
protected currentEntityBooks = computed(() => {
if (this.currentEntityId() != null) {
return this.entityType() === 'library'
? this.libraryBookService.getLibraryBooks(this.currentEntityId()!)()
: this.libraryBookService.getShelfBooks(this.currentEntityId()!)();
}
return [];
});
protected currentEntityName = computed(() => {
if (this.entityType() === 'library') {
return 'Library: ' + (this.libraryBookService.libraries().find(library => library.id === this.currentEntityId())?.name || 'Library not found');
}
return 'Shelf: ' + (this.libraryBookService.shelves().find(shelf => shelf.id === this.currentEntityId())?.name || 'Shelf not found');
});
constructor(
private activatedRoute: ActivatedRoute,
@@ -38,70 +39,35 @@ export class BooksBrowserComponent implements OnInit {
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.libraryBookService.loadLibraryBooks(libraryId);
this.books = this.libraryBookService.getLibraryBooks(libraryId);
this.libraryBookService.getLibraries();
} else if (shelfId) {
this.entityType.set('shelf');
this.currentEntityId.set(shelfId);
this.libraryBookService.loadLibraryBooks(shelfId);
this.books = this.libraryBookService.getShelfBooks(shelfId);
this.libraryBookService.getShelves();
}
});
if (this.entityType() === 'library') {
this.initializeLibraryMenuItems();
} else if (this.entityType() === 'shelf') {
//this.initializeShelfMenuItems();
this.initializeShelfMenuItems();
}
}
private initializeLibraryMenuItems(): void {
this.items = [
{
icon: 'pi pi-pencil',
tooltipOptions: {
tooltipLabel: 'Rename',
tooltipPosition: 'top'
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete: ' + this.currentEntityName() + "?",
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
});
}
});
}
});
}
},
{
icon: 'pi pi-trash',
tooltipOptions: {
@@ -110,7 +76,7 @@ export class BooksBrowserComponent implements OnInit {
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete: ' + this.currentEntityName() + "?",
message: 'Sure you want to delete ' + this.title() + "?",
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
acceptIcon: 'none',
@@ -142,45 +108,8 @@ export class BooksBrowserComponent implements OnInit {
];
}
/*private initializeShelfMenuItems(): void {
private initializeShelfMenuItems(): void {
this.items = [
{
icon: 'pi pi-pencil',
tooltipOptions: {
tooltipLabel: 'Rename',
tooltipPosition: 'top'
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete: ' + this.currentEntityName() + "?",
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
});
}
});
}
});
}
},
{
icon: 'pi pi-trash',
tooltipOptions: {
@@ -189,14 +118,14 @@ export class BooksBrowserComponent implements OnInit {
},
command: () => {
this.confirmationService.confirm({
message: 'Sure you want to delete: ' + this.currentEntityName() + "?",
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({
this.libraryBookService.deleteShelf(this.currentEntityId()!).subscribe({
complete: () => {
this.router.navigate(['/']);
this.messageService.add({
@@ -219,5 +148,5 @@ export class BooksBrowserComponent implements OnInit {
}
}
];
}*/
}
}
@@ -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="booksSignal!().length === 0">
<h4>No books available.</h4>
</div>
</div>
@@ -12,10 +12,10 @@
[infiniteScrollThrottle]="50"
#scrollContainer
[horizontal]="true">
<div class="book-item" *ngFor="let book of booksSignal()">
<img [src]="coverImageSrc(book.id)" class="book-cover" alt="Cover of {{ book.metadata.title }}" loading="lazy"/>
<div class="book-item" *ngFor="let book of booksSignal!()">
<img [src]="coverImageSrc(book.id)" class="book-cover" alt="Cover of {{ book.metadata?.title }}" loading="lazy"/>
<div class="book-info">
<h4>{{ book.metadata.title }}</h4>
<h4>{{ book.metadata?.title }}</h4>
<p>{{ getAuthorNames(book) }}</p>
<p-button [rounded]="true" icon="pi pi-eye" class="view-btn" (click)="openBook(book)"></p-button>
<p-button [rounded]="true" icon="pi pi-info" class="read-btn" (click)="openBookInfo(book.id, book.libraryId)"></p-button>
@@ -1,4 +1,4 @@
import {Component, OnInit, ViewChild, ElementRef, Input, computed, signal} from '@angular/core';
import {Component, OnInit, ViewChild, ElementRef, Input, computed, signal, Signal} from '@angular/core';
import {Book} from '../../model/book.model';
import {LibraryAndBookService} from '../../service/library-and-book.service';
import {Button} from 'primeng/button';
@@ -23,21 +23,20 @@ export class DashboardScrollerComponent implements OnInit {
@Input() title: string = 'Last Read Books';
@ViewChild('scrollContainer') scrollContainer!: ElementRef;
booksSignal = computed(() => {
return this.bookListType === 'lastRead' ? this.bookService.lastReadBooks() : this.bookService.latestAddedBooks();
});
booksSignal: Signal<Book[]> | undefined;
constructor(private bookService: LibraryAndBookService, private router: Router) {
}
ngOnInit(): void {
this.booksSignal = this.bookListType === 'lastRead' ? this.bookService.getLastReadBooks() : this.bookService.getLatestAddedBooks();;
this.loadBooks();
}
loadBooks(): void {
if (this.bookListType === 'lastRead' && !this.bookService.lastReadBooksLoaded) {
if (this.bookListType === 'lastRead' && !this.bookService.getLastReadBooks()) {
this.bookService.getLastReadBooks();
} else if (this.bookListType === 'latestAdded' && !this.bookService.latestAddedBooksLoaded) {
} else if (this.bookListType === 'latestAdded' && !this.bookService.getLatestAddedBooks()) {
this.bookService.getLatestAddedBooks();
}
}
@@ -47,7 +46,7 @@ export class DashboardScrollerComponent implements OnInit {
}
getAuthorNames(book: Book): string {
return book.metadata.authors?.map((author) => author.name).join(', ') || 'No authors available';
return book.metadata?.authors?.map((author) => author.name).join(', ') || 'No authors available';
}
openBook(book: Book): void {
@@ -23,7 +23,7 @@ export class DashboardComponent {
ref: DynamicDialogRef | undefined;
constructor(private libraryBookService: LibraryAndBookService, public dialogService: DialogService) {
this.libraries = this.libraryBookService.libraries;
this.libraries = this.libraryBookService.getLibraries();
}
get isLibrariesEmpty(): boolean {
@@ -33,7 +33,7 @@ export class FileUploadComponent {
private fileUploadService: FileUploadService,
private toastService: ToastService,
private dynamicDialogRef: DynamicDialogRef) {
this.libraries = this.libraryBookService.libraries;
this.libraries = this.libraryBookService.getLibraries();
}
triggerFileInput(fileInput: HTMLInputElement): void {
@@ -31,7 +31,7 @@ export class AppMenuComponent implements OnInit {
}
libraryMenu = computed(() => {
const libraries = this.libraryBookService.libraries();
const libraries = this.libraryBookService.getLibraries()();
return [
{
label: 'Library',
@@ -46,7 +46,7 @@ export class AppMenuComponent implements OnInit {
});
shelfMenu = computed(() => {
const shelves = this.libraryBookService.shelves();
const shelves = this.libraryBookService.getShelves()();
return [
{
label: 'Shelves',
@@ -17,7 +17,7 @@
<ng-container *ngIf="books.length > 0; else noResults">
<div class="search-dropdown-item" *ngFor="let book of books" (click)="onBookClick(book)">
<img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="search-book-cover" />
<span class="search-book-name">{{ book.metadata.title }}</span>
<span class="search-book-name">{{ book.metadata?.title }}</span>
</div>
</ng-container>
@@ -13,9 +13,7 @@ import {MessageService} from 'primeng/api';
})
export class ShelfAssignerComponent implements OnInit {
shelves = computed(() => {
return this.libraryBookService.shelves();
});
shelves;
book: Book;
selectedShelves: any[] = [];
@@ -25,6 +23,7 @@ export class ShelfAssignerComponent implements OnInit {
constructor(
private libraryBookService: LibraryAndBookService, private dynamicDialogConfig: DynamicDialogConfig,
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService) {
this.shelves = this.libraryBookService.getShelves();
this.book = this.dynamicDialogConfig.data.book;
}