Skip to content

Commit 9f937d4

Browse files
committed
03-reducers
1 parent 7ebd811 commit 9f937d4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/app/shared/state/books.reducer.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { createReducer, on } from "@ngrx/store";
12
import { BookModel } from "src/app/shared/models/book.model";
3+
import { BooksPageActions } from "src/app/books/actions";
24

35
const createBook = (books: BookModel[], book: BookModel) => [...books, book];
46
const updateBook = (books: BookModel[], changes: BookModel) =>
@@ -7,3 +9,29 @@ const updateBook = (books: BookModel[], changes: BookModel) =>
79
});
810
const deleteBook = (books: BookModel[], bookId: string) =>
911
books.filter(book => bookId !== book.id);
12+
13+
export interface State {
14+
collection: BookModel[];
15+
activeBookId: string | null;
16+
}
17+
18+
export const initialState: State = {
19+
collection: [],
20+
activeBookId: null
21+
};
22+
23+
export const booksReducer = createReducer(
24+
initialState,
25+
on(BooksPageActions.clearSelectedBook, BooksPageActions.enter, state => {
26+
return {
27+
...state,
28+
activeBookId: null
29+
};
30+
}),
31+
on(BooksPageActions.selectBook, (state, action) => {
32+
return {
33+
...state,
34+
activeBookId: action.bookId
35+
};
36+
})
37+
);

0 commit comments

Comments
 (0)