123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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: (<CompanyActionUnion> action).payload
- };
-
- //save new company
- case CompanyActionTypes.SAVE_NEW_CAMPANY:
- return {
- ...state,
- msgState: MsgState.ADD
- };
- case CompanyActionTypes.SAVE_NEW_CAMPANY_SUCCESS:
- let newCompany: Company = (<CompanyActionUnion> action).payload;
- let companiesListCopy1: Array<Company> = [...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:(<CompanyActionUnion> action).payload
- };
-
- // get all companies
- case CompanyActionTypes.GET_ALL_COMPANIES:
- return {
- ...state,
- msgState: MsgState.LOADING
- };
- case CompanyActionTypes.GET_ALL_COMPANIES_SUCCESS:
- let companiesListCopy2 : Array<Company> = (<CompanyActionUnion> action).payload;
- return {
- ...state,
- msgState: MsgState.LOADED,
- companiesList: companiesListCopy2
- };
- case CompanyActionTypes.GET_ALL_COMPANIES_ERROR:
- return {
- ...state,
- msgState:MsgState.ERR,
- errorMsg: (<CompanyActionUnion> 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 = (<CompanyActionUnion> action).payload;
- return {
- ...state,
- msgState: MsgState.FORM_LOADED,
- currentCompany:companyToUpdate
- };
- case CompanyActionTypes.CREATE_FORM_UPDATE_COMPANY_ERROR:
- return {
- ...state,
- msgState: MsgState.ERR,
- errorMsg:(<CompanyActionUnion> action).payload
- };
-
- // update company
- case CompanyActionTypes.UPDATE_COMPANY:
- return {
- ...state,
- msgState:MsgState.UPDATE
- };
- case CompanyActionTypes.UPDATE_COMPANY_SUCCESS:
- let updatedCompany: Company = (<CompanyActionUnion> action).payload;
- let companiesListCopy3: Array<Company> = [...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:(<CompanyActionUnion> action).payload
- };
-
- //delete company
- case CompanyActionTypes.DELETE_COMPANY:
- return {
- ...state,
- msgState: MsgState.DELETE
- };
- case CompanyActionTypes.DELETE_COMPANY_SUCCESS:
- let deletedCompany: Company = (<CompanyActionUnion> action).payload;
- let indexDeletedCompany = state.companiesList.indexOf(deletedCompany);
- let companiesListCopy4: Array<Company> = [...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:(<CompanyActionUnion> action).payload
- };
- default:
- return {...state};
- }
- }
|