Skip to content

Commit fd29597

Browse files
committed
[feat]댓글 작성 토스트
1 parent 0572d20 commit fd29597

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/domains/recipe/api/fetchRecipeComment.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
import { getApi } from '@/app/api/config/appConfig';
22
import { CommentType } from '@/domains/community/types/post';
33

4-
export const postRecipeComment = async (cocktailId: number, content: string) => {
5-
try {
6-
const res = await fetch(`${getApi}/cocktails/${cocktailId}/comments`, {
7-
method: 'POST',
8-
headers: { 'Content-Type': 'application/json' },
9-
credentials: 'include',
10-
body: JSON.stringify({ content }),
11-
});
12-
const text = await res.text();
13-
if (!res.ok) throw new Error('댓글 작성 실패');
14-
const data = JSON.parse(text);
15-
return data;
16-
} catch (err) {
17-
console.error(err);
18-
}
19-
};
20-
214
export const getRecipeComment = async (cocktailId: number): Promise<CommentType[] | null> => {
225
try {
236
const res = await fetch(`${getApi}/cocktails/${cocktailId}/comments`, {
@@ -74,7 +57,7 @@ export async function deleteRecipeComment(
7457
});
7558

7659
if (!response.ok) {
77-
const errorText = await response.text(); // 👈 응답 본문을 텍스트로 읽기
60+
const errorText = await response.text();
7861
console.error('서버 응답 에러:', errorText);
7962
throw new Error(`댓글 수정 실패: ${response.status}`);
8063
}

src/domains/recipe/api/useRecipeComment.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getApi } from '@/app/api/config/appConfig';
33
import { User } from '@/domains/shared/store/auth';
44
import { CommentType } from '@/domains/community/types/post';
55
import { deleteRecipeComment, getRecipeComment, updateComment } from './fetchRecipeComment';
6+
import { useToast } from '@/shared/hook/useToast';
67

78
export function useRecipeComments(
89
cocktailId: number,
@@ -16,6 +17,7 @@ export function useRecipeComments(
1617
commentId: number;
1718
cocktailId: number;
1819
} | null>(null);
20+
const { toastError } = useToast();
1921

2022
const fetchData = useCallback(async () => {
2123
const data = await getRecipeComment(cocktailId);
@@ -30,7 +32,7 @@ export function useRecipeComments(
3032

3133
const handleUpdateComment = async (commentId: number, content: string) => {
3234
if (!user) {
33-
alert('로그인이 필요합니다');
35+
toastError('로그인이 필요합니다');
3436
return;
3537
}
3638
try {
@@ -44,7 +46,7 @@ export function useRecipeComments(
4446
);
4547
} catch (err) {
4648
console.error(err);
47-
alert('댓글 수정 중 오류가 발생했습니다.');
49+
toastError('댓글 수정 중 오류가 발생했습니다.');
4850
}
4951
};
5052

@@ -54,7 +56,7 @@ export function useRecipeComments(
5456

5557
const handleConfirmDelete = async () => {
5658
if (!user) {
57-
alert('로그인이 필요합니다');
59+
toastError('로그인이 필요합니다');
5860
return;
5961
}
6062
if (!deleteTarget) return;
@@ -66,7 +68,6 @@ export function useRecipeComments(
6668
);
6769
} catch (err) {
6870
console.error(err);
69-
alert('댓글 삭제 중 오류가 발생했습니다.');
7071
} finally {
7172
setDeleteTarget(null);
7273
}

src/domains/recipe/components/details/RecipeComment.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import CommentHeader from '@/domains/shared/components/comment/CommentHeader';
22
import CommentList from '@/domains/shared/components/comment/CommentList';
3-
import { postRecipeComment } from '../../api/fetchRecipeComment';
43
import { useAuthStore } from '@/domains/shared/store/auth';
54
import { useShallow } from 'zustand/shallow';
65
import ConfirmModal from '@/shared/components/modal-pop/ConfirmModal';
76
import { useRecipeComments } from '../../api/useRecipeComment';
7+
import { getApi } from '@/app/api/config/appConfig';
8+
import { useToast } from '@/shared/hook/useToast';
89

910
interface Props {
1011
cocktailId: number;
@@ -17,6 +18,28 @@ function RecipeComment({ cocktailId }: Props) {
1718
accessToken: state.accessToken,
1819
}))
1920
);
21+
const { toastInfo } = useToast();
22+
23+
const postRecipeComment = async (cocktailId: number, content: string) => {
24+
try {
25+
const res = await fetch(`${getApi}/cocktails/${cocktailId}/comments`, {
26+
method: 'POST',
27+
headers: { 'Content-Type': 'application/json' },
28+
credentials: 'include',
29+
body: JSON.stringify({ content }),
30+
});
31+
const text = await res.text();
32+
if (!res.ok) {
33+
toastInfo('댓글은 한 개만 작성가능합니다');
34+
return;
35+
}
36+
const data = JSON.parse(text);
37+
return data;
38+
} catch (err) {
39+
console.error(err);
40+
}
41+
};
42+
2043
const {
2144
comments,
2245
fetchData,

0 commit comments

Comments
 (0)