Skip to content

Commit c26fbe5

Browse files
04-selectors complete
1 parent b2d2184 commit c26fbe5

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
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,7 +1,7 @@
11
<div class="container">
22
<div class="col-50">
33
<app-books-total
4-
[total]="total">
4+
[total]="total$ | async">
55
</app-books-total>
66

77
<app-books-list
@@ -12,7 +12,7 @@
1212
</div>
1313

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

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

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

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

3129
ngOnInit() {
@@ -37,15 +35,8 @@ export class BooksPageComponent implements OnInit {
3735
this.store.dispatch(new BooksPageActions.Enter());
3836
}
3937

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

5142
onCancel() {
@@ -54,7 +45,6 @@ export class BooksPageComponent implements OnInit {
5445

5546
removeSelectedBook() {
5647
this.store.dispatch(new BooksPageActions.ClearSelectedBook());
57-
this.currentBook = null;
5848
}
5949

6050
onSave(book: Book) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ export function reducer(state = initialState, action: BooksPageActions.BooksActi
6363
}
6464
}
6565

66+
export const { selectAll, selectEntities } = adapter.getSelectors();
67+
export const activeBookId = (state: State) => state.activeBookId;

src/app/shared/state/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,33 @@ export const selectMoviesEarningsTotal = createSelector(
4444
selectMovies,
4545
movies => movies.reduce((total, movie) => total + parseInt(`${movie.earnings}`, 10) || 0, 0)
4646
);
47+
48+
export const selectBooksState = (state: State) => state.books;
49+
50+
export const selectActiveBookId = createSelector(
51+
selectBooksState,
52+
fromBooks.activeBookId
53+
);
54+
55+
export const selectAllBooks = createSelector(
56+
selectBooksState,
57+
fromBooks.selectAll
58+
);
59+
60+
export const selectAllBooksEntities = createSelector(
61+
selectBooksState,
62+
fromBooks.selectEntities
63+
);
64+
65+
export const selectActiveBook = createSelector(
66+
selectAllBooksEntities,
67+
selectActiveBookId,
68+
(entities, bookId) => bookId ? entities[bookId] : null
69+
);
70+
71+
export const selectBookEarningsTotals = createSelector(
72+
selectAllBooks,
73+
books => books.reduce((total, book) => {
74+
return total + parseInt(`${book.earnings}`, 10) || 0;
75+
}, 0)
76+
)

0 commit comments

Comments
 (0)