omichelsen / redux-promise-middleware-actions

Redux action creator for making async actions compatible with redux-promise-middleware
MIT License
32 stars 3 forks source link

createReducer is missing metadata #13

Open sregg opened 5 years ago

sregg commented 5 years ago

First of all, congrats for the new createReducer. This is super helpful as we don't have to define types anymore!

The only issue I'm seeing is that I can't seem to grab the metadata, only the payload in fulfilled state

omichelsen commented 4 years ago

Could you give an example where you need it, just so I am sure I fully understand the use case?

sregg commented 4 years ago

For instance if I have a list of items, I'd like to differentiate the initial API call (loading indicator centered) from the pull-to-refresh (loading indicator at the top) so I'm using a flag isPullToRefresh in my metadata. Then based on that metadata I'm setting my state isLoading or isPullRefreshing.

  switch (action.type) {
    case String(actionLoadResidentBalances.pending):
      const isPullRefreshing = action.meta?.isPullRefreshing ?? false;
      return {
        ...state,
        isLoadingCurrentBalances: !isPullRefreshing,
        isRefreshingCurrentBalances: isPullRefreshing,
      };
stephenhand commented 1 year ago

To elaborate - in my experimentation, the meta data is present on the async actions. It just isn't present on the type inferred for each of the async actions.

If you cast the type back to the original action type, you can access the meta property and the data is present as expected, e.g.

export const asyncAction = createAsyncAction(
  'myAction', 
  () => Promise.resolve('a payload'), 
  () => ({ someValue: 'something'}) 
);

export const reducer = createReducer(initialState, handleAction => [
  handleAction(asyncAction.pending as typeof asyncAction, (state, action) => {
    return {
      ...state,
      someProperty: action.meta.someValue
  }),
]);