Skip to content

Instantly share code, notes, and snippets.

@thecodinganalyst
Created June 13, 2022 01:25
Show Gist options
  • Save thecodinganalyst/a5199ae70c52fd122d2f826939bdc10b to your computer and use it in GitHub Desktop.
Save thecodinganalyst/a5199ae70c52fd122d2f826939bdc10b to your computer and use it in GitHub Desktop.
NgRx Reducer Example
import {createReducer, on} from "@ngrx/store";
import {BookStoreActions} from "./books.actions";
import {BookStore} from "./books.state";
export const initialState: BookStore = { books: [], selectedBook: undefined, showDetail: false};
export const booksReducer = createReducer(
initialState,
on(BookStoreActions.booksLoaded, (state, {books}) => ({...state, books: books})),
on(BookStoreActions.showBook, (state, {book}) => ({...state, selectedBook: book, showDetail: true})),
on(BookStoreActions.newBook, (state) => ({...state, selectedBook: undefined, showDetail: true})),
on(BookStoreActions.bookSaved, (state, {book}) => ({...state, selectedBook: undefined, showDetail: false})),
on(BookStoreActions.bookDeleted, (state, {book}) => ({...state, selectedBook: undefined, showDetail: false})),
on(BookStoreActions.dismissPopup, (state => ({...state, selectedBook: undefined, showDetail: false})))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment