Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic reducer factory automatically handling async-actions - createAsyncReducer #88

Open
lajtmaN opened this issue Oct 19, 2018 · 5 comments
Assignees
Labels
💵 Funded on Issuehunt This issue has been funded on Issuehunt high priority new feature
Milestone

Comments

@lajtmaN
Copy link

lajtmaN commented Oct 19, 2018

Issuehunt badges

Is your feature request related to a problem?

It would be awesome to be able to build reducer factories which are based on async actions, that can in a generic way handle the payloads of that async action.

Who does this impact? Who is this for?

All users - as an example you could create an async action and create a reducer with a simple one liner for simple reducers like loading and error.

Example

The following example utilizes that all async failure actions return an Error object.

export const fetchCurrentUserActions = createAsyncAction(...)<void, FetchCurrentUserDetailsResponse, Error>();


export const currentUserReducer = combineReducers<CurrentUserState>({
  loading: createLoadingReducer(fetchCurrentUserActions), // <---- Nice one line reducer
  error: createErrorReducer(fetchCurrentUserActions), // <---- Nice one line reducer
  ....
});

export const createErrorReducer = <TSuccess>(
  asyncAction: AsyncActionBuilder<any, any, any, any, TSuccess, Error>
): Reducer<ErrorState> => 
  (state = null, action: ActionType<typeof asyncAction>) => {
    switch (action.type) {
      case getType(asyncAction.failure):
        return action.payload; // <---- action is not narrowed after getType(), thus .payload doesn't exist on action or isn't of type Error as expected
      case getType(asyncAction.success):
        return null;
      default:
        return state;
    }
  };

My createLoadingReducer does not care about any payloads thus my reducer function just takes an AnyAction


IssueHunt Summary

Backers (Total: $115.00)

Become a backer now!

Or submit a pull request to get the deposits!

Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

@IssueHuntBot
Copy link

@issuehuntfest has funded $20.00 to this issue. See it on IssueHunt

@ecnaidar
Copy link

ecnaidar commented Feb 8, 2019

Hey,
this would be very nice fix have, currently typecasting things manually after the switch and although it works, leaves room for error and verbose.
Is this related to piotrwitek/react-redux-typescript-guide#121 ?
If so, probably possible to replace custom factories with one that will be added from that issue.

@lajtmaN
Copy link
Author

lajtmaN commented Feb 8, 2019

I don't think typecasting is a solution since this is such a generic reducer, that it needs to handle the types correctly before the concept can be widely used.
When piotrwitek adds some documentation on how the partial reducers will work I will try to give this reducer factory a spin again with that method.

@piotrwitek piotrwitek self-assigned this Feb 11, 2019
@piotrwitek piotrwitek changed the title Reducer factory for async actions Add a generic reducer factory automatically handling async-actions - createAsyncReducer Feb 11, 2019
@IssueHuntBot
Copy link

@IssueHunt has funded $75.00 to this issue.


@issuehunt-oss
Copy link

issuehunt-oss bot commented Aug 26, 2019

@sposterkil has funded $20.00 to this issue.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💵 Funded on Issuehunt This issue has been funded on Issuehunt high priority new feature
Projects
None yet
Development

No branches or pull requests

4 participants