Load books only when needed not in constructor

This commit is contained in:
aditya.chandel
2025-01-17 22:03:12 -07:00
parent 42ccd2ce4d
commit ad741440b4
4 changed files with 15 additions and 9 deletions
-1
View File
@@ -21,7 +21,6 @@ export class AppComponent implements OnInit {
private rxStompService = inject(RxStompService);
private eventService = inject(EventService);
ngOnInit(): void {
this.rxStompService.watch('/topic/book-add').subscribe((message: Message) => {
@@ -91,6 +91,7 @@ export class BookBrowserComponent implements OnInit {
private libraryShelfMenuService = inject(LibraryShelfMenuService);
ngOnInit(): void {
this.bookService.loadBooks();
this.sortOptions = SortService.generateSortOptions();
const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books';
@@ -20,11 +20,11 @@ export class BookService {
private http = inject(HttpClient);
constructor() {
this.loadBooks();
}
private loadBooks(): void {
loadBooks(): void {
const currentState = this.bookStateSubject.value;
if (currentState.loaded) {
return;
}
this.http.get<Book[]>(this.url).pipe(
tap(books => {
this.bookStateSubject.next({
@@ -1,4 +1,4 @@
import {Component, inject} from '@angular/core';
import {Component, inject, OnInit} from '@angular/core';
import {LibraryCreatorComponent} from '../../../book/components/library-creator/library-creator.component';
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {LibraryService} from '../../../book/service/library.service';
@@ -7,6 +7,7 @@ import {map} from 'rxjs/operators';
import {Button} from 'primeng/button';
import {AsyncPipe, NgIf} from '@angular/common';
import {DashboardScrollerComponent} from '../dashboard-scroller/dashboard-scroller.component';
import {BookService} from '../../../book/service/book.service';
@Component({
selector: 'app-main-dashboard',
@@ -20,10 +21,15 @@ import {DashboardScrollerComponent} from '../dashboard-scroller/dashboard-scroll
],
providers: [DialogService],
})
export class MainDashboardComponent {
export class MainDashboardComponent implements OnInit {
ref: DynamicDialogRef | undefined;
private bookService = inject(BookService);
ngOnInit(): void {
this.bookService.loadBooks();
}
isLibrariesEmpty$: Observable<boolean> = inject(LibraryService).libraryState$.pipe(
map(state => !state.libraries || state.libraries.length === 0)
);