Skip to content

Instantly share code, notes, and snippets.

@peterbsmyth
Created January 31, 2018 05:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbsmyth/3410ab861dd2fe397885cd39fa8ea408 to your computer and use it in GitHub Desktop.
Save peterbsmyth/3410ab861dd2fe397885cd39fa8ea408 to your computer and use it in GitHub Desktop.

ngRx Recipe

Update Many nested Models

Scenario

Given two models: quote and chapter where:

interface Quote {
  id: string;
  text: string;
  chapter: Chapter;
}

interface Chapter {
  id: string;
  name: string;
}

On an extisting quote reducer that uses ngrx/Entity, update all of the quotes' nested chapters.

Recipe

case ChapterActionTypes.UPDATE_ONE_COMPLETE:
      const idsToUpdate = (state.ids as string[]).filter((id) => state.entities[id].chapter.id === action.payload.id);
      const changes = idsToUpdate.map(id => ({
        id,
        changes: { chapter: {
          id: action.payload.id,
          name: action.payload.name
        } }
      }));
      return adapter.updateMany(changes, state);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment