adam-golab/redux-phoenix

Feature Request : Use _.mergeWith instead of _.merge

ThibautMarechal opened this issue · 0 comments

I have an issue when I rehydrate my store with an array value.

Basicaly, I save some columns that the user can modify (add, remove, move).
I also have a default value for the columns.

The problem is :

  • columns = [A, B, C, D] (default value)
    The user start using the app and changes the columns that he want in his use case.
  • columns = [D, B, C]
    The user reopen the app.
    When calling _.merge the result of the rehydration will be = [D, B, C, D]
    The user don't see the columns that he has previously selected. (And D is duplicated due to the merge)

It could be great to export a new autoRehydrate function that allow the usage of _mergeWith

Proposition: (I do not have an idea for the naming 😄 )

[...]
export const rehydrateCustomizer = mergeCustomizer  => next => (reducer, initialState, enhancer) => {
  if (typeof initialState === 'function' && typeof enhancer === 'undefined') {
    enhancer = initialState;
    initialState = undefined; // eslint-disable-line no-undefined
  }
  function rehydrateReducer(state, action) {
    if (action.type === REHYDRATE) {
      return reducer(_.mergeWith({}, [state, action.payload], mergeCustomizer), action);
    }
    return reducer(state, action);
  }
  return next(rehydrateReducer, initialState, enhancer);
}
export const autoRehydrate = rehydrateCustomizer()
};