Skip to content

Instantly share code, notes, and snippets.

@peterbsmyth
Created March 25, 2018 01:45
Show Gist options
  • Save peterbsmyth/33176e2350f7c4c7a701724594ff61e4 to your computer and use it in GitHub Desktop.
Save peterbsmyth/33176e2350f7c4c7a701724594ff61e4 to your computer and use it in GitHub Desktop.
post.effects.ts for https://blog.upstate.agency
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