diff --git a/src/app/shared/state/books.reducer.ts b/src/app/shared/state/books.reducer.ts index 116ef05..56d1dfd 100644 --- a/src/app/shared/state/books.reducer.ts +++ b/src/app/shared/state/books.reducer.ts @@ -1,5 +1,8 @@ -import { createReducer, on, Action } from "@ngrx/store"; -import { BookModel } from "src/app/shared/models/book.model"; +import { createReducer, on, Action, createSelector } from "@ngrx/store"; +import { + BookModel, + calculateBooksGrossEarnings +} from "src/app/shared/models/book.model"; import { BooksPageActions, BooksApiActions } from "src/app/books/actions"; const createBook = (books: BookModel[], book: BookModel) => [...books, book]; @@ -63,3 +66,15 @@ export const booksReducer = createReducer( export function reducer(state: State | undefined, action: Action) { return booksReducer(state, action); } + +export const selectAll = (state: State) => state.collection; +export const selectActiveBookId = (state: State) => state.activeBookId; +export const selectActiveBook = createSelector( + selectAll, + selectActiveBookId, + (books, activeBookId) => books.find(book => book.id === activeBookId) || null +); +export const selectEarningsTotals = createSelector( + selectAll, + calculateBooksGrossEarnings +);