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 + }; + }) +);