@@ -4,6 +4,11 @@ import { useAuthStore } from '@/domains/shared/store/auth';
44import { useToast } from '@/shared/hook/useToast' ;
55import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
66
7+ interface Comment {
8+
9+ userNickName : string
10+ }
11+
712export const postRecipeComment = async ( cocktailId : number , content : string ) => {
813 const body = {
914 cocktailId,
@@ -18,6 +23,9 @@ export const postRecipeComment = async (cocktailId: number, content: string) =>
1823 body : JSON . stringify ( body ) ,
1924 } ) ;
2025
26+
27+ if ( res . status === 401 ) throw new Error ( 'unauth' )
28+
2129 const text = await res . text ( ) ;
2230 const data = JSON . parse ( text ) ;
2331 return data ;
@@ -72,21 +80,20 @@ export function useRecipeComment({ cocktailId }: { cocktailId: number }) {
7280 queryFn : ( ) => getRecipeComment ( cocktailId ) ,
7381 staleTime : 30_000 ,
7482 } ) ;
75-
83+ console . log ( comments )
84+ console . log ( user )
85+ const hasComment = comments . some ( ( c :Comment ) => c . userNickName === user ?. nickname ) ;
7686 const createMut = useMutation ( {
7787 mutationFn : ( content : string ) => {
7888 if ( ! user ?. id ) {
7989 toastInfo ( '로그인 후 이용 가능합니다.' ) ;
80- return Promise . reject ( new Error ( 'unauth' ) ) ;
90+ return Promise . resolve ( null )
91+ } else if ( hasComment ) {
92+ toastInfo ( '댓글은 한 개만 작성 가능합니다.' )
8193 }
8294 return postRecipeComment ( cocktailId , content ) ;
8395 } ,
8496 onSuccess : ( ) => refetch ( ) ,
85- onError : ( e ) => {
86- if ( e . message !== 'unauth' ) {
87- toastInfo ( '댓글은 한개만 작성 가능합니다' ) ;
88- }
89- } ,
9097 } ) ;
9198
9299 const updateMut = useMutation ( {
0 commit comments