import * as actionTypes from "../actions/actionTypes"; import initialState from "./initialState"; export default function groupsReducer(state = initialState.groups, action) { switch (action.type) { case actionTypes.CREATE_GROUP: { let group = action.group; if (state.length === 0) { group.id = 1; } else { group.id = Math.max.apply( Math, state.map(function(g) { return g.id; }) ) + 1; } return [...state, { ...group }]; } case actionTypes.LOAD_GROUPS_SUCCESS: { return action.groups; } case actionTypes.GET_GROUP_BY_ID: { let group = state.find(x => x.id === action.group.id); return group; } default: return state; } }