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:
parent
787a725207
commit
4310229c2f
2 changed files with 7 additions and 2 deletions
|
|
@ -6,7 +6,6 @@ from typing import TYPE_CHECKING, List, Dict, Any, Optional, Union
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
from httpx import HTTPError
|
from httpx import HTTPError
|
||||||
from langflow.services.database.models import user
|
|
||||||
from langflow.services.store.schema import (
|
from langflow.services.store.schema import (
|
||||||
ComponentResponse,
|
ComponentResponse,
|
||||||
DownloadComponentResponse,
|
DownloadComponentResponse,
|
||||||
|
|
|
||||||
|
|
@ -72,19 +72,25 @@ export const MarketCardComponent = ({ data }: { data: storeComponent }) => {
|
||||||
function handleLike() {
|
function handleLike() {
|
||||||
if (liked_by_user !== undefined || liked_by_user !== null) {
|
if (liked_by_user !== undefined || liked_by_user !== null) {
|
||||||
const temp = liked_by_user;
|
const temp = liked_by_user;
|
||||||
|
const tempNum = likes_count;
|
||||||
setLiked_by_user((prev) => !prev);
|
setLiked_by_user((prev) => !prev);
|
||||||
|
if (!temp) {
|
||||||
|
setLikes_count((prev) => prev + 1);
|
||||||
|
} else {
|
||||||
|
setLikes_count((prev) => prev - 1);
|
||||||
|
}
|
||||||
console.log(data.id);
|
console.log(data.id);
|
||||||
postLikeComponent(data.id)
|
postLikeComponent(data.id)
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
setLiked_by_user(temp);
|
setLiked_by_user(temp);
|
||||||
|
setLikes_count(tempNum);
|
||||||
setErrorData({
|
setErrorData({
|
||||||
title: "Error on liking component",
|
title: "Error on liking component",
|
||||||
list: [error["response"]["data"]["detail"]],
|
list: [error["response"]["data"]["detail"]],
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
console.log(response);
|
|
||||||
setLikes_count(response.likes_count);
|
setLikes_count(response.likes_count);
|
||||||
setLiked_by_user(response.liked_by_user);
|
setLiked_by_user(response.liked_by_user);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue