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

Extend createAsyncAction API to be able to carry over request information to success and failure handlers #73

Closed
abersnaze opened this issue Sep 4, 2018 · 6 comments

Comments

@abersnaze
Copy link

I have a bunch of epics that have a similar pattern.

export const fooAction = createAsyncAction(
    'FOO_INIT',
    'FOO_DONE',
    'FOO_ERROR')<Foo, Bar, Error>();

The problem that I'm having is that the Error type loses the request value (of type Foo). To properly mutated the state in the reducer I've had to compose the Error and Foo together in the epic. So the async action creator now looks like this.

export const fooAction = createAsyncAction(
    'FOO_INIT',
    'FOO_DONE',
    'FOO_ERROR')<Foo, [Foo, Bar], [Foo, Error]>();

I was wondering if the request payload can be carried over to the success and failure actions?

InputAction would be the same { type: string, payload: I }
OutputAction would be { type: string, payload: O, request: I }
ErrorAction would be { type: string, payload: E, request: I }

@piotrwitek
Copy link
Owner

Hi @abersnaze,
I think it should work, could you please provide full usage snippets including reducer so I can have more insight.

@Carl-Foster
Copy link
Contributor

This seems like a specific use case, seems strange to add this to this library.

@piotrwitek
Copy link
Owner

@Carl-Foster I think the intention here is to add a more generic API to createAsyncAction with an ability to extend action with custom properties. We can already do it with createStandardAction().map API, so it would stay consistent.

export const add = createStandardAction(ADD).map(
  ({ title }: { title: string }) => ({
    payload: { title, id: cuid(), completed: false } as Todo,
	request: 'whatever you want',
  })
);

To be honest I was thinking about adding something similar initially on createAsyncAction, but it was too big effort at that time so I dropped it.

I'll come back to this issue again at some point when I solve some other pending issues that are more important ATM.

@piotrwitek piotrwitek changed the title Propagate request payload to result & failure actions. Extend createAsyncAction API to be able to handle custom properties on action object Dec 3, 2018
@IssueHuntBot
Copy link

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

@IssueHuntBot
Copy link

@IssueHunt has funded $60.00 to this issue.


@piotrwitek
Copy link
Owner

Here the solution to the requested feature with the new v5.0.0 API using meta arguments:

export const fooAction = createAsyncAction(
    'FOO_INIT',
    'FOO_DONE',
    'FOO_ERROR'
)<Foo, [Bar, Foo], [Error, Foo]>();

fooAction.request(foo);
fooAction.success(bar, foo);
fooAction.failure(err, foo);

Results:
InputAction would be the same { type: string, payload: I }
OutputAction would be { type: string, payload: O, meta: I }
ErrorAction would be { type: string, payload: E, meta: I }

@piotrwitek piotrwitek changed the title Extend createAsyncAction API to be able to handle custom properties on action object Extend createAsyncAction API to be able to carry over request information to success and failure handlers Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants