Skip to content

Commit

Permalink
Feat: 즐겨찾기 관련 api 생성 (#54)
Browse files Browse the repository at this point in the history
* feat: 즐겨찾기 추가 api

* feat: 즐겨찾기 삭제 기능 추가

* fix: DB 선택문 변경
  • Loading branch information
aazkgh authored Sep 24, 2024
1 parent e67f070 commit 1806fbe
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/apis/supabase/postAddFavorite.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 1806fbe

Please sign in to comment.