Skip to content

Commit

Permalink
usuario actions, reducers, state
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo384 committed Sep 19, 2018
1 parent c78ae0c commit 90644b9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/app/store/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './usuarios.actions';
export * from './usuario.actions';



24 changes: 24 additions & 0 deletions src/app/store/actions/usuario.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Action } from '@ngrx/store';
import { Usuario } from '../../models/usuario.model';

export const CARGAR_USUARIO = '[USUARIO] Cargar usuario';
export const CARGAR_USUARIO_FAIL = '[USUARIO] Cargar usuario FAIL';
export const CARGAR_USUARIO_SUCCESS = '[USUARIO] Cargar usuario SUCCESS';

export class CargarUsuario implements Action {
readonly type = CARGAR_USUARIO;
constructor(public id: string) { }
}
export class CargarUsuarioFail implements Action {
readonly type = CARGAR_USUARIO_FAIL;
constructor(public payload: any) { }
}
export class CargarUsuarioSuccess implements Action {
readonly type = CARGAR_USUARIO_SUCCESS;
constructor(public usuario: Usuario) { }
}

export type usuarioAcciones =
CargarUsuario |
CargarUsuarioFail |
CargarUsuarioSuccess;
4 changes: 3 additions & 1 deletion src/app/store/app.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { ActionReducerMap } from '@ngrx/store';

export interface AppState {
usuarios: reducers.UsuariosState;
usuario: reducers.UsuarioState;
}

export const appReducers: ActionReducerMap<AppState> = {
usuarios: reducers.usuariosReducer
usuarios: reducers.usuariosReducer,
usuario: reducers.usuarioReducer
};

1 change: 1 addition & 0 deletions src/app/store/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './usuarios.reducer';
export * from './usuario.reducer';
49 changes: 49 additions & 0 deletions src/app/store/reducers/usuario.reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Usuario } from '../../models/usuario.model';
import * as fromUsuario from '../actions';

export interface UsuarioState {
user: Usuario;
loaded: boolean;
loading: boolean;
error: any;
}

const initState: UsuarioState = {
user: null,
loaded: false,
loading: false,
error: null
};

export function usuarioReducer(state = initState, action: fromUsuario.usuarioAcciones): UsuarioState {
switch (action.type) {
case fromUsuario.CARGAR_USUARIO:
return {
...state,
loading: true,
error: null
};
case fromUsuario.CARGAR_USUARIO_SUCCESS:
return {
...state,
loading: false,
loaded: true,
user: {...action.usuario}
};
case fromUsuario.CARGAR_USUARIO_FAIL:
return {
...state,
loading: false,
loaded: false,
user: null,
error: {
status: action.payload.status,
message: action.payload.error,
url: action.payload.url
}
};
default:
return state;
}
}

0 comments on commit 90644b9

Please sign in to comment.