NgRx Entities: Wrangling the reducers

Note: This will be a series of posts around how I moved from custom built reducers and state management with NgRx to use the NgRx Entity library. Previous posts include NgRx Entities: Namespaces with TypeScript and NgRx Entities: Updating the selectors for background.


After doing the base setup with the selectors and the state, I can finally move to updating the reducers for the application. This is where the magic and simplicity of the Entity library from NgRx comes to fruition.

A great reference book that goes deeper into this topic: Architecting Angular Applications with Redux, RxJS, and NgRx

Update the actions

I lied. Before I could get into the reducers, I needed to make a modification to the actions first specifically around any action that updates individual records. Since the entity library utilizes key stores from IDs to reference single records, any payload that modifies the record must utilize the type Update from the entity store. This would require the payload sent over to the action to look like:

1
2
3
4
5
6
const data = {
id: 1,
changes: {
// object data that is changed
}
};

Because of this, the action referencing any change to a record needs to be updated.

Before

1
2
3
4
5
6
7
// store/actions/book.actions.ts
...

export class UpdateBookSuccess implements Action {
public readonly type = BooksActions.UpdateBookSuccess;
constructor(public payload: Book) { }
}

After

1
2
3
4
5
6
7
// store/actions/book.actions.ts
...

export class UpdateBookSuccess implements Action {
public readonly type = BooksActions.UpdateBookSuccess;
constructor(public payload: Update<Book>) { }
}

On to the reducers!

The biggest change that comes with the addition of the entity adapter is that everything becomes a simple one liner and the complexity is wrapped up internally in the method. No longer do I have to handle the immutable state or select the correct item based on ID. Instead, I get to send in the whole payload and let the adapter sort it out.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// store/reducers/books.reducers.ts
export const booksReducers = (
state = initialBooksState,
action: BooksActions,
): BooksState => {
switch (action.type) {
case BooksActions.GetBookSuccess: {
return {
...state,
selectedBook: action.payload,
};
}

case BooksActions.GetBooksSuccess: {
return {
...state,
books: action.payload,
};
}

case BooksActions.CreateBookSuccess: {
return {
...state,
books: [...state.books, action.payload]
};
}

case BooksActions.UpdateBookSuccess: {
return {
...state,
books: state.books
.map(
book => book.id === action.payload.id
? action.payload
: book
),
};
}

case BooksActions.DeleteBookSuccess: {
return {
...state,
books: state.books.filter((book: Book)
=> book.id !== action.payload)
};
}

default:
return state;
}
};

As you can see, lots of handling of immutable state and filtering based on data passed in through the payload. Let’s clean this up with entities and the new namespace setup!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// store/reducers/books.reducers.ts
export const booksReducers = (
state = BooksStore.initialState,
action: BooksActions,
): BooksStore.State => {
const { adapter } = BooksStore;

switch (action.type) {
case BooksActions.GetBookSuccess: {
return adapter.addOne(action.payload, state);
}

case BooksActions.GetBooksSuccess: {
return adapter.addAll(action.payload, state);
}

case BooksActions.CreateBookSuccess: {
return adapter.addOne(action.payload, state);
}

case BooksActions.UpdateBookSuccess: {
return adapter.updateOne(action.payload, state);
}

case BooksActions.DeleteBookSuccess: {
return adapter.removeOne(action.payload, state);
}

default:
return state;
}
};

Much cleaner as the complexity is now all handled by the adapter!

As a reminder, anyplace the payload is being used to update a record, the design must contain the form:

1
2
3
4
5
6
const data = {
id: someId,
changes: {
// object data that is changed
}
};

After all of these updates, I am pretty happy how less complex the reducers have turned out. Relying on the entity setup has helped move to a more streamlined use which makes my code look and feel cleaner.

Filed under: Code