Generalize DashboardScrollerComponent and display Latest Added Books
This commit is contained in:
+2
-2
@@ -1,5 +1,5 @@
|
||||
<div class="book-list-container">
|
||||
<h2 class="last-read-title">Last Read</h2>
|
||||
<h2 class="last-read-title">{{ title }}</h2>
|
||||
<div class="book-list"
|
||||
infiniteScroll
|
||||
[infiniteScrollDistance]="2"
|
||||
@@ -7,6 +7,7 @@
|
||||
(scrolled)="loadMore()"
|
||||
#scrollContainer
|
||||
(scroll)="onScroll($event)">
|
||||
|
||||
<div class="book-item" *ngFor="let book of books">
|
||||
<img [src]="coverImageSrc(book.id)" class="book-cover" alt="Cover of {{ book.title }}" loading="lazy"/>
|
||||
<div class="book-info">
|
||||
@@ -17,4 +18,3 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
+34
-16
@@ -1,9 +1,9 @@
|
||||
import { Component, OnInit, ViewChild, ElementRef } 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 {Component, OnInit, ViewChild, ElementRef, Input} 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';
|
||||
|
||||
@Component({
|
||||
selector: 'app-dashboard-scroller',
|
||||
@@ -16,27 +16,45 @@ import { NgForOf } from '@angular/common';
|
||||
styleUrls: ['./dashboard-scroller.component.scss']
|
||||
})
|
||||
export class DashboardScrollerComponent implements OnInit {
|
||||
|
||||
@Input() bookListType: 'lastRead' | 'latestAdded' = 'lastRead';
|
||||
@Input() title: string = 'Last Read Books';
|
||||
|
||||
|
||||
books: Book[] = [];
|
||||
private currentPage: number = 0;
|
||||
|
||||
@ViewChild('scrollContainer') scrollContainer!: ElementRef;
|
||||
|
||||
constructor(private bookService: BookService) {}
|
||||
constructor(private bookService: BookService) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loadBooks();
|
||||
}
|
||||
|
||||
loadBooks(): void {
|
||||
this.bookService.loadLatestBooks(this.currentPage).subscribe({
|
||||
next: (response) => {
|
||||
this.books = [...this.books, ...response.content];
|
||||
this.currentPage++;
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Error loading books:', err);
|
||||
},
|
||||
});
|
||||
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);
|
||||
},
|
||||
});
|
||||
} 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);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
coverImageSrc(bookId: number): string {
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
</div>
|
||||
|
||||
<ng-template #dashboardScroller>
|
||||
<app-dashboard-scroller></app-dashboard-scroller>
|
||||
<app-dashboard-scroller [bookListType]="'lastRead'" title="Last Read Books"></app-dashboard-scroller>
|
||||
<app-dashboard-scroller [bookListType]="'latestAdded'" title="Latest Added Books"></app-dashboard-scroller>
|
||||
</ng-template>
|
||||
|
||||
</div>
|
||||
|
||||
@@ -30,6 +30,13 @@ export class BookService {
|
||||
);
|
||||
}
|
||||
|
||||
loadLatestAddedBooks(page: number): Observable<PaginatedBooksResponse> {
|
||||
return this.http.get<PaginatedBooksResponse>(
|
||||
`${this.bookUrl}?page=${page}&size=10&sortBy=addedOn&sortDir=desc`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
searchBooks(query: string): Observable<Book[]> {
|
||||
if (query.length < 2) {
|
||||
return new Observable<Book[]>();
|
||||
|
||||
Reference in New Issue
Block a user