diff --git a/student/app/mainPage/helpPage/edit/basicForm.js b/student/app/mainPage/helpPage/edit/basicForm.js new file mode 100644 index 0000000..e69de29 diff --git a/student/app/mainPage/helpPage/edit/detail.js b/student/app/mainPage/helpPage/edit/detail.js new file mode 100644 index 0000000..551b563 --- /dev/null +++ b/student/app/mainPage/helpPage/edit/detail.js @@ -0,0 +1,133 @@ +"use client"; +import { useSearchParams } from "next/navigation"; +import { useState , useEffect } from "react"; +import EditForm from './editForm'; + + +const DetailPage = () => { + const searchParams = useSearchParams(); + const postId = searchParams.get("postId"); + const title = searchParams.get("title"); + + const [status , setStatus] = useState(null); + const [content , setContent] = useState(''); + const [category , setCategory] = useState(''); + + const [answer , setAnswer] = useState(''); + const [loading , setLoading] = useState(true); + +// 렌더링이 될때 get요청 + useEffect(()=>{ + const fetchPostData = async () => { + try{ + const res = await fetch(`/api/posts/${postId}`); + if(!res.ok){ + throw new Error('Failed to fetch data'); + } + const postData = await res.json(); + + // console.log(postData); + + setStatus(postData.post.status); + setContent(postData.post.content); + setCategory(postData.post.category); + setAnswer(postData.post.answer); + } catch (err){ + console.error('Error fetching post Data:', err); + } finally { + setLoading(false); + } + }; + + if(postId){ + fetchPostData(); + } + + }, [postId]) + + +// 버튼을 누렀을때 post요청 + const handleEditClick = async (e) => { + e.preventDefault(); + + try { + const response = await fetch('/api/posts/updatePost', { + method: 'POST', // 또는 PUT 요청 사용 가능 + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + postId, + title, + category, + content, + status: status, // 상태도 함께 전송 + }), + }); + + if (response.ok) { + alert('수정된 내용이 저장되었습니다.'); + } else { + console.error('수정 요청에 실패했습니다.'); + } + } catch (error) { + console.error('서버와 연결하는 중 오류가 발생했습니다.', error); + } + } + + const handleDeleteClick = async (e) => { + e.preventDefault(); + + try { + const response = await fetch('/api/posts/updatePost', { + method: 'Delete', // 또는 PUT 요청 사용 가능 + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + postId, + title, + category, + content, + status: status, // 상태도 함께 전송 + }), + }); + + if (response.ok) { + alert('수정된 내용이 삭제된거 처럼되었습니다.'); + } else { + console.error('수정 요청에 실패했습니다.'); + } + } catch (error) { + console.error('서버와 연결하는 중 오류가 발생했습니다.', error); + } + } + + + const handleCategoryChange = (e) => setCategory(e.target.value); + const handleContentChange = (e) => setContent(e.target.value); + + + + return ( +
+

고객센터 - 글수정

+ + {/* Form 컴포넌트에 필요한 props 전달 */} + +
+ ); +}; + +export default DetailPage; \ No newline at end of file diff --git a/student/app/mainPage/helpPage/edit/editForm.js b/student/app/mainPage/helpPage/edit/editForm.js new file mode 100644 index 0000000..8a473e3 --- /dev/null +++ b/student/app/mainPage/helpPage/edit/editForm.js @@ -0,0 +1,99 @@ +"use client"; +import { posts } from '@/data' +import { useRouter } from "next/navigation" + +const Form = ({postId, title, category, content, status, answer, onCategoryChange, onContentChange, onSubmit , onDelete}) => { + const data = posts[postId-1]; + let router = useRouter(); + + const goSVPage = (e) => { + e.preventDefault(); + router.push('/studentService'); + } + + return ( +
+
+
+ + +
+ +
+ + +
+ +
+ + +
+
+ +{/* 여기가 추가부분 */} +
+
+
+ +