From b0bdfa211fd8c66987eaf0ca1687f047f40aa906 Mon Sep 17 00:00:00 2001 From: Igor Carvalho Date: Fri, 12 Jul 2024 17:53:59 -0300 Subject: [PATCH] refactor: store likes API (#2661) * feat: create usePostLikeComponent hook to handle store like API * refactor: use usePostLikeComponent hook to handle store likes * [autofix.ci] apply automated fixes * refactor: use mutate loading instead of managing loading with react state --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../cardComponent/hooks/use-handle-like.tsx | 51 ------------------- .../src/components/cardComponent/index.tsx | 49 ++++++++++++------ .../src/controllers/API/helpers/constants.ts | 1 + .../controllers/API/queries/store/index.ts | 1 + .../queries/store/use-post-like-component.ts | 25 +++++++++ 5 files changed, 61 insertions(+), 66 deletions(-) delete mode 100644 src/frontend/src/components/cardComponent/hooks/use-handle-like.tsx create mode 100644 src/frontend/src/controllers/API/queries/store/index.ts create mode 100644 src/frontend/src/controllers/API/queries/store/use-post-like-component.ts diff --git a/src/frontend/src/components/cardComponent/hooks/use-handle-like.tsx b/src/frontend/src/components/cardComponent/hooks/use-handle-like.tsx deleted file mode 100644 index bfda076be..000000000 --- a/src/frontend/src/components/cardComponent/hooks/use-handle-like.tsx +++ /dev/null @@ -1,51 +0,0 @@ -import { postLikeComponent } from "../../../controllers/API"; -import { storeComponent } from "../../../types/store"; - -const useLikeComponent = ( - data: storeComponent, - name: string, - setLoadingLike: (value: boolean) => void, - likedByUser: boolean | null | undefined, - likesCount: number, - setLikedByUser: (value: any) => void, - setLikesCount: (value: any) => void, - setValidApiKey: (value: boolean) => void, - setErrorData: (value: { title: string; list: string[] }) => void, -) => { - const handleLike = () => { - setLoadingLike(true); - if (likedByUser !== undefined || likedByUser !== null) { - const temp = likedByUser; - const tempNum = likesCount; - setLikedByUser((prev) => !prev); - setLikesCount((prev) => (temp ? prev - 1 : prev + 1)); - - postLikeComponent(data.id) - .then((response) => { - setLoadingLike(false); - setLikesCount(response.data.likes_count); - setLikedByUser(response.data.liked_by_user); - }) - .catch((error) => { - setLoadingLike(false); - setLikesCount(tempNum); - setLikedByUser(temp); - if (error.response.status === 403) { - setValidApiKey(false); - } else { - console.error(error); - setErrorData({ - title: `Error liking ${name}.`, - list: [error.response.data.detail], - }); - } - }); - } - }; - - return { - handleLike, - }; -}; - -export default useLikeComponent; diff --git a/src/frontend/src/components/cardComponent/index.tsx b/src/frontend/src/components/cardComponent/index.tsx index a2372e768..7fe406f88 100644 --- a/src/frontend/src/components/cardComponent/index.tsx +++ b/src/frontend/src/components/cardComponent/index.tsx @@ -1,3 +1,4 @@ +import { usePostLikeComponent } from "@/controllers/API/queries/store"; import { useEffect, useState } from "react"; import { createRoot } from "react-dom/client"; import { Control } from "react-hook-form"; @@ -29,7 +30,6 @@ import { FormControl, FormField } from "../ui/form"; import Loading from "../ui/loading"; import useDataEffect from "./hooks/use-data-effect"; import useInstallComponent from "./hooks/use-handle-install"; -import useLikeComponent from "./hooks/use-handle-like"; import useDragStart from "./hooks/use-on-drag-start"; import usePlaygroundEffect from "./hooks/use-playground-effect"; import { convertTestName } from "./utils/convert-test-name"; @@ -61,7 +61,6 @@ export default function CollectionCardComponent({ const cleanFlowPool = useFlowStore((state) => state.CleanFlowPool); const isStore = false; const [loading, setLoading] = useState(false); - const [loadingLike, setLoadingLike] = useState(false); const [likedByUser, setLikedByUser] = useState(data?.liked_by_user ?? false); const [likesCount, setLikesCount] = useState(data?.liked_by_count ?? 0); const [downloadsCount, setDownloadsCount] = useState( @@ -123,17 +122,37 @@ export default function CollectionCardComponent({ setErrorData, ); - const { handleLike } = useLikeComponent( - data, - name, - setLoadingLike, - likedByUser, - likesCount, - setLikedByUser, - setLikesCount, - setValidApiKey, - setErrorData, - ); + const { mutate, isPending } = usePostLikeComponent(); + + const handleLikeWMutate = () => { + if (likedByUser !== undefined || likedByUser !== null) { + const temp = likedByUser; + const tempNum = likesCount; + setLikedByUser((prev) => !prev); + setLikesCount((prev) => (temp ? prev - 1 : prev + 1)); + mutate( + { componentId: data.id }, + { + onSuccess: (res) => { + setLikesCount(res.data.likes_count); + setLikedByUser(res.data.liked_by_user); + }, + onError: (error) => { + setLikesCount(tempNum); + setLikedByUser(temp); + if (error.response.status === 403) { + return setValidApiKey(false); + } + console.error(error); + setErrorData({ + title: `Error liking ${name}.`, + list: [error.response.data.detail], + }); + }, + }, + ); + } + }; const isSelectedCard = selectedFlowsComponentsCards?.includes(data?.id) ?? false; @@ -365,7 +384,7 @@ export default function CollectionCardComponent({ } >