From 9f937d449f6e832a34f1d0be70682861d44398eb Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Fri, 6 Sep 2019 07:11:31 -0700 Subject: [PATCH] 03-reducers --- src/app/shared/state/books.reducer.ts | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/app/shared/state/books.reducer.ts b/src/app/shared/state/books.reducer.ts index 30d5d35..53fe775 100644 --- a/src/app/shared/state/books.reducer.ts +++ b/src/app/shared/state/books.reducer.ts @@ -1,4 +1,6 @@ +import { createReducer, on } from "@ngrx/store"; import { BookModel } from "src/app/shared/models/book.model"; +import { BooksPageActions } from "src/app/books/actions"; const createBook = (books: BookModel[], book: BookModel) => [...books, book]; const updateBook = (books: BookModel[], changes: BookModel) => @@ -7,3 +9,29 @@ const updateBook = (books: BookModel[], changes: BookModel) => }); 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 + }; + }) +);