|
1 | | -import { useEffect, useState } from 'react'; |
| 1 | +import { useState, useEffect } from 'react'; |
2 | 2 | import { useRouter } from 'next/navigation'; |
3 | | -import { useQueryClient } from '@tanstack/react-query'; |
| 3 | +import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; |
4 | 4 | import { axiosInstance } from '@/services/common/axiosInstance'; |
5 | 5 | import { API_URLS } from '@/constants/urls'; |
6 | 6 | import { ALERT_MESSAGES } from '@/constants/alertMessage'; |
7 | 7 |
|
8 | | -export const usePost = (id: string, userId: string | undefined, accessToken: string) => { |
| 8 | +export const usePostEdit = (id: string, userId: string | undefined, accessToken: string) => { |
9 | 9 | const [title, setTitle] = useState(''); |
10 | 10 | const [content, setContent] = useState(''); |
11 | | - const [loading, setLoading] = useState(true); |
12 | 11 | const router = useRouter(); |
13 | 12 | const queryClient = useQueryClient(); |
14 | 13 |
|
15 | | - useEffect(() => { |
16 | | - // 게시글 가져오기 함수 |
17 | | - const fetchPost = async () => { |
18 | | - try { |
19 | | - const response = await axiosInstance.get(`${API_URLS.POSTS}/${id}`); |
20 | | - const post = response.data; |
21 | | - |
22 | | - if (post.userId !== userId) { |
23 | | - alert(ALERT_MESSAGES.ERROR.POST.POST_PERMISSION_DENIED); |
24 | | - router.push('/community'); |
25 | | - return; |
26 | | - } |
| 14 | + // 게시글 조회 |
| 15 | + const { isLoading: loading, error, data } = useQuery({ |
| 16 | + queryKey: ['post-edit', id], |
| 17 | + queryFn: async () => { |
| 18 | + const response = await axiosInstance.get(`${API_URLS.POSTS}/${id}`); |
| 19 | + const post = response.data; |
27 | 20 |
|
28 | | - setTitle(post.title); |
29 | | - setContent(post.content); |
30 | | - } catch (error) { |
31 | | - console.error('Error fetching post:', error); |
32 | | - alert(ALERT_MESSAGES.ERROR.POST.POST_FETCH_ERROR); |
| 21 | + if (post.userId !== userId) { |
| 22 | + alert(ALERT_MESSAGES.ERROR.POST.POST_PERMISSION_DENIED); |
33 | 23 | router.push('/community'); |
34 | | - } finally { |
35 | | - setLoading(false); |
| 24 | + throw new Error('Permission denied'); |
36 | 25 | } |
37 | | - }; |
| 26 | + |
| 27 | + return post; |
| 28 | + }, |
| 29 | + enabled: !!id && !!userId, |
| 30 | + staleTime: 5 * 60 * 1000, |
| 31 | + gcTime: 10 * 60 * 1000, |
| 32 | + }); |
38 | 33 |
|
39 | | - fetchPost(); |
40 | | - }, [id, userId, router]); |
| 34 | + useEffect(() => { |
| 35 | + if (data) { |
| 36 | + setTitle(data.title); |
| 37 | + setContent(data.content); |
| 38 | + } |
| 39 | + }, [data]); |
41 | 40 |
|
42 | | - // 게시글 업데이트 함수 |
43 | | - const handleUpdatePost = async () => { |
44 | | - try { |
| 41 | + useEffect(() => { |
| 42 | + if (error) { |
| 43 | + console.error('Error fetching post:', error); |
| 44 | + alert(ALERT_MESSAGES.ERROR.POST.POST_FETCH_ERROR); |
| 45 | + router.push('/community'); |
| 46 | + } |
| 47 | + }, [error, router]); |
| 48 | + |
| 49 | + // 게시글 업데이트 |
| 50 | + const updatePostMutation = useMutation({ |
| 51 | + mutationFn: async () => { |
45 | 52 | if (!title.trim() || !content.trim()) { |
46 | 53 | alert(ALERT_MESSAGES.ERROR.POST.POST_EMPTY_FIELDS); |
47 | | - return; |
| 54 | + throw new Error('Empty fields'); |
48 | 55 | } |
49 | 56 |
|
50 | | - await axiosInstance.put( |
| 57 | + return axiosInstance.put( |
51 | 58 | `${API_URLS.POSTS}/${id}`, |
52 | 59 | { title, content }, |
53 | 60 | { headers: { Authorization: `Bearer ${accessToken}` } } |
54 | 61 | ); |
55 | | - |
| 62 | + }, |
| 63 | + onSuccess: () => { |
56 | 64 | alert(ALERT_MESSAGES.SUCCESS.POST.POST_UPDATE); |
57 | | - |
58 | 65 | queryClient.invalidateQueries({ queryKey: ['post', id] }); |
59 | | - |
60 | 66 | router.push(`/community/${id}`); |
61 | | - } catch (error) { |
| 67 | + }, |
| 68 | + onError: (error) => { |
62 | 69 | console.error('Error updating post:', error); |
63 | 70 | alert(ALERT_MESSAGES.ERROR.POST.POST_UPDATE_ERROR); |
64 | 71 | } |
65 | | - }; |
| 72 | + }); |
66 | 73 |
|
67 | | - // 게시글 삭제 함수 |
68 | | - const handleDeletePost = async () => { |
69 | | - try { |
70 | | - if (!window.confirm(ALERT_MESSAGES.CONFIRM.CHECK_DELETE)) return; |
| 74 | + // 게시글 삭제 |
| 75 | + const deletePostMutation = useMutation({ |
| 76 | + mutationFn: async () => { |
| 77 | + if (!window.confirm(ALERT_MESSAGES.CONFIRM.CHECK_DELETE)) { |
| 78 | + throw new Error('Delete cancelled'); |
| 79 | + } |
71 | 80 |
|
72 | | - await axiosInstance.delete(`${API_URLS.POSTS}/${id}`, { |
73 | | - headers: { Authorization: `Bearer ${accessToken}` }, |
| 81 | + return axiosInstance.delete(`${API_URLS.POSTS}/${id}`, { |
| 82 | + headers: { Authorization: `Bearer ${accessToken}` } |
74 | 83 | }); |
75 | | - |
| 84 | + }, |
| 85 | + onSuccess: () => { |
76 | 86 | alert(ALERT_MESSAGES.SUCCESS.POST.POST_DELETE); |
| 87 | + queryClient.invalidateQueries({ queryKey: ['posts'] }); |
77 | 88 | router.push('/community'); |
78 | | - } catch (error) { |
| 89 | + }, |
| 90 | + onError: (error) => { |
79 | 91 | console.error('Error deleting post:', error); |
80 | 92 | alert(ALERT_MESSAGES.ERROR.POST.POST_DELETE_ERROR); |
81 | 93 | } |
| 94 | + }); |
| 95 | + |
| 96 | + const handleUpdatePost = () => { |
| 97 | + updatePostMutation.mutate(); |
| 98 | + }; |
| 99 | + |
| 100 | + const handleDeletePost = () => { |
| 101 | + deletePostMutation.mutate(); |
82 | 102 | }; |
83 | 103 |
|
84 | 104 | return { title, setTitle, content, setContent, loading, handleUpdatePost, handleDeletePost }; |
|
0 commit comments