11import { Component , OnInit } from "@angular/core" ;
22import { Store } from "@ngrx/store" ;
3- import { State } from "src/app/shared/state " ;
3+ import { Observable } from "rxjs " ;
44import {
5- BookModel ,
6- calculateBooksGrossEarnings ,
7- BookRequiredProps
8- } from "src/app/shared/models/book.model" ;
5+ State ,
6+ selectAllBooks ,
7+ selectActiveBook ,
8+ selectBooksEarningsTotals
9+ } from "src/app/shared/state" ;
10+ import { BookModel , BookRequiredProps } from "src/app/shared/models/book.model" ;
911import { BooksService } from "src/app/shared/services/book.service" ;
1012import { BooksPageActions , BooksApiActions } from "../../actions" ;
1113
@@ -15,39 +17,30 @@ import { BooksPageActions, BooksApiActions } from "../../actions";
1517 styleUrls : [ "./books-page.component.css" ]
1618} )
1719export class BooksPageComponent implements OnInit {
18- books : BookModel [ ] = [ ] ;
19- currentBook : BookModel | null = null ;
20- total : number = 0 ;
21-
22- constructor (
23- private booksService : BooksService ,
24- private store : Store < State >
25- ) { }
20+ books$ : Observable < BookModel [ ] > ;
21+ currentBook$ : Observable < BookModel | null > ;
22+ total$ : Observable < number > ;
23+
24+ constructor ( private booksService : BooksService , private store : Store < State > ) {
25+ this . books$ = store . select ( selectAllBooks ) ;
26+ this . currentBook$ = store . select ( selectActiveBook ) ;
27+ this . total$ = store . select ( selectBooksEarningsTotals ) ;
28+ }
2629
2730 ngOnInit ( ) {
2831 this . store . dispatch ( BooksPageActions . enter ( ) ) ;
2932
3033 this . getBooks ( ) ;
31- this . removeSelectedBook ( ) ;
3234 }
3335
3436 getBooks ( ) {
3537 this . booksService . all ( ) . subscribe ( books => {
36- this . books = books ;
37- this . updateTotals ( books ) ;
38-
3938 this . store . dispatch ( BooksApiActions . booksLoaded ( { books } ) ) ;
4039 } ) ;
4140 }
4241
43- updateTotals ( books : BookModel [ ] ) {
44- this . total = calculateBooksGrossEarnings ( books ) ;
45- }
46-
4742 onSelect ( book : BookModel ) {
4843 this . store . dispatch ( BooksPageActions . selectBook ( { bookId : book . id } ) ) ;
49-
50- this . currentBook = book ;
5144 }
5245
5346 onCancel ( ) {
@@ -56,8 +49,6 @@ export class BooksPageComponent implements OnInit {
5649
5750 removeSelectedBook ( ) {
5851 this . store . dispatch ( BooksPageActions . clearSelectedBook ( ) ) ;
59-
60- this . currentBook = null ;
6152 }
6253
6354 onSave ( book : BookRequiredProps | BookModel ) {
@@ -85,9 +76,6 @@ export class BooksPageComponent implements OnInit {
8576 ) ;
8677
8778 this . booksService . update ( book . id , book ) . subscribe ( book => {
88- this . getBooks ( ) ;
89- this . removeSelectedBook ( ) ;
90-
9179 this . store . dispatch ( BooksApiActions . bookUpdated ( { book } ) ) ;
9280 } ) ;
9381 }
0 commit comments