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

update button

parent deade4a1
......@@ -27,9 +27,9 @@ export const App = () => {
}
/>
<Route path="board" element={<Board />} />
<Route path="post/:postId/" element={<IntoPost />}>
<Route path="post/:postId" element={<IntoPost />} />
<Route path="edit" element={<EditPost />} />
</Route>
{/* </Route> */}
<Route
path="profile"
element={
......
......@@ -33,17 +33,18 @@ export function EditPost() {
const [disabled, setDisabled] = useState(false);
const [success, setSuccess] = useState(false);
async function handlePostSubmit(event: FormEvent) {
async function reWriteSubmit(event: FormEvent) {
event.preventDefault();
try {
setError("");
console.log("user data", user);
if (postingFormMatch()) {
if (postingFormMatch(user) === true) {
setLoading(true);
const res = await postApi.updating(user);
console.log("clear", res);
navigate("/board", { replace: true });
setSuccess(true);
setError("");
......@@ -56,17 +57,17 @@ export function EditPost() {
}
}
function postingFormMatch() {
function postingFormMatch(user: PostType) {
if (!isLength(user.title ?? "", { min: 1 })) {
setError("제목을 입력해 주세요.");
return false;
} else if (!isLength(user.text ?? "", { min: 1 })) {
setError("내용을 입력해 주세요.");
return false;
} else if (equals(city, "질문종류")) {
} else if (equals(city, "city")) {
setError("테마를 선택해 주세요.");
return false;
} else if (equals(theme, "질문종류")) {
} else if (equals(theme, "theme")) {
setError("도시를 선택해 주세요.");
return false;
} else {
......@@ -108,18 +109,17 @@ export function EditPost() {
return (
<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">
<p className="basis-1/12 gap-x-8">Id</p>
<p className="basis-8/12 invisible">empty</p>
<select
name="city"
id="Questions"
className="border border-3 border-black w-1/12"
onChange={cityChange}
defaultValue="질문종류"
defaultValue={post.city}
>
<option value="질문종류">도시</option>
<option value="city">도시</option>
<option value="Seoul">서울</option>
<option value="Busan">부산</option>
<option value="Incheon">인천</option>
......@@ -133,12 +133,11 @@ export function EditPost() {
</select>
<select
name="theme"
id="Questions"
className="border border-3 border-black w-1/12"
onChange={themeChange}
defaultValue="질문종류"
defaultValue={post.theme}
>
<option value="질문종류">테마</option>
<option value="theme">테마</option>
<option value="cycling">사이클링</option>
<option value="surfing">서핑</option>
<option value="activity">액티비티</option>
......@@ -157,7 +156,7 @@ export function EditPost() {
type="submit"
className="border border-black basis-1/12 gap-x-8"
>
글쓰기
Rewrite
</button>
</div>
......@@ -167,7 +166,9 @@ export function EditPost() {
onChange={titleChange}
placeholder="title"
className="w-full h-8"
></textarea>
>
{post.title}
</textarea>
</div>
<div className="flex border-2">
<textarea
......@@ -175,7 +176,9 @@ export function EditPost() {
name="text"
placeholder="text"
className="w-full h-96"
></textarea>
>
{post.text}
</textarea>
</div>
</form>
</div>
......
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 { PostType } from "../types";
......@@ -21,6 +21,7 @@ export function IntoPost() {
};
return (
<div>
<div>
<div>
<div className="flex flex-row basis-8">
......@@ -30,7 +31,7 @@ export function IntoPost() {
</button>
</div>
<div className="border-2 border-current rounded">
<Link to={`/post/${post._id}/edit`} state={post}>
<Link to="/edit" state={post}>
<button>update</button>
</Link>
</div>
......@@ -58,7 +59,9 @@ export function IntoPost() {
</div>
</div>
</div>
<div className="border-2 border-black rounded h-96">{post.text}</div>
<div className="border-2 border-black rounded h-full">{post.text}</div>
</div>
{/* <Outlet /> */}
</div>
);
}
......@@ -94,7 +94,7 @@ export const getOnePost = asyncWrap(async (req, res) => {
export const deleteOnePost = asyncWrap(async (req, res) => {
const { postId } = req.params;
console.log(postId);
// console.log(postId);
const deleteCount = await postDb.deletePost(postId);
return res.json(deleteCount);
......
......@@ -42,7 +42,15 @@ export const deletePost = async (_id: string) => {
export const updateOnePost = async (post: PostType, _id: string) => {
const newPost = await Post.findOneAndUpdate(
{ _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 }
);
return newPost;
......
......@@ -9,11 +9,9 @@ router.route("/").get(postCtrl.getAllPost);
router
.route("/:postId")
.post(authCtrl.requireLogin, postCtrl.addCounts)
.get(authCtrl.requireLogin, postCtrl.getOnePost);
router.route("/:postId").delete(authCtrl.requireLogin, postCtrl.deleteOnePost); // authenticate
router.route("/:postId").put(authCtrl.requireLogin, postCtrl.updatePost);
.get(authCtrl.requireLogin, postCtrl.getOnePost)
.delete(authCtrl.requireLogin, postCtrl.deleteOnePost) // authenticate
.put(authCtrl.requireLogin, postCtrl.updatePost);
router.param("postId", postCtrl.userByPostId);
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