Skip to content

Commit db8b3f8

Browse files
committed
06-selectors
1 parent 14718c3 commit db8b3f8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { createReducer, on, Action } from "@ngrx/store";
2-
import { BookModel } from "src/app/shared/models/book.model";
1+
import { createReducer, on, Action, createSelector } from "@ngrx/store";
2+
import {
3+
BookModel,
4+
calculateBooksGrossEarnings
5+
} from "src/app/shared/models/book.model";
36
import { BooksPageActions, BooksApiActions } from "src/app/books/actions";
47

58
const createBook = (books: BookModel[], book: BookModel) => [...books, book];
@@ -63,3 +66,15 @@ export const booksReducer = createReducer(
6366
export function reducer(state: State | undefined, action: Action) {
6467
return booksReducer(state, action);
6568
}
69+
70+
export const selectAll = (state: State) => state.collection;
71+
export const selectActiveBookId = (state: State) => state.activeBookId;
72+
export const selectActiveBook = createSelector(
73+
selectAll,
74+
selectActiveBookId,
75+
(books, activeBookId) => books.find(book => book.id === activeBookId) || null
76+
);
77+
export const selectEarningsTotals = createSelector(
78+
selectAll,
79+
calculateBooksGrossEarnings
80+
);

0 commit comments

Comments
 (0)