From 2ee14ffc40ffb8b9c239cb0f4526cc9274cc7177 Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Sat, 28 Mar 2020 19:54:31 -0500 Subject: [PATCH] 03-reducers --- src/app/shared/state/books.reducer.ts | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/app/shared/state/books.reducer.ts b/src/app/shared/state/books.reducer.ts index c114f7e..426fbbb 100644 --- a/src/app/shared/state/books.reducer.ts +++ b/src/app/shared/state/books.reducer.ts @@ -1,3 +1,37 @@ import { createReducer, on, Action, createSelector } from "@ngrx/store"; import { BookModel, calculateBooksGrossEarnings } from "src/app/shared/models"; import { BooksPageActions, BooksApiActions } from "src/app/books/actions"; + +const createBook = (books: BookModel[], book: BookModel) => [...books, book]; +const updateBook = (books: BookModel[], changes: BookModel) => + books.map(book => { + return book.id === changes.id ? Object.assign({}, book, changes) : book; + }); +const deleteBook = (books: BookModel[], bookId: string) => + books.filter(book => bookId !== book.id); + +export interface State { + collection: BookModel[]; + activeBookId: string | null; +} + +export const initialState: State = { + collection: [], + activeBookId: null +}; + +export const booksReducer = createReducer( + initialState, + on(BooksPageActions.clearSelectedBook, BooksPageActions.enter, state => { + return { + ...state, + activeBookId: null + }; + }), + on(BooksPageActions.selectBook, (state, action) => { + return { + ...state, + activeBookId: action.bookId + }; + }) +);