From ad741440b4e5865f4d1fb771107c2c8e58a20426 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Fri, 17 Jan 2025 22:03:12 -0700 Subject: [PATCH] Load books only when needed not in constructor --- booklore-ui/src/app/app.component.ts | 1 - .../book-browser/book-browser.component.ts | 1 + booklore-ui/src/app/book/service/book.service.ts | 10 +++++----- .../main-dashboard/main-dashboard.component.ts | 12 +++++++++--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/booklore-ui/src/app/app.component.ts b/booklore-ui/src/app/app.component.ts index 1375b5d0b..43b8740af 100644 --- a/booklore-ui/src/app/app.component.ts +++ b/booklore-ui/src/app/app.component.ts @@ -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) => { diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts index 428c2f0c4..fa7d927cb 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts @@ -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'; diff --git a/booklore-ui/src/app/book/service/book.service.ts b/booklore-ui/src/app/book/service/book.service.ts index 1a28a97bf..0fc7686c3 100644 --- a/booklore-ui/src/app/book/service/book.service.ts +++ b/booklore-ui/src/app/book/service/book.service.ts @@ -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(this.url).pipe( tap(books => { this.bookStateSubject.next({ diff --git a/booklore-ui/src/app/dashboard/components/main-dashboard/main-dashboard.component.ts b/booklore-ui/src/app/dashboard/components/main-dashboard/main-dashboard.component.ts index 3f193af2a..613e3ca8c 100644 --- a/booklore-ui/src/app/dashboard/components/main-dashboard/main-dashboard.component.ts +++ b/booklore-ui/src/app/dashboard/components/main-dashboard/main-dashboard.component.ts @@ -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 = inject(LibraryService).libraryState$.pipe( map(state => !state.libraries || state.libraries.length === 0) );