Skip to content

Commit

Permalink
usuario effects listo
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo384 committed Sep 19, 2018
1 parent 90644b9 commit 0cde07d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/app/services/usuario.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
);
}
}
4 changes: 3 additions & 1 deletion src/app/store/effects/index.ts
Original file line number Diff line number Diff line change
@@ -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';


31 changes: 31 additions & 0 deletions src/app/store/effects/usuario.effects.ts
Original file line number Diff line number Diff line change
@@ -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)))
);
}
)
);
}


17 changes: 16 additions & 1 deletion src/app/usuarios/usuario/usuario.component.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -7,9 +11,20 @@ import { Component, OnInit } from '@angular/core';
})
export class UsuarioComponent implements OnInit {

constructor() { }
constructor(
private router: ActivatedRoute,
private store: Store<AppState>
) { }

ngOnInit() {
this.router.params.subscribe(
params => {
const id = params['id'];
this.store.dispatch(new CargarUsuario(id));
// console.log(id);

}
);
}

}

0 comments on commit 0cde07d

Please sign in to comment.