From 0cde07d329b18a887423f79000c2e39cbdeecddd Mon Sep 17 00:00:00 2001 From: Pablo Luis Reinoso Santana Date: Wed, 19 Sep 2018 11:46:32 -0400 Subject: [PATCH] usuario effects listo --- src/app/services/usuario.service.ts | 6 ++++ src/app/store/effects/index.ts | 4 ++- src/app/store/effects/usuario.effects.ts | 31 +++++++++++++++++++ src/app/usuarios/usuario/usuario.component.ts | 17 +++++++++- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/app/store/effects/usuario.effects.ts diff --git a/src/app/services/usuario.service.ts b/src/app/services/usuario.service.ts index 339d31e..7258080 100644 --- a/src/app/services/usuario.service.ts +++ b/src/app/services/usuario.service.ts @@ -19,4 +19,10 @@ export class UsuarioService { map(res => res['data']) ); } + getUserbyId(id: string) { + return this.http.get(`${this.url}/users/${id}`) + .pipe( + map(res => res['data']) + ); + } } diff --git a/src/app/store/effects/index.ts b/src/app/store/effects/index.ts index 3cc77de..478d19a 100644 --- a/src/app/store/effects/index.ts +++ b/src/app/store/effects/index.ts @@ -1,7 +1,9 @@ import {UsuariosEffects} from './usuarios.effects'; +import {UsuarioEffects} from './usuario.effects'; -export const effectsArr: any[] = [UsuariosEffects]; +export const effectsArr: any[] = [UsuariosEffects, UsuarioEffects]; export * from './usuarios.effects'; +export * from './usuario.effects'; diff --git a/src/app/store/effects/usuario.effects.ts b/src/app/store/effects/usuario.effects.ts new file mode 100644 index 0000000..ca6dbe5 --- /dev/null +++ b/src/app/store/effects/usuario.effects.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@angular/core'; +import { Actions, Effect } from '@ngrx/effects'; +import * as fromActions from '../actions'; +import { map, switchMap, catchError } from 'rxjs/operators'; +import { of } from 'rxjs'; +import { UsuarioService } from '../../services/usuario.service'; + +@Injectable() +export class UsuarioEffects { + constructor( + private actions$: Actions, + public usuarioS: UsuarioService + ) { } + + @Effect() + cargarUsuario$ = this.actions$ + .ofType(fromActions.CARGAR_USUARIO) + .pipe( + switchMap( + action => { + return this.usuarioS.getUserbyId(action['id']) + .pipe( + map(user => new fromActions.CargarUsuarioSuccess(user)), + catchError(error => of(new fromActions.CargarUsuarioFail(error))) + ); + } + ) + ); +} + + diff --git a/src/app/usuarios/usuario/usuario.component.ts b/src/app/usuarios/usuario/usuario.component.ts index f9ec610..046f50f 100644 --- a/src/app/usuarios/usuario/usuario.component.ts +++ b/src/app/usuarios/usuario/usuario.component.ts @@ -1,4 +1,8 @@ import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { AppState } from '../../store/app.reducers'; +import { Store } from '@ngrx/store'; +import { CargarUsuario } from '../../store/actions/usuario.actions'; @Component({ selector: 'app-usuario', @@ -7,9 +11,20 @@ import { Component, OnInit } from '@angular/core'; }) export class UsuarioComponent implements OnInit { - constructor() { } + constructor( + private router: ActivatedRoute, + private store: Store + ) { } ngOnInit() { + this.router.params.subscribe( + params => { + const id = params['id']; + this.store.dispatch(new CargarUsuario(id)); + // console.log(id); + + } + ); } }