More signals

This commit is contained in:
aditya.chandel
2024-12-18 16:08:22 -07:00
parent 71f5927364
commit ce515649f6
14 changed files with 160 additions and 129 deletions
@@ -1,14 +1,20 @@
<div class="book-list-container">
<h2 class="last-read-title">{{ title }}</h2>
<div class="last-read-title-container">
<h2 class="last-read-title">{{ title }}</h2>
<div *ngIf="booksSignal().length === 0">
<h4>No books available.</h4>
</div>
</div>
<div class="book-list"
infiniteScroll
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="50"
(scrolled)="loadMore()"
#scrollContainer
[horizontal]="true"
(scrolled)="loadMore()"
(scroll)="onScroll($event)">
<div class="book-item" *ngFor="let book of books">
<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>
@@ -2,6 +2,10 @@
margin-bottom: 20px;
}
.last-read-title-container {
}
.last-read-title {
font-size: 1.5rem;
color: var(--text-color);
@@ -1,31 +1,33 @@
import {Component, OnInit, ViewChild, ElementRef, Input} from '@angular/core';
import {Component, OnInit, ViewChild, ElementRef, Input, computed, signal} from '@angular/core';
import {Book} from '../../model/book.model';
import {BookService} from '../../service/book.service';
import {Button} from 'primeng/button';
import {InfiniteScrollDirective} from 'ngx-infinite-scroll';
import {NgForOf} from '@angular/common';
import {NgForOf, NgIf} from '@angular/common';
import {Router} from '@angular/router';
@Component({
selector: 'app-dashboard-scroller',
templateUrl: './dashboard-scroller.component.html',
styleUrls: ['./dashboard-scroller.component.scss'],
imports: [
Button,
InfiniteScrollDirective,
NgForOf
NgForOf,
NgIf
],
styleUrls: ['./dashboard-scroller.component.scss']
})
export class DashboardScrollerComponent implements OnInit {
@Input() bookListType: 'lastRead' | 'latestAdded' = 'lastRead';
@Input() title: string = 'Last Read Books';
@ViewChild('scrollContainer') scrollContainer!: ElementRef;
books: Book[] = [];
private currentPage: number = 0;
@ViewChild('scrollContainer') scrollContainer!: ElementRef;
booksSignal = computed(() => {
return this.bookListType === 'lastRead' ? this.bookService.lastReadBooks() : this.bookService.latestAddedBooks();
});
constructor(private bookService: BookService, private router: Router) {
}
@@ -36,37 +38,17 @@ export class DashboardScrollerComponent implements OnInit {
loadBooks(): void {
if (this.bookListType === 'lastRead') {
this.bookService.loadLatestBooks(this.currentPage).subscribe({
next: (response) => {
this.books = [...this.books, ...response.content];
this.currentPage++;
},
error: (err) => {
console.error('Error loading books:', err);
},
});
this.bookService.getLastReadBooks(this.currentPage);
} else {
this.bookService.loadLatestAddedBooks(this.currentPage).subscribe({
next: (response) => {
this.books = [...this.books, ...response.content];
this.currentPage++;
},
error: (err) => {
console.error('Error loading books:', err);
},
});
this.bookService.getLatestAddedBooks(this.currentPage);
}
this.currentPage++;
}
coverImageSrc(bookId: number): string {
return this.bookService.getBookCoverUrl(bookId);
}
loadMore(): void {
console.log('Loading more books...');
this.loadBooks();
}
getAuthorNames(book: Book): string {
return book.metadata.authors?.map((author) => author.name).join(', ') || 'No authors available';
}
@@ -76,13 +58,18 @@ export class DashboardScrollerComponent implements OnInit {
window.open(url, '_blank');
}
loadMore() {
/*this.loadBooks();
this.currentPage++;*/
}
onScroll(event: any): void {
const container = this.scrollContainer.nativeElement;
/*const container = this.scrollContainer.nativeElement;
const scrollRight = container.scrollWidth - container.scrollLeft === container.clientWidth;
if (scrollRight) {
this.loadMore();
}
this.currentPage++;
this.loadBooks();
}*/
}
openBookInfo(bookId: number, libraryId: number) {