Try fixing dashboard spinner

This commit is contained in:
aditya.chandel
2025-03-08 11:52:24 -07:00
parent d705c7c04e
commit b48a79bd64
4 changed files with 25 additions and 20 deletions
@@ -1,12 +1,17 @@
<p class="dashboard-scroller-title">{{ title }}</p> <p class="dashboard-scroller-title">{{ title }}</p>
<div class="dashboard-scroller-no-books" *ngIf="!books || books.length === 0"> <div *ngIf="isLoading">
<p-progressSpinner></p-progressSpinner>
</div>
<div class="dashboard-scroller-no-books" *ngIf="!isLoading && (!books || books.length === 0)">
<p class="text-sm"> <p class="text-sm">
{{ bookListType === 'lastRead' ? "You haven't read any books yet!" : "No books have been added to BookLore!" }} {{ bookListType === 'lastRead' ? "You haven't read any books yet!" : "No books have been added to BookLore!" }}
</p> </p>
</div> </div>
<div class="dashboard-scroller-infinite" <div class="dashboard-scroller-infinite"
*ngIf="!isLoading && books"
infiniteScroll infiniteScroll
[infiniteScrollDistance]="2" [infiniteScrollDistance]="2"
[infiniteScrollThrottle]="50" [infiniteScrollThrottle]="50"
@@ -19,10 +24,6 @@
</div> </div>
<div *ngIf="!books"> <div *ngIf="!isLoading && books === null">
<p-progressSpinner></p-progressSpinner>
</div>
<div *ngIf="books === null">
<p class="text-sm">Failed to load books! :(</p> <p class="text-sm">Failed to load books! :(</p>
</div> </div>
@@ -1,4 +1,4 @@
import {Component, ElementRef, Input, OnInit, ViewChild} from '@angular/core'; import {Component, ElementRef, Input, OnChanges, OnInit, ViewChild} from '@angular/core';
import {BookCardComponent} from '../../../book/components/book-browser/book-card/book-card.component'; import {BookCardComponent} from '../../../book/components/book-browser/book-card/book-card.component';
import {InfiniteScrollDirective} from 'ngx-infinite-scroll'; import {InfiniteScrollDirective} from 'ngx-infinite-scroll';
import {NgForOf, NgIf} from '@angular/common'; import {NgForOf, NgIf} from '@angular/common';
@@ -18,7 +18,7 @@ import {Book} from '../../../book/model/book.model';
], ],
standalone: true standalone: true
}) })
export class DashboardScrollerComponent implements OnInit { export class DashboardScrollerComponent implements OnInit, OnChanges {
@Input() bookListType: 'lastRead' | null = null; @Input() bookListType: 'lastRead' | null = null;
@Input() title: string = 'Last Read Books'; @Input() title: string = 'Last Read Books';
@@ -27,6 +27,17 @@ export class DashboardScrollerComponent implements OnInit {
@ViewChild('scrollContainer') scrollContainer!: ElementRef; @ViewChild('scrollContainer') scrollContainer!: ElementRef;
isLoading = true;
ngOnInit(): void { ngOnInit(): void {
if (this.books !== undefined) {
this.isLoading = false;
}
}
ngOnChanges(): void {
if (this.books !== undefined) {
this.isLoading = false;
}
} }
} }
@@ -34,7 +34,7 @@
</div> </div>
<app-dashboard-scroller <app-dashboard-scroller
[books]="randomBooks" [books]="randomBooks$ | async"
[title]="'Discover Random Reads'"> [title]="'Discover Random Reads'">
</app-dashboard-scroller> </app-dashboard-scroller>
@@ -34,7 +34,7 @@ export class MainDashboardComponent implements OnInit {
lastReadBooks$: Observable<Book[]> | undefined; lastReadBooks$: Observable<Book[]> | undefined;
latestAddedBooks$: Observable<Book[]> | undefined; latestAddedBooks$: Observable<Book[]> | undefined;
randomBooks!: Book[]; randomBooks$: Observable<Book[]> | undefined;
isLibrariesEmpty$: Observable<boolean> = inject(LibraryService).libraryState$.pipe( isLibrariesEmpty$: Observable<boolean> = inject(LibraryService).libraryState$.pipe(
map(state => !state.libraries || state.libraries.length === 0) map(state => !state.libraries || state.libraries.length === 0)
@@ -68,16 +68,9 @@ export class MainDashboardComponent implements OnInit {
)) ))
); );
this.bookService.bookState$.pipe( this.randomBooks$ = this.bookService.bookState$.pipe(
filter((state): state is BookState => !!state && !!state.books && state.books.length > 0), map((state: BookState) => this.getRandomBooks(state.books || [], 15))
take(1), );
map((state) => {
const books = state.books || [];
return this.getRandomBooks(books, 15);
})
).subscribe((randomBooks) => {
this.randomBooks = randomBooks;
});
} }
private getRandomBooks(books: Book[], count: number): Book[] { private getRandomBooks(books: Book[], count: number): Book[] {