lore-extract-reducer

Extracts the reducer blueprint for a model to src/reducers

find

CLI command to extract the find reducer for a given model into your project.

Usage

lore extract reducer [model]/find

Example

lore extract reducer post/find
import { ActionTypes } from 'lore-utils';
import _ from 'lodash';

/*
 * find Reducer Blueprint
 */

function mergeDataAndIntersectWithDictionaryKeys(oldData, newData, dictionary) {
  const ids = _.map(oldData, 'id');
  const newIds = _.map(newData, 'id');
  const combinedIds = _.union(ids, newIds);
  const dictionaryKeys = _.keys(dictionary);

  // If any ids are Numbers, convert them to Strings so we can compare equality
  // with the dictionary keys
  const combinedIdsAsStrings = combinedIds.map(function(id) {
    return id.toString();
  });

  const idsInDictionary = _.intersection(combinedIdsAsStrings, dictionaryKeys);
  return idsInDictionary.map(function(id) {
    return dictionary[id];
  });
}

const initialState = {};

export default function find(state, action, options) {
  state = state || initialState;

  let nextState = _.assign({}, state);
  const byId = options.nextState.byId;
  const byCid = options.nextState.byCid;

  switch (action.type) {
    case ActionTypes.fetchPlural('post'): {
      const query = JSON.stringify(action.query);
      nextState[query] = nextState[query] || {};
      nextState[query] = _.assign({}, action.payload, {
        data: mergeDataAndIntersectWithDictionaryKeys(
          nextState[query].data,
          action.payload.data,
          byId
        )
      });
      return nextState;
    }

    case ActionTypes.update('post'): {
      nextState = _.mapValues(nextState, function(collection, key) {
        collection.data = collection.data.map(function(datum) {
          if (datum.id === action.payload.id) {
            return action.payload;
          }
          return datum;
        });

        return collection;
      });
      return nextState;
    }

    case ActionTypes.remove('post'): {
      nextState = _.mapValues(nextState, function(collection, key) {
        collection.data = collection.data.map(function(datum) {
          if (datum.id === action.payload.id) {
            return action.payload;
          }
          return datum;
        });

        return collection;
      });
      return nextState;
    }

    default:
      return nextState
  }
};