-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 즐겨찾기 추가 api * feat: 즐겨찾기 삭제 기능 추가 * fix: DB 선택문 변경
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |