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

pathvars and body in store + multiple actions => weird behaviour #153

Open
xurei opened this issue Jul 12, 2017 · 2 comments
Open

pathvars and body in store + multiple actions => weird behaviour #153

xurei opened this issue Jul 12, 2017 · 2 comments
Labels

Comments

@xurei
Copy link
Contributor

xurei commented Jul 12, 2017

This issue is related to #152, that have been merged in version 0.10.7

If several actions are bound to the same reducer (with the reducerName option), the pathvars and body will contain the latest action done on it. That might not be the expected behaviour.

Example :

const rest = reduxApi({
  getUser: {
    reducerName: "user"
    url: "/user/:id", // return a user object
  },
  updateUser: {
    reducerName: "user"
    url: "/user/:id/update", 
    options: {
      method: "post"
    },
    transformer: function(data, prevData, action) {
      /* update the store content based on the data received */
    }
  }
});

/* ... */

dispatch(rest.actions.getUser({id:1})); 
// store will contain { pathvars: {id:1}, body: {} }

dispatch(rest.actions.updateUser({ id:1 }, {
  body: {message: 'hello world'}
}));
// store will contain { pathvars: {id:1}, body: {message: 'hello world'} }

The content of body is kinda weird : the store still contains the results of the first call, with the update.
It would make sense that the body stays empty.

This gets even worse with a poorly coded API, where updateUser does not contain any pathvar :

const rest = reduxApi({
  getUser: {
    reducerName: "user"
    url: "/user/:id", // return a user object
  },
  updateUser: {
    reducerName: "user"
    url: "/user/update", 
    options: {
      method: "post"
    },
    transformer: function(data, prevData, action) {
      /* update the store content based on the data received */
    }
  }
});

/* ... */

dispatch(rest.actions.getUser({id:1})); 
// store will contain { pathvars: {id:1}, body: {} }

dispatch(rest.actions.updateUser({ id:1 }, {
  body: {user_id:1, message: 'hello world'}
}));
// store will contain { pathvars: {}, body: {user_id:1, message: 'hello world'} }

This can also be a problem when the request gives an error : pathvars and body will change, but not the content.

How should we handle such cases ?

@lexich
Copy link
Owner

lexich commented Jul 18, 2017

@xurei It's a correct behaviour. State hold the last request. We can add flag to save request data or skip it. What do you think about it?

@xurei
Copy link
Contributor Author

xurei commented Jul 19, 2017

Yes I was thinking of something like that...
Although I'm not fan, I cannot see any better option, so let's do that.

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