Skip to content

Commit 31f79a3

Browse files
committed
Feat: 게시글 삭제기능 추가 및 업로드 api 정리 #65
1 parent fb62d35 commit 31f79a3

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

components/pages/main/Feed/FeedHeader/Upload.tsx

+14-39
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use client';
22

3-
import { useState } from 'react';
3+
import { useEffect, useState } from 'react';
44
import Toast from '@/utils/notification';
5-
import { postBoard } from '@/service/board';
5+
import usePostBoard from '@/hooks/mutations/usePostBoard';
66
import Avatar from '../../../../ui/Avatar';
77
import Button from '../../../../ui/Button';
88
import FlexBox from '../../../../ui/FlexBox';
@@ -15,38 +15,17 @@ export default function Upload({
1515
nickname: string | undefined;
1616
}) {
1717
const [postText, setPostText] = useState('');
18-
const [isUploading, setIsUploading] = useState(false);
18+
const { mutate: postBoard, isLoading, isSuccess } = usePostBoard(postText);
1919

2020
const maxCharacters = 100;
2121
const isOverMaxChar = postText.length > maxCharacters;
2222

2323
const handleTextChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
2424
setPostText(event.target.value);
2525
};
26-
27-
// 데이터 전송을 위한 함수
28-
const onUploadBoard = async () => {
29-
if (!postText || isOverMaxChar) {
30-
return;
31-
}
32-
setIsUploading(true);
33-
try {
34-
const response = await postBoard({
35-
title: 'title',
36-
content: postText,
37-
});
38-
if (response.content) {
39-
Toast.success('게시물이 성공적으로 업로드되었습니다.');
40-
setPostText('');
41-
} else {
42-
Toast.error('업로드에 실패했습니다. 잠시 후 다시 시도해주세요.');
43-
}
44-
} catch (error) {
45-
Toast.error('오류가 발생했습니다. 잠시 후 다시 시도해주세요.');
46-
} finally {
47-
setIsUploading(false);
48-
}
49-
};
26+
useEffect(() => {
27+
if (isSuccess) setPostText('');
28+
}, [isSuccess]);
5029

5130
return (
5231
<form className="flex flex-col bg-primary-50 p-5 border-[1px] border-primary-300 rounded-[10px] w-full">
@@ -82,18 +61,14 @@ export default function Upload({
8261
>
8362
파일
8463
</Button>
85-
{isUploading ? (
86-
<div>업로드 중입니다</div>
87-
) : (
88-
<Button
89-
size="lg"
90-
disabled={isOverMaxChar}
91-
className="w-40"
92-
onClickAction={() => onUploadBoard()}
93-
>
94-
업로드
95-
</Button>
96-
)}
64+
<Button
65+
size="lg"
66+
disabled={postText.length === 0 || isOverMaxChar || isLoading}
67+
className="w-40"
68+
onClickAction={() => postBoard()}
69+
>
70+
{isLoading ? '업로드 중입니다' : '업로드'}
71+
</Button>
9772
<div className="mt-4" id="renderedText" />
9873
</FlexBox>
9974
</form>

hooks/mutations/usePostBoard.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { postBoard } from '@/service/board';
2+
import Toast from '@/utils/notification';
3+
import { useMutation, useQueryClient } from '@tanstack/react-query';
4+
5+
export default function usePostBoard(postText: string) {
6+
const queryClient = useQueryClient();
7+
const { mutate, isLoading, isSuccess } = useMutation({
8+
mutationFn: () => postBoard({ title: 'title', content: postText }),
9+
onSuccess: () => {
10+
Toast.success('게시글이 성공적으로 업로드 되었습니다.');
11+
queryClient.invalidateQueries(['boardList']);
12+
},
13+
onError: () => {
14+
Toast.error(
15+
'게시글을 업로드하지 못했습니다. 잠시후 다시 시도해주세요.🥲',
16+
);
17+
},
18+
});
19+
return { mutate, isLoading, isSuccess };
20+
}

0 commit comments

Comments
 (0)