import { CompanyActionTypes } from './company.action.type'; import { Action } from '@ngrx/store'; import { CompanyActionUnion } from './company.actions'; import { MsgState } from '../msg.state'; import { CompanyState } from './company.state'; import { Company } from 'src/app/shared/models/company/company.model'; const companyInitState : CompanyState = { currentCompany: null, companiesList: [], msgState: MsgState.INIT_STATE, errorMsg: "no error" } export function companyReducer(state: CompanyState = companyInitState, action: Action): CompanyState{ switch(action.type){ case CompanyActionTypes.CREATE_FORM_ADD_COMPANY: return { ...state, msgState: MsgState.FORM_LOADING }; case CompanyActionTypes.CREATE_FORM_ADD_COMPANY_SUCCESS: return{ ...state, msgState:MsgState.FORM_LOADED }; case CompanyActionTypes.CREATE_FORM_ADD_COMPANY_ERROR: return { ...state, msgState:MsgState.ERR, errorMsg: ( action).payload }; //save new company case CompanyActionTypes.SAVE_NEW_CAMPANY: return { ...state, msgState: MsgState.ADD }; case CompanyActionTypes.SAVE_NEW_CAMPANY_SUCCESS: let newCompany: Company = ( action).payload; let companiesListCopy1: Array = [...state.companiesList]; companiesListCopy1.push(newCompany); return { ...state, msgState: MsgState.ADDED, companiesList: companiesListCopy1, currentCompany:newCompany }; case CompanyActionTypes.SAVE_NEW_CAMPANY_ERROR: return { ...state, msgState: MsgState.ERR, errorMsg:( action).payload }; // get all companies case CompanyActionTypes.GET_ALL_COMPANIES: return { ...state, msgState: MsgState.LOADING }; case CompanyActionTypes.GET_ALL_COMPANIES_SUCCESS: let companiesListCopy2 : Array = ( action).payload; return { ...state, msgState: MsgState.LOADED, companiesList: companiesListCopy2 }; case CompanyActionTypes.GET_ALL_COMPANIES_ERROR: return { ...state, msgState:MsgState.ERR, errorMsg: ( action).payload }; // create form to update company case CompanyActionTypes.CREATE_FORM_UPDATE_COMPANY: return{ ...state, msgState: MsgState.FORM_LOADING }; case CompanyActionTypes.CREATE_FORM_UPDATE_COMPANY_SUCCESS: let companyToUpdate: Company = ( action).payload; return { ...state, msgState: MsgState.FORM_LOADED, currentCompany:companyToUpdate }; case CompanyActionTypes.CREATE_FORM_UPDATE_COMPANY_ERROR: return { ...state, msgState: MsgState.ERR, errorMsg:( action).payload }; // update company case CompanyActionTypes.UPDATE_COMPANY: return { ...state, msgState:MsgState.UPDATE }; case CompanyActionTypes.UPDATE_COMPANY_SUCCESS: let updatedCompany: Company = ( action).payload; let companiesListCopy3: Array = [...state.companiesList]; companiesListCopy3.map( (company: Company) => (company.companyID==updatedCompany.companyID)?updatedCompany:company ); return { ...state, msgState:MsgState.UPDATED, companiesList: companiesListCopy3, currentCompany: updatedCompany }; case CompanyActionTypes.UPDATE_COMPANY_ERROR: return { ...state, msgState: MsgState.ERR, errorMsg:( action).payload }; //delete company case CompanyActionTypes.DELETE_COMPANY: return { ...state, msgState: MsgState.DELETE }; case CompanyActionTypes.DELETE_COMPANY_SUCCESS: let deletedCompany: Company = ( action).payload; let indexDeletedCompany = state.companiesList.indexOf(deletedCompany); let companiesListCopy4: Array = [...state.companiesList]; companiesListCopy4.splice(indexDeletedCompany,1); return{ ...state, msgState: MsgState.DELETED, companiesList: companiesListCopy4, currentCompany: deletedCompany }; case CompanyActionTypes.DELETE_COMPANY_ERROR: return{ ...state, msgState: MsgState.ERR, errorMsg:( action).payload }; default: return {...state}; } }