0
votes

There are four reducers used in the @ngrx effects example namely (1) books (2) collection (3) layout and (4) search. I don't understand why a collection reducer is necessary here because the books reducer already reflects the state of the collection after an add or remove operation. I see the use of the collection reducer as to track the loading and loaded status only.

Look at the final state of the books being create with getBookCollection. The getBookEntities reflect the state of the collection after any add or remove, so does not have to compose with the collection reducer.

Am I missing any logic here?

export const getCollectionState = (state: State) => state.collection;

export const getCollectionLoaded = createSelector(getCollectionState, fromCollection.getLoaded);
export const getCollectionLoading = createSelector(getCollectionState, fromCollection.getLoading);
export const getCollectionBookIds = createSelector(getCollectionState, fromCollection.getIds);

export const getBookCollection = createSelector(getBookEntities, getCollectionBookIds, (entities, ids) => {
  return ids.map(id => entities[id]);
});
1
how would you otherwise get collection from server without adding/removing items? just pure get.dee zg
The collection represents the items that are available on the client side.wonderful world
of course but you seem to assume that those items could come into client side state only through add/remove actions. my point is they could also be loaded into state by simply getting them from server.dee zg

1 Answers

0
votes

The books state doesn't reflect the collection state after an add or removal of a book from the user's book collection.

Using the redux devtools this first image shows that I have 2 books in my books state and 2 books in my collection state:

Two books in collection

After removing one of the books you can see that the books state stays the same and the collection now only has one id.

One book in collection