Skip to content

Commit 6715c7c

Browse files
committed
08-reading-state
1 parent dc2894f commit 6715c7c

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

src/app/books/components/books-page/books-page.component.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<div class="container">
22
<div class="col-50">
3-
<app-books-total [total]="total"> </app-books-total>
3+
<app-books-total [total]="total$ | async"> </app-books-total>
44

55
<app-books-list
6-
[books]="books"
6+
[books]="books$ | async"
77
(select)="onSelect($event)"
88
(delete)="onDelete($event)"
99
>
@@ -12,7 +12,7 @@
1212

1313
<app-book-detail
1414
class="col-50"
15-
[book]="currentBook"
15+
[book]="currentBook$ | async"
1616
(save)="onSave($event)"
1717
(cancel)="onCancel()"
1818
>

src/app/books/components/books-page/books-page.component.ts

+16-28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Component, OnInit } from "@angular/core";
22
import { Store } from "@ngrx/store";
3-
import { State } from "src/app/shared/state";
3+
import { Observable } from "rxjs";
44
import {
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";
911
import { BooksService } from "src/app/shared/services/book.service";
1012
import { BooksPageActions, BooksApiActions } from "../../actions";
1113

@@ -15,39 +17,30 @@ import { BooksPageActions, BooksApiActions } from "../../actions";
1517
styleUrls: ["./books-page.component.css"]
1618
})
1719
export 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

Comments
 (0)