-
Notifications
You must be signed in to change notification settings - Fork 295
Make combineActions works with nested handleActions #294
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -529,3 +529,54 @@ test('works with nested reducerMap and identity handlers', () => { | |
| message: 'hello' | ||
| }); | ||
| }); | ||
|
|
||
| test('works with combineActions nested', () => { | ||
| const { | ||
| app: { counter, sameCounter } | ||
| } = createActions({ | ||
| APP: { | ||
| COUNTER: undefined, | ||
| SAME_COUNTER: undefined | ||
| } | ||
| }); | ||
| const { app } = createActions({ | ||
| APP: { | ||
| COUNTER: { | ||
| INCREMENT: amount => ({ amount }), | ||
| DECREMENT: amount => ({ amount: -amount }) | ||
| }, | ||
| SAME_COUNTER: { | ||
| INCREMENT: amount => ({ amount }), | ||
| DECREMENT: amount => ({ amount: -amount }) | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // NOTE: We should be using combineReducers in production, but this is just a test. | ||
|
||
| const reducer = handleActions( | ||
| { | ||
| [combineActions(counter, sameCounter)]: { | ||
| INCREMENT: ({ counter }, { payload: { amount } }) => ({ | ||
| counter: counter + amount | ||
| }), | ||
| DECREMENT: ({ counter }, { payload: { amount } }) => ({ | ||
| counter: counter + amount | ||
| }) | ||
| } | ||
| }, | ||
| { counter: 0 } | ||
| ); | ||
|
|
||
| expect(reducer({ counter: 3 }, app.counter.increment(2))).toEqual({ | ||
| counter: 5 | ||
| }); | ||
| expect(reducer({ counter: 10 }, app.counter.decrement(3))).toEqual({ | ||
| counter: 7 | ||
| }); | ||
| expect(reducer({ counter: 3 }, app.sameCounter.increment(2))).toEqual({ | ||
| counter: 5 | ||
| }); | ||
| expect(reducer({ counter: 10 }, app.sameCounter.decrement(3))).toEqual({ | ||
| counter: 7 | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the double usage of
createActionin L536 and L542. Shouldn't it be only at L542?counterandsameCountercan be destructured there instead too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes maybe I should rename the actions of L536. In my app I have an API call (which is an action) and I handle
SUCCESS,LOADING.. withcombineActions.So I dispatch
API_CALLand handleAPI_CALL_SUCCESS. This is why I have 2createActions. I tried to reproduce this here but maybe it's confusing. 😐There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, but still couldn't you just remove L536 and do this instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timche This doesn't work because of line L558:
combineActions(counter, sameCounter).combineActionsexpect actions, not keys. I can rewrite L536 with:..if you prefer.
Or totally change action names and do something like
API_CALLandAPI_CALL_SUCCESS?I know this is not very clear but the initials dispatched (and combined) actions have to be different than the one nested in the combine.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, didn't have much time to look into that and try it out myself. I just want it to be changed so the code makes sense. Doesn't matter how and to what, it just must be readable and clear.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still not very clear from my perspective because of using
undefinedinAPI_CALL_1: { LOADING: undefined }. Why is it defined asundefined? It just doesn't look right and is also not documented.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's totally documented: https://redux-actions.js.org/api-reference/createaction-s#createaction-type-payloadcreator and also used in an example below
But if you prefer I can write
createAction('API_CALL_1/LOADING'). This way, I use explicitly the/separator which is implicit in the object form.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that
undefinedwill use the identity function instead, but I'm more concerned about usingcreateActionstwice and the first one is just being used for creating action types and the second to create action creators which is overall looks quite confusing imo.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know.. As I said, in a real app, the
LOADINGaction would have been created else where (in a middleware for example) so it won't be confusing.However, this is a simple test ensuring the fix works, maybe we can pass through and improve it later?
I've changed it a few times and I don't have many options left. If you propose me something, I would be happy to include it, I'm currently out of ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think this is too much nitpicking from my side, sorry. After all, this issue is fixed. Thanks!