forked from JustOneBite/job-front
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor[JustOneBite#53]: Write페이지 작성완료
- Loading branch information
1 parent
a5291cf
commit 60b3139
Showing
7 changed files
with
321 additions
and
34 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<div> | ||
<h1>고객센터 - 글수정</h1> | ||
|
||
{/* Form 컴포넌트에 필요한 props 전달 */} | ||
<EditForm | ||
postId={postId} | ||
title={title} | ||
category={category} | ||
content={content} | ||
status={status} | ||
answer={answer} | ||
onCategoryChange={handleCategoryChange} | ||
onContentChange={handleContentChange} | ||
onSubmit={handleEditClick} | ||
onDelete={handleDeleteClick} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default DetailPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<form onSubmit={onSubmit}> | ||
<div> | ||
<div> | ||
<label>제목</label> | ||
<input | ||
type="text" | ||
value={title} | ||
disabled={status === 1} // 제목은 수정 불가능 | ||
/> | ||
</div> | ||
|
||
<div> | ||
<label>카테고리</label> | ||
<select id="category" name="category" | ||
required | ||
value={category} | ||
onChange={onCategoryChange} | ||
disabled={status === 1} | ||
> | ||
<option value="로그인">로그인</option> | ||
<option value="회원가입">회원가입</option> | ||
<option value="기타">기타</option> | ||
</select> | ||
</div> | ||
|
||
<div> | ||
<label>등록자</label> | ||
<input | ||
type="text" | ||
value={data.author} | ||
disabled // 등록자는 수정 불가능 | ||
/> | ||
</div> | ||
</div> | ||
|
||
{/* 여기가 추가부분 */} | ||
<div> | ||
<div> | ||
<div> | ||
<label>내용</label> | ||
<textarea | ||
value={content} | ||
onChange={onContentChange} // 내용 수정 가능 | ||
disabled={status === 1} // 상태가 '처리완료'일 때는 수정 불가능 | ||
/> | ||
</div> | ||
|
||
<div> | ||
<div> | ||
<label>답변</label> | ||
<span>{status === 0 ? '답변대기' : '처리완료'}</span> | ||
</div> | ||
<textarea | ||
value={answer || '답변을 준비 중입니다.'} | ||
disabled // 답변은 수정 불가능 | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
<div> | ||
{status === 0 && ( | ||
<> | ||
<button type="submit"> | ||
수정하기 | ||
</button> | ||
<button type="button" onClick={onDelete}> | ||
삭제하기 | ||
</button> | ||
</> | ||
)} | ||
<button | ||
type="button" | ||
onClick={goSVPage} | ||
> | ||
목록보기 | ||
</button> | ||
</div> | ||
</form> | ||
|
||
); | ||
}; | ||
|
||
export default Form; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.