Last active
June 3, 2018 20:33
-
-
Save wesleygrimes/f95dc16a1793bce388f1a32bfe1f1e24 to your computer and use it in GitHub Desktop.
Standard Feature Store Module - Effects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Actions, Effect, ofType } from '@ngrx/effects'; | |
import { Action } from '@ngrx/store'; | |
import { Observable, of as observableOf } from 'rxjs'; | |
import { catchError, map, startWith, switchMap } from 'rxjs/operators'; | |
import { DataService } from '../../services/data.service'; | |
import * as featureActions from './actions'; | |
@Injectable() | |
export class MyFeatureStoreEffects { | |
constructor(private dataService: DataService, private actions$: Actions) {} | |
@Effect() | |
loginRequestEffect$: Observable<Action> = this.actions$.pipe( | |
ofType<featureActions.LoginRequestAction>( | |
featureActions.ActionTypes.LOGIN_REQUEST | |
), | |
switchMap(action => | |
this.dataService | |
.login(action.payload.userName, action.payload.password) | |
.pipe( | |
map( | |
user => | |
new featureActions.LoginSuccessAction({ | |
user | |
}) | |
), | |
catchError(error => | |
observableOf(new featureActions.LoginFailureAction({ error })) | |
) | |
) | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment