-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add redux comments store #3143
Add redux comments store #3143
Conversation
} ); | ||
|
||
// if the api have returned comments count, dispatch it | ||
if ( totalCommentsCount > -1 ) { |
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.
In which cases totalCommentsCount
can be -1 (or less)?
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.
Most of the times the api will return found === -1 (don't think it can be less than -1, -1 is to indicate "unable/error" I guess.), since I use before filter on the request to fetch more comments, whenever a filter is used, api would not return the total comments count.
The only time it returns the count is when the request is first made without any filters.
fa067e3
to
899c782
Compare
We just landed a move to lodash 4.x, so we'll need to rework this to take that into account. See https://github.com/lodash/lodash/wiki/Changelog#compatibility-warnings for a list of changes. |
74a972e
to
8cc3c4f
Compare
* @param {Function|Object} updaterOrValue an updater function or a value | ||
* @returns {Immutable.Map} new state | ||
*/ | ||
function updateSpecificState( state, action, updaterOrValue ) { |
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.
How about updateCommentState( state, id, updaterOrValue )
? Since we're only using action
to generate an id, it makes it clearer if we change the function signature to accept the id instead. It should make mocking the tests a bit shorter 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.
The reason I've done it like that is because I didn't want to call createCommentTargetId( action.siteId, action.postId )
in every reducer and capture the logic of getting the specific state (and the generation of it's ID) in a single place, It is after all a helper function =)
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.
If every handler uses the id, you can create the id before the switch statement.
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.
@gwwar Every reducer needs the id, not just every handler at the switch...
Because of the structure we chose.
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'll elaborate a bit more.
If I understand you correctly, you're suggesting factoring out the id generation from the updateSpecificState
function,
What it'll mean in my opinion, that in each reducer ( items
, requests
, totalCommentsCount
),
i'll have to repeat ID creation:
function items( state, action ) {
const id = createCommentTargetId( action.siteId, action.postId ); // DRY
switch ( action.type ) {
...
}
}
in my opinion, it's better not to repeat.
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.
Passing exactly what the function needs, helps keep the intent clearer and makes refactoring easier. This is a helper function, so not a blocker here.
I'm still 👀 I'll try to finish up tomorrow. |
@gwwar Thanks for reviewing that! |
* @param {Boolean} updateAll whether update all matches of predicate or just the first | ||
* @returns {Immutable.List} modified list | ||
*/ | ||
export function listFindAndModify( list, predicate, updater, updateAll = false ) { |
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.
You could probably do the single item case with findIndex
and updateIn
. Where do you use the multi item case?
https://facebook.github.io/immutable-js/docs/#/List/findIndex
https://facebook.github.io/immutable-js/docs/#/Map/updateIn
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 don't use the multi case.
It's still 2 not chain-able method calls I need to encapsulate in some function.
You're suggesting remove the multi case?
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.
You're suggesting remove the multi case?
If we're not using it, yeah. No reason to have it.
It's still 2 not chain-able method calls I need to encapsulate in some function.
Having the function is fine. Just thinking we can simplify it and use more idiomatic immutable methods.
Maybe for a name updateExistingIn
? Given how Immutable works, this could really work for List or Map.. It's pretty much like updateIn
, but it wouldn't create missing keys.
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.
Sure, I'll make that later tonight.
Update: done
case SERIALIZE: | ||
return state.toJS(); | ||
case DESERIALIZE: | ||
return Immutable.fromJS( state ); |
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.
If we want to persist this state tree, we'll want to add a JSON Schema for each subtree. We can also defer this work by temporarily choosing to not persist state, and add it in a future PR.
case SERIALIZE:
return {};
case DESERIALIZE:
return Immutable.Map();
The documentation update is still open, but if you're interested you can read some of the reasoning here: #3267
Let me know if you need help with adding schemas.
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'd hold off on persisting this bit of the state tree for now.
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've removed the serialization.
👍 Great progress! Before merging
|
Looks like #3545 landed, so we should switch over to that from |
a81bcc3
to
d0913be
Compare
d0913be
to
2af9474
Compare
a828768
to
63908b4
Compare
#3627 is merged, so this PR should be good to go imo. |
Looks good to me. @gwwar any objections to landing this? |
👍 🚢 |
🚢 🎉 🎈 |
This PR lays some ground work for issues #2839, #967, and #792 .
Current status is that the code is mainly done, what left IMO, are some style adjustments and documentation.
Your opinion is appreciated.
#2839:
The comments pulled by date in DESCending order, while a tree of comments is being build in the store.
This is achieved by calling
requestPostComments()
action creator, subsequent calls will fetch the earlier (older) dated comments.#967:
The comments are built in a tree-like structure, that allows us to see in each comment which are it's children and how many children the comment has.
It can be used something like that:
#792: Polling can be done by invoking `pollComments()` action creator ( from `` maybe? ) after first `requestPostComments()`, it'll fetch the new comments ( comments that got approved and meet the time-frame and entirely new comments ) and remove comments that got unapproved.It is very inefficient however, and it also can't detect edited comments.
No polling support in this PR.