loop-modules
Version:
Shared modules for the Loop product suite.
43 lines (41 loc) • 1.59 kB
text/typescript
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 (<any>Object).assign({}, state, {
entries: [...state.entries, ...action.payload]
});
case ActionTypes.SET_ENTRIES:
return (<any>Object).assign({}, state, {
entries: action.payload
});
case ActionTypes.SET_SELECTED:
return (<any>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 (<any>Object).assign({}, state, {
selectedEntries: selectedEntries
});
case ActionTypes.ACTIVE:
return (<any>Object).assign({}, state, {
active: action.payload
});
default:
return state;
}
}