Skip to content
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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

ronaldlangeveld
Copy link
Member

@ronaldlangeveld ronaldlangeveld commented Dec 11, 2024

ref PLG-288

  • Implemented instant UI updates for comment likes/unlikes using optimistic rendering.
  • Enhanced error handling reverts state on API failure, ensuring consistency.
  • Added new test helper to mock failed API requests, needed to test revert state handling.

@ronaldlangeveld ronaldlangeveld changed the title [wip] Make comments likes instant Updated Comments likes UI changes to be instant Dec 12, 2024
@ronaldlangeveld ronaldlangeveld marked this pull request as ready for review December 12, 2024 04:29
Comment on lines 198 to 202
// if error we revert the state
return {
comments: commentsState,
commentLikeLoadingId: null
};
Copy link
Member

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?

Comment on lines 255 to 257
return {
commentUnlikeLoading: null
};
Copy link
Member

@kevinansfield kevinansfield Dec 12, 2024

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.

Copy link
Member

@kevinansfield kevinansfield Dec 12, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants