Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/app/shared/state/books.reducer.ts
Original file line number Diff line number Diff line change
@@ -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];
Expand Down Expand Up @@ -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
);