Skip to content

Commit 902f2dc

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
04-selectors complete
1 parent 1f8e09c commit 902f2dc

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
66
[books]="books$ | async"
@@ -12,7 +12,7 @@
1212

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

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@ import { BooksPageActions } from "../../actions";
1515
})
1616
export class BooksPageComponent implements OnInit {
1717
books$: Observable<Book[]>;
18-
currentBook: Book;
19-
total: number;
18+
activeBook$: Observable<Book>;
19+
total$: Observable<number>;
2020

2121
constructor(private store: Store<fromRoot.State>) {
22-
this.books$ = this.store.pipe(
23-
select(state => state.books),
24-
map((booksState: any) =>
25-
booksState.ids.map(id => booksState.entities[id])
26-
),
27-
tap(books => this.updateTotals(books))
28-
);
22+
this.books$ = this.store.pipe(select(fromRoot.selectAllBooks));
23+
this.activeBook$ = this.store.pipe(select(fromRoot.selectActiveBook));
24+
this.total$ = this.store.pipe(select(fromRoot.selectBookEarningsTotals));
2925
}
3026

3127
ngOnInit() {
@@ -37,15 +33,8 @@ export class BooksPageComponent implements OnInit {
3733
this.store.dispatch(new BooksPageActions.Enter());
3834
}
3935

40-
updateTotals(books: Book[]) {
41-
this.total = books.reduce((total, book) => {
42-
return total + parseInt(`${book.earnings}`, 10) || 0;
43-
}, 0);
44-
}
45-
4636
onSelect(book: Book) {
4737
this.store.dispatch(new BooksPageActions.SelectBook(book.id));
48-
this.currentBook = book;
4938
}
5039

5140
onCancel() {
@@ -54,7 +43,6 @@ export class BooksPageComponent implements OnInit {
5443

5544
removeSelectedBook() {
5645
this.store.dispatch(new BooksPageActions.ClearSelectedBook());
57-
this.currentBook = null;
5846
}
5947

6048
onSave(book: Book) {

src/app/shared/state/books.reducer.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createEntityAdapter, EntityAdapter, EntityState } from "@ngrx/entity";
22
import { Book } from "src/app/shared/models/book.model";
33
import { BooksPageActions } from "src/app/books/actions";
4+
import { createSelector } from "@ngrx/store";
45

56
export const initialBooks: Book[] = [
67
{
@@ -72,3 +73,18 @@ export function reducer(
7273
return state;
7374
}
7475
}
76+
77+
export const { selectAll, selectEntities } = adapter.getSelectors();
78+
export const selectActiveBookId = (state: State) => state.activeBookId;
79+
export const selectActiveBook = createSelector(
80+
selectEntities,
81+
selectActiveBookId,
82+
(entities, bookId) => (bookId ? entities[bookId] : null)
83+
);
84+
export const selectEarningsTotals = createSelector(
85+
selectAll,
86+
books =>
87+
books.reduce((total, book) => {
88+
return total + parseInt(`${book.earnings}`, 10) || 0;
89+
}, 0)
90+
);

src/app/shared/state/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,20 @@ export const selectMoviesEarningsTotal = createSelector(
4343
selectMovieState,
4444
fromMovies.selectEarningsTotal
4545
);
46+
47+
export const selectBooksState = (state: State) => state.books;
48+
49+
export const selectAllBooks = createSelector(
50+
selectBooksState,
51+
fromBooks.selectAll
52+
);
53+
54+
export const selectActiveBook = createSelector(
55+
selectBooksState,
56+
fromBooks.selectActiveBook
57+
);
58+
59+
export const selectBookEarningsTotals = createSelector(
60+
selectBooksState,
61+
fromBooks.selectEarningsTotals
62+
);

0 commit comments

Comments
 (0)