Created
June 27, 2020 18:07
-
-
Save kingsley-einstein/c8a66e65a113c94c4ab140cff8e42231 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 { of } from 'rxjs'; | |
import { switchMap, withLatestFrom, map } from 'rxjs/operators'; | |
import { LoadUserAction, LoadUsersAction, LoadUserActionPing, LoadUsersActionPing, UserActionTypes } from '../../actions'; | |
import { AppState } from '../../states'; | |
import { UserService } from '../../../services'; | |
import { selectUsers } from '../../selectors'; | |
@Injectable() | |
export class UserEffects { | |
@Effect() | |
getUser$ = this.actions$.pipe( | |
ofType<LoadUserActionPing>(UserActionTypes.LoadUserPing), | |
map((action) => action.payload), | |
withLatestFrom(this.store$.pipe(select(selectUsers))), | |
switchMap(([email, users]) => { | |
const data = this.service.getByEmail(email); | |
return of(new LoadUserAction({ | |
data | |
})); | |
}) | |
); | |
@Effect() | |
getUsers$ = this.actions$.pipe( | |
ofType<LoadUsersActionPing>(UserActionTypes.LoadUsersPing), | |
switchMap(() => of(new LoadUsersAction({ | |
data: this.service.getDb() | |
}))) | |
); | |
constructor(private actions$: Actions, private store$: Store<AppState>, private service: UserService) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment