13 - NgRx Effects
In this section we examine adding effects
The official docs say Effects decrease the responsibility of the component.
1. Add an Auth Effects
import { Injectable } from '@angular/core';
import { createEffect, Actions, ofType } from '@ngrx/effects';
import { fetch } from '@nrwl/angular';
import { AuthActionTypes } from './auth.actions';
import * as AuthActions from './auth.actions';
import { AuthService } from './../services/auth/auth.service';
@Injectable()
export class AuthEffects {
login$ = createEffect(() => this.actions$.pipe(
ofType(AuthActionTypes.Login),
fetch({
run: (action) => {
return this.authService.login(action);
},
onError: (action, error) => {
return AuthActions.loginFailure(error);
},
})
));
constructor(
private actions$: Actions,
private authService: AuthService
) {}
}2. Add reducer code
3. Update LoginComponent to dispatch an action
4. Add new Effect action to navigate on LoginSuccess
Add new effect to manage routing
You can read more about routing with actions here: Router Actions.
5. Export state references in index.ts
6. Update AuthGuard to use the store
7. On load check local storage and dispatch a LoginSuccess action
Last updated