diff --git a/src/app/store/actions/index.ts b/src/app/store/actions/index.ts index d2700d8..a6c7bcd 100644 --- a/src/app/store/actions/index.ts +++ b/src/app/store/actions/index.ts @@ -1,4 +1,5 @@ export * from './usuarios.actions'; +export * from './usuario.actions'; diff --git a/src/app/store/actions/usuario.actions.ts b/src/app/store/actions/usuario.actions.ts new file mode 100644 index 0000000..59e970b --- /dev/null +++ b/src/app/store/actions/usuario.actions.ts @@ -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; diff --git a/src/app/store/app.reducers.ts b/src/app/store/app.reducers.ts index f975052..1d03fcb 100644 --- a/src/app/store/app.reducers.ts +++ b/src/app/store/app.reducers.ts @@ -3,9 +3,11 @@ import { ActionReducerMap } from '@ngrx/store'; export interface AppState { usuarios: reducers.UsuariosState; + usuario: reducers.UsuarioState; } export const appReducers: ActionReducerMap = { - usuarios: reducers.usuariosReducer + usuarios: reducers.usuariosReducer, + usuario: reducers.usuarioReducer }; diff --git a/src/app/store/reducers/index.ts b/src/app/store/reducers/index.ts index 308ce7c..0daa58b 100644 --- a/src/app/store/reducers/index.ts +++ b/src/app/store/reducers/index.ts @@ -1 +1,2 @@ export * from './usuarios.reducer'; +export * from './usuario.reducer'; diff --git a/src/app/store/reducers/usuario.reducer.ts b/src/app/store/reducers/usuario.reducer.ts new file mode 100644 index 0000000..d8ce453 --- /dev/null +++ b/src/app/store/reducers/usuario.reducer.ts @@ -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; + } +} +