From 1806fbe136f47e20f21b34dd1ad70f3d8e97622c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=A0=EA=B0=80=ED=98=95?= <101045330+aazkgh@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:38:17 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=A6=90=EA=B2=A8=EC=B0=BE=EA=B8=B0=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20api=20=EC=83=9D=EC=84=B1=20(#54)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 즐겨찾기 추가 api * feat: 즐겨찾기 삭제 기능 추가 * fix: DB 선택문 변경 --- src/apis/supabase/postAddFavorite.ts | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/apis/supabase/postAddFavorite.ts diff --git a/src/apis/supabase/postAddFavorite.ts b/src/apis/supabase/postAddFavorite.ts new file mode 100644 index 0000000..559c0b0 --- /dev/null +++ b/src/apis/supabase/postAddFavorite.ts @@ -0,0 +1,39 @@ +import { useLocation } from 'react-router-dom'; + +import { unitripSupabase } from '@/utils/supabaseClient'; + +const toggleFavorite = async () => { + const kakaoId = sessionStorage.getItem('kakao_id'); + + const location = useLocation(); + const queryParams = new URLSearchParams(location.search); + const place = queryParams.get('contentId'); + + const { data, error: fetchError } = await unitripSupabase + .from('USER') + .select('favorite_list') + .eq('kakao_id', kakaoId); + + if (fetchError) { + throw new Error('사용자 정보를 가져오는 데 문제가 발생했습니다'); + } + + const currentFavorites = data[0].favorite_list || []; + //기존 배열에 해당 장소가 존재하면 제거, 존재하지 않으면 추가 + const updatedFavorites = currentFavorites.includes(place) + ? currentFavorites.filter((favorite: number) => favorite !== Number(place)) + : [...currentFavorites, Number(place)]; + + const { error } = await unitripSupabase + .from('USER') + .update({ + favorite_list: updatedFavorites, + }) + .eq('kakao_id', kakaoId); + + if (error) { + throw new Error('서버에 문제가 있습니다'); + } +}; + +export default toggleFavorite;