NGRX Entity
Managing Passengers
In this exercise, you will leverage @ngrx/entity
and @ngrx/schematics
to manage Passenger
entities with the store. For this, you will create an separate PassengerModule with a PassengerComponent.
-
Use the CLI to generate a new
PassengersModule
with the boilerplate for@ngrx/entity
. For this, switch into the folderflight-app\src\app
and use the following commands:npx ng g module passengers cd passengers npx ng g @ngrx/schematics:entity Passenger --module passengers.module.ts --creators
-
Discover the generated files.
-
Open the file
passenger.model
and add aname
property to thePassenger
class. Also, make the id a number:export interface Passenger { id: number; // <-- Modify (number) name: string; // <-- Add this }
-
In the
passengers
folder, create a new filepassenger.selectors.ts
:import * as fromPassenger from './passenger.reducer'; import { createSelector } from '@ngrx/store'; import { passengersFeatureKey } from './passenger.reducer'; // Parent node pointing to passenger state export class PassengerAppState { [passengersFeatureKey]: fromPassenger.State; } // Selector pointing to passenger state in store const base = (s: PassengerAppState) => s.passengers; // Selector pointing to all passenger entities export const selectAllPassengers = createSelector(base, fromPassenger.selectAll);
-
In the
passengers
folder, create a newPassengersComponent
. In itsngOnInit
method, send anAddPassengers
action with an hard coded array of passengers to the store and query all the passengers using the above mentionedselectAllPassengers
selector. Display the passengers in the template.Show code (TypeScript)
@Component({ selector: 'app-passengers', templateUrl: './passengers.component.html', styleUrls: ['./passengers.component.css'] }) export class PassengersComponent implements OnInit { constructor(private store: Store<PassengerAppState>) {} passengers$: Observable<Passenger[]>; ngOnInit(): void { this.store.dispatch(addPassengers({ passengers: [{id: 1, name: 'Max'}, {id:2, name: 'Susi'}]})); this.passengers$ = this.store.select(selectAllPassengers); } }
Show code (HTML)
<div class="card"> <div class="header"> <h2 class="title">Latest Passengers</h2> </div> <div class="content"> <pre>{{ passengers$ | async | json}}</pre> </div> </div>
-
Make sure, the
PassengersComponent
is declared AND exported with thePassengerModule
. -
Make sure, the
PassengersModule
is imported into theAppModule
. -
Call the
PassengersComponent
within theHomeComponent
to try it out.<app-passengers></app-passengers>
-
Test your application.
Bonus: Loading passengers **
Extend your solution to load passengers using a search form and an effect. You can use the following Web API for this:
http://angular.at/api/passenger?name=Muster
Please note that this Web API is using PascalCase to display attributes with XML but camelCase for JSON to respect the respective usual conventions.