Skip to content

Commit 1bca233

Browse files
committed
refactor: 데이터 조회, 업데이트, 삭제 기능 hook 분리
1 parent a85369a commit 1bca233

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed
139 KB
Loading

apps/next-client/src/hooks/queries/usePostEdit.ts

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ import { API_URLS } from '@/constants/urls';
66
import { ALERT_MESSAGES } from '@/constants/alertMessage';
77
import { ERROR_MESSAGES } from '@/constants/errors';
88

9-
export const usePostEdit = (id: string, userId: string | undefined, accessToken: string) => {
10-
const [title, setTitle] = useState('');
11-
const [content, setContent] = useState('');
9+
// 게시글 데이터 가져오기
10+
export function usePostEditData(id: string, userId: string | undefined) {
1211
const router = useRouter();
13-
const queryClient = useQueryClient();
14-
15-
// 게시글 데이터 가져오기
16-
const { data } = useSuspenseQuery({
12+
13+
return useSuspenseQuery({
1714
queryKey: ['post-edit', id],
1815
queryFn: async () => {
1916
const response = await axiosInstance.get(`${API_URLS.POSTS}/${id}`);
@@ -30,18 +27,15 @@ export const usePostEdit = (id: string, userId: string | undefined, accessToken:
3027
staleTime: 5 * 60 * 1000,
3128
gcTime: 10 * 60 * 1000,
3229
});
30+
}
3331

34-
// 데이터로부터 상태 설정
35-
useEffect(() => {
36-
if (data) {
37-
setTitle(data.title);
38-
setContent(data.content);
39-
}
40-
}, [data]);
32+
// 게시글 업데이트 기능
33+
export function useUpdatePost(id: string, accessToken: string) {
34+
const router = useRouter();
35+
const queryClient = useQueryClient();
4136

42-
// 게시글 업데이트
43-
const updatePostMutation = useMutation({
44-
mutationFn: async () => {
37+
return useMutation({
38+
mutationFn: async ({ title, content }: { title: string; content: string }) => {
4539
if (!title.trim() || !content.trim()) {
4640
alert(ALERT_MESSAGES.ERROR.POST.POST_EMPTY_FIELDS);
4741
throw new Error(ERROR_MESSAGES.EMPTY_FIELDS);
@@ -63,9 +57,14 @@ export const usePostEdit = (id: string, userId: string | undefined, accessToken:
6357
alert(ALERT_MESSAGES.ERROR.POST.POST_UPDATE_ERROR);
6458
}
6559
});
60+
}
6661

67-
// 게시글 삭제
68-
const deletePostMutation = useMutation({
62+
// 게시글 삭제 기능
63+
export function useDeletePost(id: string, accessToken: string) {
64+
const router = useRouter();
65+
const queryClient = useQueryClient();
66+
67+
return useMutation({
6968
mutationFn: async () => {
7069
if (!window.confirm(ALERT_MESSAGES.CONFIRM.CHECK_DELETE)) {
7170
throw new Error(ERROR_MESSAGES.DELETE_CANCELLED);
@@ -85,9 +84,25 @@ export const usePostEdit = (id: string, userId: string | undefined, accessToken:
8584
alert(ALERT_MESSAGES.ERROR.POST.POST_DELETE_ERROR);
8685
}
8786
});
87+
}
88+
89+
export const usePostEdit = (id: string, userId: string | undefined, accessToken: string) => {
90+
const [title, setTitle] = useState('');
91+
const [content, setContent] = useState('');
92+
93+
const { data } = usePostEditData(id, userId);
94+
const updatePostMutation = useUpdatePost(id, accessToken);
95+
const deletePostMutation = useDeletePost(id, accessToken);
96+
97+
useEffect(() => {
98+
if (data) {
99+
setTitle(data.title);
100+
setContent(data.content);
101+
}
102+
}, [data]);
88103

89104
const handleUpdatePost = () => {
90-
updatePostMutation.mutate();
105+
updatePostMutation.mutate({ title, content });
91106
};
92107

93108
const handleDeletePost = () => {

0 commit comments

Comments
 (0)