Load books only when needed not in constructor
This commit is contained in:
@@ -21,7 +21,6 @@ export class AppComponent implements OnInit {
|
|||||||
private rxStompService = inject(RxStompService);
|
private rxStompService = inject(RxStompService);
|
||||||
private eventService = inject(EventService);
|
private eventService = inject(EventService);
|
||||||
|
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
|
||||||
this.rxStompService.watch('/topic/book-add').subscribe((message: Message) => {
|
this.rxStompService.watch('/topic/book-add').subscribe((message: Message) => {
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ export class BookBrowserComponent implements OnInit {
|
|||||||
private libraryShelfMenuService = inject(LibraryShelfMenuService);
|
private libraryShelfMenuService = inject(LibraryShelfMenuService);
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
this.bookService.loadBooks();
|
||||||
this.sortOptions = SortService.generateSortOptions();
|
this.sortOptions = SortService.generateSortOptions();
|
||||||
const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books';
|
const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books';
|
||||||
|
|
||||||
|
|||||||
@@ -20,11 +20,11 @@ export class BookService {
|
|||||||
|
|
||||||
private http = inject(HttpClient);
|
private http = inject(HttpClient);
|
||||||
|
|
||||||
constructor() {
|
loadBooks(): void {
|
||||||
this.loadBooks();
|
const currentState = this.bookStateSubject.value;
|
||||||
}
|
if (currentState.loaded) {
|
||||||
|
return;
|
||||||
private loadBooks(): void {
|
}
|
||||||
this.http.get<Book[]>(this.url).pipe(
|
this.http.get<Book[]>(this.url).pipe(
|
||||||
tap(books => {
|
tap(books => {
|
||||||
this.bookStateSubject.next({
|
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 {LibraryCreatorComponent} from '../../../book/components/library-creator/library-creator.component';
|
||||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||||
import {LibraryService} from '../../../book/service/library.service';
|
import {LibraryService} from '../../../book/service/library.service';
|
||||||
@@ -7,6 +7,7 @@ import {map} from 'rxjs/operators';
|
|||||||
import {Button} from 'primeng/button';
|
import {Button} from 'primeng/button';
|
||||||
import {AsyncPipe, NgIf} from '@angular/common';
|
import {AsyncPipe, NgIf} from '@angular/common';
|
||||||
import {DashboardScrollerComponent} from '../dashboard-scroller/dashboard-scroller.component';
|
import {DashboardScrollerComponent} from '../dashboard-scroller/dashboard-scroller.component';
|
||||||
|
import {BookService} from '../../../book/service/book.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-main-dashboard',
|
selector: 'app-main-dashboard',
|
||||||
@@ -20,10 +21,15 @@ import {DashboardScrollerComponent} from '../dashboard-scroller/dashboard-scroll
|
|||||||
],
|
],
|
||||||
providers: [DialogService],
|
providers: [DialogService],
|
||||||
})
|
})
|
||||||
export class MainDashboardComponent {
|
export class MainDashboardComponent implements OnInit {
|
||||||
|
|
||||||
ref: DynamicDialogRef | undefined;
|
ref: DynamicDialogRef | undefined;
|
||||||
|
|
||||||
|
private bookService = inject(BookService);
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
this.bookService.loadBooks();
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user