Created
June 27, 2020 19:08
-
-
Save kingsley-einstein/30036697d6611e50b6b7952a2a44915f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { Store, select } from '@ngrx/store'; | |
import { switchMap, map, withLatestFrom } from 'rxjs/operators'; | |
import { of } from 'rxjs'; | |
import { LoadPostAction, LoadPostsAction, LoadPostActionPing, LoadPostsActionPing, PostActionTypes } from '../../actions'; | |
import { AppState } from '../../states'; | |
import { selectPosts } from '../../selectors'; | |
import { PostService } from '../../../services'; | |
@Injectable() | |
export class PostEffects { | |
@Effect() | |
getPost$ = this.actions$.pipe( | |
ofType<LoadPostActionPing>(PostActionTypes.LoadPostPing), | |
map((action) => action.payload), | |
withLatestFrom(this.store$.pipe(select(selectPosts))), | |
switchMap(([id, posts]) => { | |
const data = this.service.getById(id); | |
return of(new LoadPostAction({ | |
data | |
})); | |
}) | |
); | |
@Effect() | |
getPosts$ = this.actions$.pipe( | |
ofType<LoadPostsActionPing>(PostActionTypes.LoadPostsPing), | |
switchMap(() => of(new LoadPostsAction({ | |
data: this.service.getDb() | |
}))) | |
); | |
constructor(private actions$: Actions, private store$: Store<AppState>, private service: PostService) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment