13 - NgRx Effects
In this section we examine adding effects
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
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