fix(market-card.tsx): fix logic for updating likes_count and liked_by_user state variables

The logic for updating the `likes_count` and `liked_by_user` state variables in the `handleLike` function was incorrect. This commit fixes the logic to correctly update the state variables based on the previous values. Additionally, it adds error handling for the `postLikeComponent` API call and sets the `likes_count` and `liked_by_user` state variables to their previous values in case of an error.
This commit is contained in:
anovazzi1 2023-11-01 10:14:00 -03:00
commit 4310229c2f
2 changed files with 7 additions and 2 deletions

View file

@ -6,7 +6,6 @@ from typing import TYPE_CHECKING, List, Dict, Any, Optional, Union
import httpx
from httpx import HTTPError
from langflow.services.database.models import user
from langflow.services.store.schema import (
ComponentResponse,
DownloadComponentResponse,

View file

@ -72,19 +72,25 @@ export const MarketCardComponent = ({ data }: { data: storeComponent }) => {
function handleLike() {
if (liked_by_user !== undefined || liked_by_user !== null) {
const temp = liked_by_user;
const tempNum = likes_count;
setLiked_by_user((prev) => !prev);
if (!temp) {
setLikes_count((prev) => prev + 1);
} else {
setLikes_count((prev) => prev - 1);
}
console.log(data.id);
postLikeComponent(data.id)
.catch((error) => {
console.error(error);
setLiked_by_user(temp);
setLikes_count(tempNum);
setErrorData({
title: "Error on liking component",
list: [error["response"]["data"]["detail"]],
});
})
.then((response) => {
console.log(response);
setLikes_count(response.likes_count);
setLiked_by_user(response.liked_by_user);
});