-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Updated Comments likes UI changes to be instant #21861
base: main
Are you sure you want to change the base?
Conversation
apps/comments-ui/src/actions.ts
Outdated
// if error we revert the state | ||
return { | ||
comments: commentsState, | ||
commentLikeLoadingId: null | ||
}; |
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.
This seems dangerous as other state changes could have happened if there was a long delay before the endpoint eventually errored. I would expect a rollback to only occur for the liked status of the comment under change, would that make things considerably more difficult?
apps/comments-ui/src/actions.ts
Outdated
return { | ||
commentUnlikeLoading: null | ||
}; |
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.
Do we need this? It feels clunky and limits you to a single id which may not be true if network is slow and multiple like buttons were clicked before a request finishes.
Can we keep likeComment
as it was where it awaits the API response but change it to first dispatch an action that does an immediate state update? That way the component that dispatches likeComment
can await as normal and manage the button disabling with the typical pattern of internal component state updates around an async action but the global state still updates immediately in the background to show the like/unlike status. If we add like/unlike state-update actions then it also makes the rollback (inside the likeComment
action) easier if the API request fails as it's just another dispatch.
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.
Pseudo-code:
async function likeComment(id) {
dispatchAction('updateCommentLikeState', {id, liked: true});
try {
await api.likeComment(id);
return null;
} catch (e) {
dispatchAction('updateCommentLikeState', {id, liked: false});
// throw e; // optional, depends whether we want to handle the error inside components
return null;
}
}
async handleLikeClick() {
setIsSaving(true);
await dispatchAction('likeComment', id);
setIsSaving(false)
}
ref #21861 (comment) - simplified disabled logic to be within the component instead of globally. - added state change in a single dispatch query that can handle like / unlike as well as fallback handling. - updated test
ref PLG-288