-
Notifications
You must be signed in to change notification settings - Fork 297
Make combineActions works with nested handleActions #294
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Make combineActions works with nested handleActions #294
Conversation
Codecov Report
@@ Coverage Diff @@
## master #294 +/- ##
==========================================
+ Coverage 95.45% 95.61% +0.15%
==========================================
Files 14 14
Lines 110 114 +4
Branches 36 36
==========================================
+ Hits 105 109 +4
Misses 4 4
Partials 1 1
Continue to review full report at Codecov.
|
Thanks! I'll look into this asap. |
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.
Could you explain me from an API perspective what exactly is being added here and why that isn't possible now? I was out of this library for longer time and many things have been added since then. After reading #241 and your example I'm not 100% sure and also using the term "nested" in combination with handleActions
and combineActions
confuses me a little bit. Thanks.
I think it's best if you could update the documentation accordingly.
test/handleActions.test.js
Outdated
} | ||
}); | ||
|
||
// NOTE: We should be using combineReducers in production, but this is just a test. |
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.
What does this comment mean, because
combineReducers
doesn't exist- if
combineActions
is meant, it is being used
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 just copy-pasted the previous test 🙃
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.
So we can remove it as I don't see that it makes any sense.
@timche To be honest, I wanted to use a "nested" Currently, if you write your handleActions({
NOT_NESTED: function,
[combineActions(action1, action2)]: {
NESTED: function,
}
}) Only |
Thanks @Kerumen for explaining. I'm not questioning your pull request, it's totally fine and appreciate it. Please don't get me wrong, I'm just trying to understand and to make everyone clear what we are trying to achieve as this must also be documented in the documentation and change log. Also regarding the left-over comment in the test, it doesn't mean we can't do better and fix it, right? Do I understand correctly that |
I don't know how the library was designed. I'm sure that this is not documented for
So a combination of both should work? |
Alright, so it really sounds more like a bug to me. So we are basically fixing |
test/handleActions.test.js
Outdated
DECREMENT: amount => ({ amount: -amount }) | ||
} | ||
} | ||
}); |
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 createAction
in L536 and L542. Shouldn't it be only at L542? counter
and sameCounter
can 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
.. with combineActions
.
So I dispatch API_CALL
and handle API_CALL_SUCCESS
. This is why I have 2 createActions
. 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?
const { app } = createActions({
APP: {
COUNTER: {
INCREMENT: amount => ({ amount }),
DECREMENT: amount => ({ amount: -amount })
},
SAME_COUNTER: {
INCREMENT: amount => ({ amount }),
DECREMENT: amount => ({ amount: -amount })
}
}
});
const { counter, sameCounter } = app
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)
. combineActions
expect actions, not keys. I can rewrite L536 with:
const app: { counter, sameCounter } = createActions('APP/COUNTER', 'APP/SAME_COUNTER')
..if you prefer.
Or totally change action names and do something like API_CALL
and API_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.
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.
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 undefined
in API_CALL_1: { LOADING: undefined }
. Why is it defined as undefined
? 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
SET: undefined // given undefined, the identity function will be used
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 undefined
will use the identity function instead, but I'm more concerned about using createActions
twice 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.
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 LOADING
action 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!
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.
Happy to merge it asap when finished discussing about the test.
@timche Friendly ping, I made a comment in the diff :) |
@timche I've renamed the variables to better match my app and my intention. Is that clearer? Sorry for that, I can't find a proper way to write this test.. |
@timche Can you publish a new version with this change on npm please? Thanks! |
Sorry for the delay. Published. |
Fixes #241 (I used the code @Parakleta suggested)
It allows nesting of actions in
handleActions
after acombineActions
(very useful for loadings):I had some troubles to write a clean test, let me know if you want to improve it.