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

Bug when API payload has the same structure as an action #61

Closed
mrtnbroder opened this issue Feb 18, 2016 · 10 comments · Fixed by #65
Closed

Bug when API payload has the same structure as an action #61

mrtnbroder opened this issue Feb 18, 2016 · 10 comments · Fixed by #65
Labels
bug Unexpected behavior or broken functionality

Comments

@mrtnbroder
Copy link

Not sure if this is actually a bug but I just came across this really edge-case-y thing where my API payload had the same structure like an action, which caused me a 3h headache why my promise never reached the fulfilled part, even though the API call was successfull.

My Action looks like this:

export const createCar = (data) => ({
  type: types.CREATE_CAR,
  payload: {
    promise: API.createCar(data)
  }
})

API:

API.createCar = function createCar(data) {
    return fetch(`${API_URL}/cars`, {
      method: 'POST',
      body: JSON.stringify(data)
    }).then((r) => r.json())

Reducer:

  ...

  [`${types.CREATE_CAR}_PENDING`]: (state) => {
    return {
      ...state,
      isPending: true
    }
  },

  [`${types.CREATE_CAR}_FULFILLED`]: (state, { payload }) => {
    return {
      ...state,
      isPending: false,
      data: {
        ...state.data,
        [payload.id]: payload
      }
    }
  },
  ...

Now I was wondering why my action never reached the 'fulfilled' state. This was because my API payload looked like this:

{
  id: 12345
  type: 'car',
  payload: {
    foo: 'bar',
    ...etc.
  }
}

What now happened is that this payload got fired as an action and it never reached the fulfilled state because of that.

I wonder if there is some way around that? currently I'm wrapping the api response in another object so it reaches the fulfilled state but I feel like this should not actually happen. It should always reach one of my pending...fulfilled...rejected states.

@mrtnbroder mrtnbroder changed the title Bug when API payload has the same structure Bug when API payload has the same structure as an action Feb 18, 2016
@tomatau
Copy link
Collaborator

tomatau commented Feb 19, 2016

This is a tough one! We want to support being able to fire a new action meta in the fulfilled/rejected case and reuse the existing type.

The use case for this was one of my own - some middleware use data in the meta for performing side effects with the payload, and often you want these effects to only apply to rejected/fulfilled and not pending. So we support a new action dispatch when the resolved object has a meta or payload.

Right now to check if that fulfilled/rejected object is an action that gets dispatched, we check for either meta or payload properties... we could remove the check for a payload property and only look for meta. Or even make sure the resolved object has both meta and payload. I don't see a use-case for when the resolved object just has a payload property - it wouldn't give any benefit to dispatch it as an action in that case.

But we would still have the same problem if a resolved payload wanted to contain it's own 'meta' property.

@pburtchaell what do you think?

@pburtchaell
Copy link
Owner

@tomatau Perhaps we could make firing new action meta optional by moving it into the middleware configuration? This way @mrtnbroder could just disable that functionality and everything would work as expected.

@tomatau
Copy link
Collaborator

tomatau commented Feb 21, 2016

Yeh that would work! Not sure if it feels right though? Would be nice if the middleware had consistent behaviour.

I was thinking it might be ok to remove the functionality from the object resolved by a Promise -- as the thunk functionality can also achieve the same goal. It's a bit more verbose to use a thunk in the resolve, but less 'hidden'. Magically making a new action sometimes and just making a payload depending on what is resolved is starting to feel a bit wrong in general.

@pburtchaell
Copy link
Owner

Okay, so remove the functionality and release a new major version? I would be fine with that since I haven't actually used this functionality yet. Do you want to make a PR?

@tomatau
Copy link
Collaborator

tomatau commented Feb 23, 2016

I'm super busy at the moment so don't think I'll be able to! Although it is literally just deleting code and tests :)

@pburtchaell
Copy link
Owner

No worries, just seeing if you could. I'll be able to take care of this
over the weekend.
On Feb 23, 2016 03:19, "Thomas" notifications@github.com wrote:

I'm super busy at the moment so don't think I'll be able to! Although it
is literally just deleting code and tests :)


Reply to this email directly or view it on GitHub
#61 (comment)
.

@mrtnbroder
Copy link
Author

Any update on this? 👯

@pburtchaell
Copy link
Owner

I am working on it! I have a branch I started locally for a version 3.0.0 release and this will be included in it. I hope to finish it up soon.

@Cinamonas
Copy link

Just stumbled upon this. The API I am consuming is responding with objects that have meta key.

@pburtchaell pburtchaell added the bug Unexpected behavior or broken functionality label Mar 19, 2016
@pburtchaell
Copy link
Owner

This is fixed in the new API release: redux-promise-middleware@3.0.0. Please let me know if you have any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected behavior or broken functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants