import { Injectable } from '@angular/core' ; import { Effect , ofType , Actions } from '@ngrx/effects' ; import { Store , select } from '@ngrx/store' ; import { of } from 'rxjs' ; import { switchMap , map , withLatestFrom } from 'rxjs/operators' ; import { IAppState } from '../state/app.state' ; import { GetUsersSuccess , EUserActions , GetUserSuccess , GetUser , GetUsers } from '../actions/user.actions' ; import { UserService } from '../../services/user.service' ; import { IUserHttp } from '../../models/http-models/user-http.interface' ; import { selectUserList } from '../selectors/user.selector' ; @ Injectable ( ) export class UserEffects { @ Effect ( ) getUser$ = this . _actions$ . pipe ( ofType < GetUser > ( EUserActions . GetUser ) , map ( action => action . payload ) , withLatestFrom ( this . _store . pipe ( select ( selectUserList ) ) ) , switchMap ( ( [ id , users ] ) => { const selectedUser = users . filter ( user => user . id === + id ) [ 0 ] ; return of ( new GetUserSuccess ( selectedUser ) ) ; } ) ) ; @ Effect ( ) getUsers$ = this . _actions$ . pipe ( ofType < GetUsers > ( EUserActions . GetUsers ) , switchMap ( ( ) => this . _userService . getUsers ( ) ) , switchMap ( ( userHttp ) => of ( new GetUsersSuccess ( userHttp . users ) ) ) ) ; constructor ( private _userService , private _actions$ , private _store ) { } }