diff --git a/front-end/src/app/posts/[id]/edit/page.tsx b/front-end/src/app/posts/[id]/edit/page.tsx index 2f1590d..0f7a541 100644 --- a/front-end/src/app/posts/[id]/edit/page.tsx +++ b/front-end/src/app/posts/[id]/edit/page.tsx @@ -2,43 +2,81 @@ import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; +import { useSelector } from 'react-redux'; import { updatePost } from '@/app/api/postAPI'; import apiClient from '@/components/axios/apiClient'; +import { RootState } from '@/redux/store'; import '@/styles/pages/post/post.scss'; +interface Post { + id: number; + title: string; + content: string; + category: 'FREE' | 'QNA'; + writtenAt: string; + email: string; + nickname: string; +} + export default function EditPost({ params }: { params: { id: string } }) { const router = useRouter(); + const userState = useSelector((state: RootState) => state.user); + const currentUser = userState.userInfo; + const [title, setTitle] = useState(''); const [content, setContent] = useState(''); const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + const [post, setPost] = useState(null); useEffect(() => { const fetchPost = async () => { try { - const response = await apiClient.get(`/post/${params.id}`); - const { title, content } = response.data; - setTitle(title); - setContent(content); + // 게시글 목록에서 해당 게시글 찾기 + let foundPost = null; + const categories = ['FREE', 'QNA']; + for (const category of categories) { + const response = await apiClient.get(`/post/category/${category}?page=0`); + foundPost = response.data.content.find((post: Post) => post.id.toString() === params.id); + if (foundPost) break; + } + + if (!foundPost) { + throw new Error('게시글을 찾을 수 없습니다.'); + } + + // 현재 사용자가 게시글 작성자인지 확인 + if (currentUser && foundPost.email !== currentUser.email) { + throw new Error('게시글을 수정할 권한이 없습니다.'); + } + + setPost(foundPost); + setTitle(foundPost.title); + setContent(foundPost.content); } catch (error) { console.error('게시글 불러오기 실패:', error); - alert('게시글을 불러오지 못했습니다.'); - router.back(); + setError(error instanceof Error ? error.message : '게시글을 불러오지 못했습니다.'); } finally { setLoading(false); } }; fetchPost(); - }, [params.id, router]); + }, [params.id, currentUser]); const handleUpdate = async () => { - if (!title || !content) { - alert('제목과 내용을 입력해주세요.'); + if (!title.trim()) { + alert('제목을 입력해주세요.'); + return; + } + + if (!content.trim()) { + alert('내용을 입력해주세요.'); return; } try { - await updatePost(params.id, { title, content }); + await updatePost(params.id, { title: title.trim(), content: content.trim() }); alert('게시글이 수정되었습니다.'); router.push(`/posts/${params.id}`); } catch (error) { @@ -47,30 +85,78 @@ export default function EditPost({ params }: { params: { id: string } }) { } }; - if (loading) return

게시글을 불러오는 중입니다...

; + const handleCancel = () => { + if (title !== post?.title || content !== post?.content) { + const shouldCancel = confirm('수정사항이 있습니다. 정말로 취소하시겠습니까?'); + if (!shouldCancel) return; + } + router.push(`/posts/${params.id}`); + }; + + if (loading) { + return ( +
+
+
+
📝
+

게시글을 불러오는 중...

+
+
+
+ ); + } + + if (error) { + return ( +
+
+
+
⚠️
+

{error}

+ +
+
+
+ ); + } return (
-

게시글 수정

- - setTitle(e.target.value)} - /> - -