Complex State Update with API Call to Persist Data #540
Unanswered
jperna7254
asked this question in
Q&A
Replies: 1 comment 2 replies
-
|
I'm not sure I follow, but Effects should really be for talking to external sources rather than coordinating stuff. I would do this
If you wish, you could have both actions descend from a base or implement a common interface and have your reducer act on that, so you don't have to write the same reducer code twice. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am creating a quoting tool which has quote items and groups of quote items. I am trying to implement the feature that allows the user to modify the order of the groups or individual quote items. There is a decent amount of complexity sometimes with moving items in and out of groups and to the beginning and end of the quote. As a result I think I should move some of the complexity to an EffectMethod, and then dispatch an action that has just the updated data so reducing the old state and the action together is simple in the ReducerMethod. However, I also need to persist the changes made to indices and groupIds of the quote items to the database through an API call, which will inevitably be asynchronous. I would also love to have a snappy optimistic update of the state done right when the user tries to move the row, rather than wait for the success/failure of the API call.
With these issues the only thing that seems reasonable is to dispatch a MoveRowIntentAction, have a EffectMethod(typeof(MoveRowAction)), with that effect method doing the indexing swapping logic then dispatching a MoveRowAction and sending a fire and forget API call to persist the change, then having a ReducerMethod(typeof(MoveRowAction)).
The only thing that worries me about this solution is the fire and forget API call. It also seems convoluted so I wanted to post about it and see if anyone has had a similar problem they found an elegant solution to. Thank you all!
Edit: i am now thinking doing an initial synchronous effect to figure out the index changes would work for this. Then dispatch the update action where the reducer can respond immediately then another effect can do the api call in the background.
Beta Was this translation helpful? Give feedback.
All reactions