Last active
April 25, 2018 18:12
-
-
Save praveenpuglia/12b445b9f5b1ea8f160c83ff64d30164 to your computer and use it in GitHub Desktop.
Access state inside an ngrx effect
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
// In the Effect's constructor, inject store. | |
constructor(private store: Store<AppState>) | |
... | |
@Effect() | |
fetchUserProfile$: Observable<Action> = this.actions$.pipe( | |
ofType<FetchUserProfile>(UserActionTypes.FETCH_USER_PROFILE), | |
withLatestFrom(this.store), | |
mergeMap(([action, appState]) => { | |
return this.http | |
.get( | |
`${environment.apiUrl}/api/user/${appState.user.id}` | |
) | |
.pipe( | |
map((response: any) => { | |
return new FetchUserProfileSuccess(response.data); | |
}), | |
catchError(error => of(new FetchUserProfileFailure(error))) | |
); | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment