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

Fire multiple requests for a mutation. #90

Closed
binchik opened this issue Nov 3, 2017 · 2 comments
Closed

Fire multiple requests for a mutation. #90

binchik opened this issue Nov 3, 2017 · 2 comments
Labels

Comments

@binchik
Copy link

binchik commented Nov 3, 2017

Hello, big thanks for redux-query. I'm converting my project from redux-saga to it. Already have most of the requests handled by redux-query.

I have to do two POST requests sometimes. For example the first one would upload an image and return its id, and the second one would attach that id to the second request's body. What would be the best way to go about this?

@binchik binchik closed this as completed Nov 3, 2017
@binchik binchik reopened this Nov 3, 2017
@ryanashcraft
Copy link
Contributor

@binchik Glad you get value out of redux-query! By the way, I wouldn't think of redux-query as a full on replacement of redux-saga, you can use both together!

Anyways, take a look at the promise-chain notes in the README in the mutateAsync section: https://github.com/amplitude/redux-query#mutateasync

Here's an illustrative example for your situation. (Warning this might have typos – I haven't run this or anything).

// Query configs

const uploadImageQueryConfig = imageData => ({
  url: '/image/upload',
  body: { imageData },
  update: {},
});

const somethingElseQueryConfig = id => ({
  url: '/image/something-else',
  body: { id },
  update: {
    myEntity: (oldValue, newValue) => newValue,
  },
});

// Redux actions

const uploadImageAction = imageData => mutateAsync(uploadImageQueryConfig(imageData));

const somethingElseAction = id => mutateAsync(somethingElseQueryConfig(id));

// react-redux container

const mapDispatchToProps = dispatch => {
  return {
    uploadImage: imageData => {
      dispatch(imageData).then(result => {
        if (result.status === 200) {
          dispatch(somethingElseAction(result.body.id));
        }
      });
    },
  };
}

@binchik
Copy link
Author

binchik commented Nov 4, 2017

@ryanashcraft Thaks, this is awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants