diegohaz / arc

React starter kit based on Atomic Design
https://arc.js.org
2.91k stars 297 forks source link

[redux] Entities/resource pattern with redux-saga-thunk #273

Open brandoniffert opened 7 years ago

brandoniffert commented 7 years ago

From the redux-saga-thunk docs:

store.dispatch(resourceCreateRequest({ title: 'foo' })).then((detail) => {
  // detail is the action payload property
  console.log('Yaay!', detail)
}).catch((error) => {
  // error is the action payload property
  console.log('Oops!', error)
})

I'm using the entities/resource pattern outlined in this repo and it seems that detail above is always the id of the resource I am creating/updating. I'm assuming this is so it works with the entities/normalizr pattern? Is there any way to have it return the actual entity object or get access to it somehow from within the resource dispatch promise? I am using resource dispatches a lot in redux-form onSubmits.

Thank you

diegohaz commented 7 years ago

Does this work for you?

import { fromEntities } from 'store/selectors'

store.dispatch(resourceCreateRequest('posts', { title: 'foo' })).then((id) => {
  const detail = fromEntities.getDetail(store.getState(), 'posts', id)
  console.log('Yaay!', detail)
})
brandoniffert commented 7 years ago

Seems like it would but I am trying to use it inside a redux-form onSubmit function so I wouldn't necessarily have access to the current state.

https://github.com/diegohaz/arc/blob/redux-ssr/src-example/containers/PostForm.js#L10

const onSubmit = (data, dispatch) => dispatch(resourceCreateRequest('posts', data))

Trying to do something like that and then have access to the response object from the create/update API call all within onSubmit.

alduro commented 6 years ago

@brandoniffert I'm facing the same issue. Did you solve that someway ? Tks.