15 - Add Products NgRx Feature Module

In this section we challenge your understanding by adding a Products module like we did for login

Steps

  1. Add NgRx Products lib making it a state state

  2. Add Products Action Creators

  3. Add default state and interface

  4. Make new Product interface

  5. Make new ProductsService in products module

  6. Add effect

  7. Add reducer logic

  8. Dispatch and select products from the store into container component

  9. Dump products as JSON onto the page

1. Add NgRx Products lib making it a state state

nx g @nrwl/angular:ngrx --module=libs/products/src/lib/products.module.ts --minimal false

2. Add Products Action Creators

libs/products/src/lib/+state/products.actions.ts
import { createAction, props } from '@ngrx/store';
import { Product } from '@demo-app/data-models';

export enum ProductsActionTypes {
  LoadProducts = '[Products Page] Load Products',
  LoadProductsSuccess = '[Products API] Load Products Success',
  LoadProductsFail = '[Products API] LoadProducts Fail',
}

export const loadProducts = createAction(
  ProductsActionTypes.LoadProducts
);

export const loadProductsSuccess = createAction(
  ProductsActionTypes.LoadProductsSuccess,
  props<{ payload: Product [] }>()
);

export const loadProductsFailure = createAction(
  ProductsActionTypes.LoadProductsFail,
  props<{ error: any }>()
);

3. Add default state and interface

  • Update state interface

4. Make new Product interface

5. Make new ProductsService in products module

When the CLI asks for the name of the service, add the path for it, and it will be created in the right place since we also provide the project in the command:

6. Add effect

7. Add reducer logic

8. Dispatch and select products from the store into container component

9. Dump products as JSON onto the page

Last updated