Created
March 25, 2018 01:45
-
-
Save peterbsmyth/33176e2350f7c4c7a701724594ff61e4 to your computer and use it in GitHub Desktop.
post.effects.ts for https://blog.upstate.agency
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 { Post } from './post.model'; | |
import { PostActions, PostActionTypes, LoadPosts, LoadPostsComplete } from './post.actions'; | |
import { Observable } from 'rxjs/Observable'; | |
import { | |
map, | |
switchMap, | |
} from 'rxjs/operators'; | |
import { ApiService } from './api.service'; | |
@Injectable() | |
export class PostEffects { | |
constructor( | |
private actions$: Actions, | |
private api: ApiService, | |
) {} | |
@Effect() | |
get$: Observable<Action> = this.actions$.pipe( | |
ofType<LoadPosts>(PostActionTypes.LoadPosts), | |
switchMap(() => { | |
return this.api.getPosts() | |
.pipe( | |
map((res: Post[]) => new LoadPostsComplete({ | |
posts: res | |
})) | |
); | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment