Commit 33e86a75 authored by Lee Soobeom's avatar Lee Soobeom
Browse files

update button

parent deade4a1
...@@ -27,9 +27,9 @@ export const App = () => { ...@@ -27,9 +27,9 @@ export const App = () => {
} }
/> />
<Route path="board" element={<Board />} /> <Route path="board" element={<Board />} />
<Route path="post/:postId/" element={<IntoPost />}> <Route path="post/:postId" element={<IntoPost />} />
<Route path="edit" element={<EditPost />} /> <Route path="edit" element={<EditPost />} />
</Route> {/* </Route> */}
<Route <Route
path="profile" path="profile"
element={ element={
......
...@@ -33,17 +33,18 @@ export function EditPost() { ...@@ -33,17 +33,18 @@ export function EditPost() {
const [disabled, setDisabled] = useState(false); const [disabled, setDisabled] = useState(false);
const [success, setSuccess] = useState(false); const [success, setSuccess] = useState(false);
async function handlePostSubmit(event: FormEvent) { async function reWriteSubmit(event: FormEvent) {
event.preventDefault(); event.preventDefault();
try { try {
setError(""); setError("");
console.log("user data", user); console.log("user data", user);
if (postingFormMatch()) { if (postingFormMatch(user) === true) {
setLoading(true); setLoading(true);
const res = await postApi.updating(user); const res = await postApi.updating(user);
console.log("clear", res);
navigate("/board", { replace: true }); navigate("/board", { replace: true });
setSuccess(true); setSuccess(true);
setError(""); setError("");
...@@ -56,17 +57,17 @@ export function EditPost() { ...@@ -56,17 +57,17 @@ export function EditPost() {
} }
} }
function postingFormMatch() { function postingFormMatch(user: PostType) {
if (!isLength(user.title ?? "", { min: 1 })) { if (!isLength(user.title ?? "", { min: 1 })) {
setError("제목을 입력해 주세요."); setError("제목을 입력해 주세요.");
return false; return false;
} else if (!isLength(user.text ?? "", { min: 1 })) { } else if (!isLength(user.text ?? "", { min: 1 })) {
setError("내용을 입력해 주세요."); setError("내용을 입력해 주세요.");
return false; return false;
} else if (equals(city, "질문종류")) { } else if (equals(city, "city")) {
setError("테마를 선택해 주세요."); setError("테마를 선택해 주세요.");
return false; return false;
} else if (equals(theme, "질문종류")) { } else if (equals(theme, "theme")) {
setError("도시를 선택해 주세요."); setError("도시를 선택해 주세요.");
return false; return false;
} else { } else {
...@@ -108,18 +109,17 @@ export function EditPost() { ...@@ -108,18 +109,17 @@ export function EditPost() {
return ( return (
<div className="flex flex-col border-3"> <div className="flex flex-col border-3">
<form onSubmit={handlePostSubmit} className="w-full items-center"> <form onSubmit={reWriteSubmit} className="w-full items-center">
<div className="flex flex-row relative"> <div className="flex flex-row relative">
<p className="basis-1/12 gap-x-8">Id</p> <p className="basis-1/12 gap-x-8">Id</p>
<p className="basis-8/12 invisible">empty</p> <p className="basis-8/12 invisible">empty</p>
<select <select
name="city" name="city"
id="Questions"
className="border border-3 border-black w-1/12" className="border border-3 border-black w-1/12"
onChange={cityChange} onChange={cityChange}
defaultValue="질문종류" defaultValue={post.city}
> >
<option value="질문종류">도시</option> <option value="city">도시</option>
<option value="Seoul">서울</option> <option value="Seoul">서울</option>
<option value="Busan">부산</option> <option value="Busan">부산</option>
<option value="Incheon">인천</option> <option value="Incheon">인천</option>
...@@ -133,12 +133,11 @@ export function EditPost() { ...@@ -133,12 +133,11 @@ export function EditPost() {
</select> </select>
<select <select
name="theme" name="theme"
id="Questions"
className="border border-3 border-black w-1/12" className="border border-3 border-black w-1/12"
onChange={themeChange} onChange={themeChange}
defaultValue="질문종류" defaultValue={post.theme}
> >
<option value="질문종류">테마</option> <option value="theme">테마</option>
<option value="cycling">사이클링</option> <option value="cycling">사이클링</option>
<option value="surfing">서핑</option> <option value="surfing">서핑</option>
<option value="activity">액티비티</option> <option value="activity">액티비티</option>
...@@ -157,7 +156,7 @@ export function EditPost() { ...@@ -157,7 +156,7 @@ export function EditPost() {
type="submit" type="submit"
className="border border-black basis-1/12 gap-x-8" className="border border-black basis-1/12 gap-x-8"
> >
글쓰기 Rewrite
</button> </button>
</div> </div>
...@@ -167,7 +166,9 @@ export function EditPost() { ...@@ -167,7 +166,9 @@ export function EditPost() {
onChange={titleChange} onChange={titleChange}
placeholder="title" placeholder="title"
className="w-full h-8" className="w-full h-8"
></textarea> >
{post.title}
</textarea>
</div> </div>
<div className="flex border-2"> <div className="flex border-2">
<textarea <textarea
...@@ -175,7 +176,9 @@ export function EditPost() { ...@@ -175,7 +176,9 @@ export function EditPost() {
name="text" name="text"
placeholder="text" placeholder="text"
className="w-full h-96" className="w-full h-96"
></textarea> >
{post.text}
</textarea>
</div> </div>
</form> </form>
</div> </div>
......
import React, { MouseEvent } from "react"; import React, { MouseEvent } from "react";
import { useLocation, useNavigate, Link } from "react-router-dom"; import { useLocation, useNavigate, Link, Outlet } from "react-router-dom";
import { postApi } from "../apis"; import { postApi } from "../apis";
import { PostType } from "../types"; import { PostType } from "../types";
...@@ -23,42 +23,45 @@ export function IntoPost() { ...@@ -23,42 +23,45 @@ export function IntoPost() {
return ( return (
<div> <div>
<div> <div>
<div className="flex flex-row basis-8"> <div>
<div className="border-2 border-current rounded"> <div className="flex flex-row basis-8">
<button id={post._id} onClick={handleDeleteClick}> <div className="border-2 border-current rounded">
delete <button id={post._id} onClick={handleDeleteClick}>
</button> delete
</button>
</div>
<div className="border-2 border-current rounded">
<Link to="/edit" state={post}>
<button>update</button>
</Link>
</div>
</div> </div>
<div className="border-2 border-current rounded"> <div className="flex flex-row">
<Link to={`/post/${post._id}/edit`} state={post}> <div className="flex basis-3/4 border-2 border-black rounded">
<button>update</button> 제목: {post.title}
</Link> </div>
<div className="flex basis-1/4 border-2 border-black rounded">
작성자: nickname
</div>
</div> </div>
</div> <div className="flex flex-row">
<div className="flex flex-row"> <div className="flex basis-1/4 border-2 border-black rounded">
<div className="flex basis-3/4 border-2 border-black rounded"> 도시: {post.city}
제목: {post.title} </div>
</div> <div className="flex basis-1/4 border-2 border-black rounded">
<div className="flex basis-1/4 border-2 border-black rounded"> 테마: {post.theme}
작성자: nickname </div>
</div> <div className="flex basis-1/4 border-2 border-black rounded">
</div> 작성일: {post.date}
<div className="flex flex-row"> </div>
<div className="flex basis-1/4 border-2 border-black rounded"> <div className="flex basis-1/4 border-2 border-black rounded">
도시: {post.city} 조회수: {post.counts}
</div> </div>
<div className="flex basis-1/4 border-2 border-black rounded">
테마: {post.theme}
</div>
<div className="flex basis-1/4 border-2 border-black rounded">
작성일: {post.date}
</div>
<div className="flex basis-1/4 border-2 border-black rounded">
조회수: {post.counts}
</div> </div>
</div> </div>
<div className="border-2 border-black rounded h-full">{post.text}</div>
</div> </div>
<div className="border-2 border-black rounded h-96">{post.text}</div> {/* <Outlet /> */}
</div> </div>
); );
} }
...@@ -94,7 +94,7 @@ export const getOnePost = asyncWrap(async (req, res) => { ...@@ -94,7 +94,7 @@ export const getOnePost = asyncWrap(async (req, res) => {
export const deleteOnePost = asyncWrap(async (req, res) => { export const deleteOnePost = asyncWrap(async (req, res) => {
const { postId } = req.params; const { postId } = req.params;
console.log(postId); // console.log(postId);
const deleteCount = await postDb.deletePost(postId); const deleteCount = await postDb.deletePost(postId);
return res.json(deleteCount); return res.json(deleteCount);
......
...@@ -42,7 +42,15 @@ export const deletePost = async (_id: string) => { ...@@ -42,7 +42,15 @@ export const deletePost = async (_id: string) => {
export const updateOnePost = async (post: PostType, _id: string) => { export const updateOnePost = async (post: PostType, _id: string) => {
const newPost = await Post.findOneAndUpdate( const newPost = await Post.findOneAndUpdate(
{ _id: _id }, { _id: _id },
{ post }, {
title: post.title,
text: post.text,
theme: post.theme,
city: post.city,
date: post.date,
counts: post.counts,
user: post.user,
},
{ new: true } { new: true }
); );
return newPost; return newPost;
......
...@@ -9,11 +9,9 @@ router.route("/").get(postCtrl.getAllPost); ...@@ -9,11 +9,9 @@ router.route("/").get(postCtrl.getAllPost);
router router
.route("/:postId") .route("/:postId")
.post(authCtrl.requireLogin, postCtrl.addCounts) .post(authCtrl.requireLogin, postCtrl.addCounts)
.get(authCtrl.requireLogin, postCtrl.getOnePost); .get(authCtrl.requireLogin, postCtrl.getOnePost)
.delete(authCtrl.requireLogin, postCtrl.deleteOnePost) // authenticate
router.route("/:postId").delete(authCtrl.requireLogin, postCtrl.deleteOnePost); // authenticate .put(authCtrl.requireLogin, postCtrl.updatePost);
router.route("/:postId").put(authCtrl.requireLogin, postCtrl.updatePost);
router.param("postId", postCtrl.userByPostId); router.param("postId", postCtrl.userByPostId);
export default router; export default router;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment