Skip to content

Commit

Permalink
Fix: 'LikeButton' non-persistent liked style
Browse files Browse the repository at this point in the history
- Fixed by swapping the 'toggle' method for 'add' method
at 'styleButton' function to always persist the liked style
on the 'LikeButton' component when user keep liking the same
project.
- Simplify all the 'setWasLiked' method logic to just
toggle the 'wasLiked' state value, without the need to
check the previous value.
  • Loading branch information
ITurres committed Apr 11, 2024
1 parent 0f8348e commit 83a8bd0
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/components/UI/LikeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function LikeButton(props: LikeButtonProps): React.ReactElement {

const styleButton = useCallback(() => {
if (wasLiked) {
likeBtn.current?.classList.toggle('liked');
likeBtn.current?.classList.add('liked');
}
}, [wasLiked]);

Expand Down Expand Up @@ -100,13 +100,15 @@ function LikeButton(props: LikeButtonProps): React.ReactElement {
const handleLikeSubmit = async () => {
// ? Since 'wasLiked' was false, now it will be set to true. This is to
// ? trigger the function 'styleButton' to add the 'liked' class to the button.
setWasLiked((wasLiked) => !wasLiked);
setWasLiked(true);

// * shows an instant update of the like count.
if (!wasLiked) {
setLikeCount((count) => count + 1);
// * set 'wasLiked' to false, so that the user can like the project again.
setWasLiked((wasLiked) => !wasLiked);
setTimeout(() => {
setWasLiked(false);
}, 1000);
}

if (projectsLikes[0].error || error) {
Expand Down

0 comments on commit 83a8bd0

Please sign in to comment.