import { EmployeeState, initialState } from '../states/employee.state'; import { Actions, ActionTypes } from '../actions/employee.action'; export function reducer( state: EmployeeState = initialState, action: Actions ): EmployeeState { switch (action.type) { case ActionTypes.SUBORDINATES: return (Object).assign({}, state, { entries: [...state.entries, ...action.payload] }); case ActionTypes.SET_ENTRIES: return (Object).assign({}, state, { entries: action.payload }); case ActionTypes.SET_SELECTED: return (Object).assign({}, state, { selectedEntries: action.payload }); case ActionTypes.TOGGLE_SELECTED: let selectedEntries: any[] = [...state.selectedEntries]; if(selectedEntries.indexOf(action.payload) !== -1) { selectedEntries.splice(selectedEntries.indexOf(action.payload), 1); } else { selectedEntries.push(action.payload); } selectedEntries.sort((a: any, b: any) => { return (a.first_name + ' ' + a.last_name).localeCompare(b.first_name + ' ' + b.last_name); }); return (Object).assign({}, state, { selectedEntries: selectedEntries }); case ActionTypes.ACTIVE: return (Object).assign({}, state, { active: action.payload }); default: return state; } }