| import * as contactsActions from '@shared/store/actions/contacts.action'; |
| import { Contact } from '@shared/models'; |
| import { createReducer, on } from '@ngrx/store'; |
| |
| export interface State { |
| loading: boolean; |
| loaded: boolean; |
| failed: boolean; |
| data: Array<Contact>; |
| } |
| |
| export const INITIAL_STATE: State = { |
| loading: false, |
| loaded: false, |
| failed: false, |
| data: [] |
| }; |
| export const ContactsReducer = createReducer( |
| INITIAL_STATE, |
| on(contactsActions.loadContacts, (state: any, action: any) => { |
| return { |
| ...state, |
| loading: true, |
| loaded: false, |
| failed: false, |
| data: [] |
| }; |
| }), |
| on(contactsActions.loadContactsSuccess, (state: any, action: any) => { |
| return { |
| ...state, |
| loading: false, |
| loaded: true, |
| failed: false, |
| data: action['payload'] |
| }; |
| }), |
| on(contactsActions.loadContactsFail, (state: any, action: any) => { |
| return { |
| ...state, |
| loaded: false, |
| loading: false, |
| failed: true, |
| data: [] |
| }; |
| }) |
| ); |
| export function reducer(state = INITIAL_STATE, action: any): State { |
| if (!action) { |
| return state; |
| } |
| |
| return ContactsReducer(state, action); |
| } |
| |
| export const getData = (state: State) => state.data; |
| export const getLoading = (state: State) => state.loading; |
| export const getLoaded = (state: State) => state.loaded; |
| export const getFailed = (state: State) => state.failed; |