score:0

Accepted answer
import {CREATE_CATEGORY, EDIT_CATEGORY, DELETE_CATEGORY, FETCH_CATEGORY, FETCH_CATEGORIES, FETCH_TOP_CATEGORIES} from "../actions/categoriesActionTypes";
import _ from 'lodash';

const initState = {
  categories: []
};

export default (state = initState, action) => {
    switch (action.type) {
        case CREATE_CATEGORY:
        case EDIT_CATEGORY:
        case FETCH_CATEGORY:
            return {
                ...state,
                categories: {
                    ...state.categories,
                    [action.payload.id]: action.payload
                }
            };

        case DELETE_CATEGORY:
            return {
                ...state,
                categories: _.omit(state.categories, action.payload),
                topCategories: _.omit(state.topCategories, action.payload)
            };

        case FETCH_CATEGORIES:
            return {...state, categories:_.mapKeys(action.payload, 'id')};

        case FETCH_TOP_CATEGORIES:
            return {...state, topCategories: _.mapKeys(action.payload, 'id')};

        default:
            return state;
    }
}

Related Query

More Query from same tag