From 9cd7b2a67d55d91e676024c18e727974c690ec09 Mon Sep 17 00:00:00 2001 From: Alibek Date: Tue, 20 Feb 2024 00:33:33 +0600 Subject: [PATCH] made like --- src/features/ReportLike/ReportLike.tsx | 44 ++++++++++++++++---------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/features/ReportLike/ReportLike.tsx b/src/features/ReportLike/ReportLike.tsx index 7ba1e42..3e84991 100644 --- a/src/features/ReportLike/ReportLike.tsx +++ b/src/features/ReportLike/ReportLike.tsx @@ -6,6 +6,7 @@ import like from "./icons/like.svg"; import { apiInstance } from "@/shared/config/apiConfig"; import { useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; +import { useEffect } from "react"; interface IReportLikeProps { count: number; @@ -17,31 +18,40 @@ const ReportLike: React.FC = ({ report_id, }: IReportLikeProps) => { const session = useSession(); + const router = useRouter(); - const getFans = async () => { - const res = await apiInstance.get( - `/report/${report_id}/get_fans` + const Authorization = `Bearer ${session?.data?.access_token}`; + const config = { + headers: { + Authorization, + }, + }; + + const checkLike = async () => { + const res = await apiInstance.get<{ detail: string }>( + `/report/${report_id}/like/check/`, + config ); return res.data; }; const likeReport = async () => { - const Authorization = `Bearer ${session?.data?.access_token}`; - const config = { - headers: { - Authorization, - }, - }; - - // console.log(getFans()); - - const res = await apiInstance.post( - `/report/${report_id}/like/`, - {}, - config - ); + const isLiked = await checkLike(); + if (isLiked.detail === "Like does not exist!") { + await apiInstance.post( + `/report/${report_id}/like/`, + {}, + config + ); + } else { + await apiInstance.post( + `/report/${report_id}/unlike/`, + {}, + config + ); + } router.refresh(); };